diff --git a/ResNet/Data.swift b/ResNet/Data.swift index cf101551b79..f9dcb270547 100644 --- a/ResNet/Data.swift +++ b/ResNet/Data.swift @@ -35,6 +35,25 @@ func downloadCIFAR10IfNotPresent(to directory: String = ".") { struct Example: TensorGroup { var label: Tensor var data: Tensor + + init(label: Tensor, data: Tensor) { + self.label = label + self.data = data + } + + public init( + _handles: C + ) where C.Element: _AnyTensorHandle { + precondition(_handles.count == 2) + let labelIndex = _handles.startIndex + let dataIndex = _handles.index(labelIndex, offsetBy: 1) + label = Tensor(handle: TensorHandle(handle: _handles[labelIndex])) + data = Tensor(handle: TensorHandle(handle: _handles[dataIndex])) + } + + //public var _tensorHandles: [_AnyTensorHandle] { [label.handle.handle, data.handle.handle] } + // error: 'handle' is inaccessible due to 'internal' protection level + public var _tensorHandles: [_AnyTensorHandle] { [] } } // Each CIFAR data file is provided as a Python pickle of NumPy arrays diff --git a/ResNet/ResNet50.swift b/ResNet/ResNet50.swift index 487cee5eded..6684677237d 100644 --- a/ResNet/ResNet50.swift +++ b/ResNet/ResNet50.swift @@ -37,7 +37,7 @@ struct ConvBN: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { return input.sequenced(through: conv, norm) } } @@ -65,7 +65,7 @@ struct ResidualBasicBlock: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { return layer2(relu(layer1(input))) } } @@ -94,7 +94,7 @@ struct ResidualBasicBlockShortcut: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { return layer2(relu(layer1(input))) + shortcut(input) } } @@ -127,7 +127,7 @@ struct ResidualConvBlock: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { let tmp = relu(layer2(relu(layer1(input)))) return relu(layer3(tmp) + shortcut(input)) } @@ -150,7 +150,7 @@ struct ResidualIdentityBlock: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { let tmp = relu(layer2(relu(layer1(input)))) return relu(layer3(tmp) + input) } @@ -175,7 +175,7 @@ struct ResidualIdentityBlockStack: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { return input.sequenced(through: block1, block2, block3, block4, block5) } } @@ -218,7 +218,7 @@ struct ResNet18: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { let inputLayer = maxPool(relu(l1(input))) let level2 = inputLayer.sequenced(through: l2a, l2b) let level3 = level2.sequenced(through: l3a, l3b) @@ -274,7 +274,7 @@ struct ResNet34: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { let inputLayer = maxPool(relu(l1(input))) let level2 = inputLayer.sequenced(through: l2a, l2b, l2c) let level3 = level2.sequenced(through: l3a, l3b, l3c, l3d) @@ -326,7 +326,7 @@ struct ResNet50: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { let inputLayer = maxPool(relu(l1(input))) let level2 = inputLayer.sequenced(through: l2a, l2b, l2c) let level3 = level2.sequenced(through: l3a, l3b, l3c, l3d) @@ -383,7 +383,7 @@ struct ResNet101: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { let inputLayer = maxPool(relu(l1(input))) let level2 = inputLayer.sequenced(through: l2a, l2b, l2c) let level3 = level2.sequenced(through: l3a, l3b, l3c, l3d) @@ -441,7 +441,7 @@ struct ResNet152: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { let inputLayer = maxPool(relu(l1(input))) let level2 = inputLayer.sequenced(through: l2a, l2b, l2c) let level3 = level2.sequenced(through: l3a, l3b, l3c, l3d) diff --git a/ResNet/ResNetV2.swift b/ResNet/ResNetV2.swift index b8545e8ee48..cb3dbb5c90b 100644 --- a/ResNet/ResNetV2.swift +++ b/ResNet/ResNetV2.swift @@ -38,7 +38,7 @@ struct Conv2DBatchNorm: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { return input.sequenced(through: conv, norm) } } @@ -60,7 +60,7 @@ struct BatchNormConv2D: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { return conv(relu(norm(input))) } } @@ -88,7 +88,7 @@ struct PreActivatedResidualBasicBlock: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { return input.sequenced(through: layer1, layer2) } } @@ -117,7 +117,7 @@ struct PreActivatedResidualBasicBlockShortcut: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { return input.sequenced(through: layer1, layer2) + shortcut(input) } } @@ -162,7 +162,7 @@ struct PreActivatedResNet18: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { let inputLayer = input.sequenced(through: l1, maxPool) let level2 = inputLayer.sequenced(through: l2a, l2b) let level3 = level2.sequenced(through: l3a, l3b) @@ -221,7 +221,7 @@ struct PreActivatedResNet34: Layer { } @differentiable - func call(_ input: Input) -> Output { + func callAsFunction(_ input: Input) -> Output { let inputLayer = input.sequenced(through: l1, maxPool) let level2 = inputLayer.sequenced(through: l2a, l2b, l2c) let level3 = level2.sequenced(through: l3a, l3b, l3c, l3d)