From 04361acbd50ef3dbd1d120e20ef19f74cccdd040 Mon Sep 17 00:00:00 2001 From: Yuta Koshizawa Date: Wed, 25 May 2016 15:21:06 +0900 Subject: [PATCH] Replace combinations of `guard` and `fatalError` with `precondition`s --- TensorSwift/Tensor.swift | 16 ++++++++-------- TensorSwift/TensorNN.swift | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/TensorSwift/Tensor.swift b/TensorSwift/Tensor.swift index 3714abd..191087b 100644 --- a/TensorSwift/Tensor.swift +++ b/TensorSwift/Tensor.swift @@ -11,7 +11,7 @@ public struct Tensor { public init(shape: Shape, elements: [Element]) { let volume = shape.volume - guard elements.count >= volume else { fatalError("`elements.count` must be greater than or equal to `shape.volume`: elements.count = \(elements.count), shape.volume = \(shape.volume)") } + precondition(elements.count >= volume, "`elements.count` must be greater than or equal to `shape.volume`: elements.count = \(elements.count), shape.volume = \(shape.volume)") self.shape = shape self.elements = (elements.count == volume) ? elements : Array(elements[0.. Tensor { extension Tensor { // Matrix public func matmul(tensor: Tensor) -> Tensor { - guard shape.dimensions.count == 2 else { fatalError("This tensor is not a matrix: shape = \(shape)") } - guard tensor.shape.dimensions.count == 2 else { fatalError("The given tensor is not a matrix: shape = \(tensor.shape)") } - guard tensor.shape.dimensions[0] == shape.dimensions[1] else { fatalError("Incompatible shapes of matrices: self.shape = \(shape), tensor.shape = \(tensor.shape)") } + precondition(shape.dimensions.count == 2, "This tensor is not a matrix: shape = \(shape)") + precondition(tensor.shape.dimensions.count == 2, "The given tensor is not a matrix: shape = \(tensor.shape)") + precondition(tensor.shape.dimensions[0] == shape.dimensions[1], "Incompatible shapes of matrices: self.shape = \(shape), tensor.shape = \(tensor.shape)") #if os(iOS) || os(OSX) let result = Tensor(shape: [shape.dimensions[0], tensor.shape.dimensions[1]]) diff --git a/TensorSwift/TensorNN.swift b/TensorSwift/TensorNN.swift index be18aae..1579d11 100644 --- a/TensorSwift/TensorNN.swift +++ b/TensorSwift/TensorNN.swift @@ -17,11 +17,11 @@ extension Tensor { extension Tensor { public func maxPool(kernelSize kernelSize: [Int], strides: [Int]) -> Tensor { // padding = Same - guard shape.dimensions.count == 3 else { fatalError("`shape.dimensions.count` must be 3: \(shape.dimensions.count)") } - guard kernelSize.count == 3 else { fatalError("`ksize.count` must be 3: \(kernelSize.count)") } - guard kernelSize[2] == 1 else { fatalError("`ksize[3]` != 1 is not supported: \(kernelSize[2])") } - guard strides.count == 3 else { fatalError("`strides.count` must be 3: \(strides.count)") } - guard strides[2] == 1 else { fatalError("`strides[2]` != 1 is not supported: \(strides[2])") } + precondition(shape.dimensions.count == 3, "`shape.dimensions.count` must be 3: \(shape.dimensions.count)") + precondition(kernelSize.count == 3, "`ksize.count` must be 3: \(kernelSize.count)") + precondition(kernelSize[2] == 1, "`ksize[3]` != 1 is not supported: \(kernelSize[2])") + precondition(strides.count == 3, "`strides.count` must be 3: \(strides.count)") + precondition(strides[2] == 1, "`strides[2]` != 1 is not supported: \(strides[2])") let inRows = shape.dimensions[0].value let inCols = shape.dimensions[1].value @@ -77,11 +77,11 @@ extension Tensor { public func conv2d(filter filter: Tensor, strides: [Int]) -> Tensor { // padding = Same let inChannels = filter.shape.dimensions[2].value - guard shape.dimensions.count == 3 else { fatalError("`shape.dimensions.count` must be 3: \(shape.dimensions.count)") } - guard filter.shape.dimensions.count == 4 else { fatalError("`filter.shape.dimensions.count` must be 4: \(filter.shape.dimensions.count)") } - guard strides.count == 3 else { fatalError("`strides.count` must be 3: \(strides.count)") } - guard strides[2] == 1 else { fatalError("`strides[2]` must be 1") } - guard shape.dimensions[2].value == inChannels else { fatalError("The number of channels of this tensor and the filter are not compatible: \(shape.dimensions[2]) != \(inChannels)") } + precondition(shape.dimensions.count == 3, "`shape.dimensions.count` must be 3: \(shape.dimensions.count)") + precondition(filter.shape.dimensions.count == 4, "`filter.shape.dimensions.count` must be 4: \(filter.shape.dimensions.count)") + precondition(strides.count == 3, "`strides.count` must be 3: \(strides.count)") + precondition(strides[2] == 1, "`strides[2]` must be 1") + precondition(shape.dimensions[2].value == inChannels, "The number of channels of this tensor and the filter are not compatible: \(shape.dimensions[2]) != \(inChannels)") let inRows = shape.dimensions[0].value let inCols = shape.dimensions[1].value