Skip to content

Commit 7a9f52a

Browse files
committed
Minor formatting fixes.
1 parent 88df455 commit 7a9f52a

File tree

9 files changed

+100
-68
lines changed

9 files changed

+100
-68
lines changed

Datasets/CIFAR10/CIFAR10.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import TensorFlow
2727
public struct CIFAR10 {
2828
public let trainingDataset: Dataset<CIFARExample>
2929
public let testDataset: Dataset<CIFARExample>
30-
30+
3131
public init() {
3232
self.trainingDataset = Dataset<CIFARExample>(elements: loadCIFARTrainingFiles())
3333
self.testDataset = Dataset<CIFARExample>(elements: loadCIFARTestFile())

Datasets/CIFAR10/CIFARExample.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@ public struct CIFARExample: TensorGroup {
3333
data = Tensor<Float>(handle: TensorHandle<Float>(handle: _handles[dataIndex]))
3434
}
3535
}
36-

Examples/Custom-CIFAR10/main.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import TensorFlow
1615
import Datasets
16+
import TensorFlow
1717

1818
let batchSize = 100
1919

@@ -53,15 +53,17 @@ for epoch in 1...100 {
5353
testBatchCount += 1
5454

5555
let correctPredictions = logits.argmax(squeezingAxis: 1) .== labels
56-
correctGuessCount = correctGuessCount +
57-
Int(Tensor<Int32>(correctPredictions).sum().scalarized())
56+
correctGuessCount = correctGuessCount + Int(
57+
Tensor<Int32>(correctPredictions).sum().scalarized())
5858
totalGuessCount = totalGuessCount + batchSize
5959
}
6060

6161
let accuracy = Float(correctGuessCount) / Float(totalGuessCount)
62-
print("""
62+
print(
63+
"""
6364
[Epoch \(epoch)] \
6465
Accuracy: \(correctGuessCount)/\(totalGuessCount) (\(accuracy)) \
6566
Loss: \(testLossSum / Float(testBatchCount))
66-
""")
67+
"""
68+
)
6769
}

Examples/ResNet-CIFAR10/main.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import TensorFlow
16-
import ImageClassificationModels
1715
import Datasets
16+
import ImageClassificationModels
17+
import TensorFlow
1818

1919
let batchSize = 100
2020

@@ -57,15 +57,17 @@ for epoch in 1...10 {
5757
testBatchCount += 1
5858

5959
let correctPredictions = logits.argmax(squeezingAxis: 1) .== labels
60-
correctGuessCount = correctGuessCount +
61-
Int(Tensor<Int32>(correctPredictions).sum().scalarized())
60+
correctGuessCount = correctGuessCount + Int(
61+
Tensor<Int32>(correctPredictions).sum().scalarized())
6262
totalGuessCount = totalGuessCount + batchSize
6363
}
6464

6565
let accuracy = Float(correctGuessCount) / Float(totalGuessCount)
66-
print("""
66+
print(
67+
"""
6768
[Epoch \(epoch)] \
6869
Accuracy: \(correctGuessCount)/\(totalGuessCount) (\(accuracy)) \
6970
Loss: \(testLossSum / Float(testBatchCount))
70-
""")
71+
"""
72+
)
7173
}

Models/ImageClassification/DifferentiableReduce.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ extension Array where Element: Differentiable {
1717
@differentiable(wrt: (self, initialResult), vjp: reduceDerivative)
1818
func differentiableReduce<Result: Differentiable>(
1919
_ initialResult: Result,
20-
_ nextPartialResult: @differentiable (Result, Element) -> Result
20+
_ nextPartialResult: @differentiable(Result, Element) -> Result
2121
) -> Result {
2222
return reduce(initialResult, nextPartialResult)
2323
}
2424

2525
func reduceDerivative<Result: Differentiable>(
2626
_ initialResult: Result,
27-
_ nextPartialResult: @differentiable (Result, Element) -> Result
27+
_ nextPartialResult: @differentiable(Result, Element) -> Result
2828
) -> (Result, (Result.TangentVector) -> (Array.TangentVector, Result.TangentVector)) {
29-
var pullbacks: [(Result.TangentVector)
29+
var pullbacks:
30+
[(Result.TangentVector)
3031
-> (Result.TangentVector, Element.TangentVector)] = []
3132
let count = self.count
3233
pullbacks.reserveCapacity(count)
@@ -36,16 +37,19 @@ extension Array where Element: Differentiable {
3637
result = y
3738
pullbacks.append(pb)
3839
}
39-
return (value: result, pullback: { cotangent in
40-
var resultCotangent = cotangent
41-
var elementCotangents = TangentVector([])
42-
elementCotangents.base.reserveCapacity(count)
43-
for pullback in pullbacks.reversed() {
44-
let (newResultCotangent, elementCotangent) = pullback(resultCotangent)
45-
resultCotangent = newResultCotangent
46-
elementCotangents.base.append(elementCotangent)
40+
return (
41+
value: result,
42+
pullback: { cotangent in
43+
var resultCotangent = cotangent
44+
var elementCotangents = TangentVector([])
45+
elementCotangents.base.reserveCapacity(count)
46+
for pullback in pullbacks.reversed() {
47+
let (newResultCotangent, elementCotangent) = pullback(resultCotangent)
48+
resultCotangent = newResultCotangent
49+
elementCotangents.base.append(elementCotangent)
50+
}
51+
return (TangentVector(elementCotangents.base.reversed()), resultCotangent)
4752
}
48-
return (TangentVector(elementCotangents.base.reversed()), resultCotangent)
49-
})
53+
)
5054
}
5155
}

Models/ImageClassification/LeNet-5.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public struct LeNet: Layer {
3333
public var fc3 = Dense<Float>(inputSize: 84, outputSize: 10, activation: softmax)
3434

3535
public init() {}
36-
36+
3737
@differentiable
3838
public func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
3939
let convolved = input.sequenced(through: conv1, pool1, conv2, pool2)

Models/ImageClassification/ResNet50.swift

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public struct ResidualBasicBlock: Layer {
9797

9898
public struct ResidualBasicBlockStack: Layer {
9999
public var blocks: [ResidualBasicBlock] = []
100+
100101
public init(featureCounts: (Int, Int, Int, Int), kernelSize: Int = 3, blockCount: Int) {
101102
for _ in 1..<blockCount {
102103
blocks += [ResidualBasicBlock(featureCounts: featureCounts, kernelSize: kernelSize)]
@@ -165,6 +166,7 @@ public struct ResidualIdentityBlock: Layer {
165166

166167
public struct ResidualIdentityBlockStack: Layer {
167168
public var blocks: [ResidualIdentityBlock] = []
169+
168170
public init(featureCounts: (Int, Int, Int, Int), kernelSize: Int = 3, blockCount: Int) {
169171
for _ in 1..<blockCount {
170172
blocks += [ResidualIdentityBlock(featureCounts: featureCounts, kernelSize: kernelSize)]
@@ -209,18 +211,22 @@ public struct ResNetBasic: Layer {
209211
classifier = Dense(inputSize: 512, outputSize: 1000)
210212
case .cifar:
211213
l1 = ConvBN(filterShape: (3, 3, 3, 64), padding: .same)
212-
maxPool = MaxPool2D(poolSize: (1, 1), strides: (1, 1)) // no-op
214+
maxPool = MaxPool2D(poolSize: (1, 1), strides: (1, 1)) // no-op
213215
avgPool = AvgPool2D(poolSize: (4, 4), strides: (4, 4))
214216
classifier = Dense(inputSize: 512, outputSize: 10)
215217
}
216218

217-
l2b = ResidualBasicBlockStack(featureCounts: (64, 64, 64, 64),
219+
l2b = ResidualBasicBlockStack(
220+
featureCounts: (64, 64, 64, 64),
218221
blockCount: layerBlockCounts.0)
219-
l3b = ResidualBasicBlockStack(featureCounts: (128, 128, 128, 128),
222+
l3b = ResidualBasicBlockStack(
223+
featureCounts: (128, 128, 128, 128),
220224
blockCount: layerBlockCounts.1)
221-
l4b = ResidualBasicBlockStack(featureCounts: (256, 256, 256, 256),
225+
l4b = ResidualBasicBlockStack(
226+
featureCounts: (256, 256, 256, 256),
222227
blockCount: layerBlockCounts.2)
223-
l5b = ResidualBasicBlockStack(featureCounts: (512, 512, 512, 512),
228+
l5b = ResidualBasicBlockStack(
229+
featureCounts: (512, 512, 512, 512),
224230
blockCount: layerBlockCounts.3)
225231
}
226232

@@ -235,13 +241,13 @@ public struct ResNetBasic: Layer {
235241
}
236242
}
237243

238-
public extension ResNetBasic {
239-
enum Kind {
244+
extension ResNetBasic {
245+
public enum Kind {
240246
case resNet18
241247
case resNet34
242248
}
243249

244-
init(inputKind: Kind, dataKind: DataKind) {
250+
public init(inputKind: Kind, dataKind: DataKind) {
245251
switch inputKind {
246252
case .resNet18:
247253
self.init(dataKind: dataKind, layerBlockCounts: (2, 2, 2, 2))
@@ -280,18 +286,22 @@ public struct ResNet: Layer {
280286
classifier = Dense(inputSize: 2048, outputSize: 1000)
281287
case .cifar:
282288
l1 = ConvBN(filterShape: (3, 3, 3, 64), padding: .same)
283-
maxPool = MaxPool2D(poolSize: (1, 1), strides: (1, 1)) // no-op
289+
maxPool = MaxPool2D(poolSize: (1, 1), strides: (1, 1)) // no-op
284290
avgPool = AvgPool2D(poolSize: (4, 4), strides: (4, 4))
285291
classifier = Dense(inputSize: 2048, outputSize: 10)
286292
}
287293

288-
l2b = ResidualIdentityBlockStack(featureCounts: (256, 64, 64, 256),
294+
l2b = ResidualIdentityBlockStack(
295+
featureCounts: (256, 64, 64, 256),
289296
blockCount: layerBlockCounts.0)
290-
l3b = ResidualIdentityBlockStack(featureCounts: (512, 128, 128, 512),
297+
l3b = ResidualIdentityBlockStack(
298+
featureCounts: (512, 128, 128, 512),
291299
blockCount: layerBlockCounts.1)
292-
l4b = ResidualIdentityBlockStack(featureCounts: (1024, 256, 256, 1024),
300+
l4b = ResidualIdentityBlockStack(
301+
featureCounts: (1024, 256, 256, 1024),
293302
blockCount: layerBlockCounts.2)
294-
l5b = ResidualIdentityBlockStack(featureCounts: (2048, 512, 512, 2048),
303+
l5b = ResidualIdentityBlockStack(
304+
featureCounts: (2048, 512, 512, 2048),
295305
blockCount: layerBlockCounts.3)
296306
}
297307

@@ -306,14 +316,14 @@ public struct ResNet: Layer {
306316
}
307317
}
308318

309-
public extension ResNet {
310-
enum Kind {
319+
extension ResNet {
320+
public enum Kind {
311321
case resNet50
312322
case resNet101
313323
case resNet152
314324
}
315325

316-
init(inputKind: Kind, dataKind: DataKind) {
326+
public init(inputKind: Kind, dataKind: DataKind) {
317327
switch inputKind {
318328
case .resNet50:
319329
self.init(dataKind: dataKind, layerBlockCounts: (3, 4, 6, 3))

Models/ImageClassification/ResNetV2.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public struct PreActivatedResNet18: Layer {
125125

126126
public var l5a = PreActivatedResidualBasicBlockShortcut(featureCounts: (256, 512, 512, 512))
127127
public var l5b = PreActivatedResidualBasicBlock(featureCounts: (512, 512, 512, 512))
128-
128+
129129
public var norm: BatchNorm<Float>
130130
public var avgPool: AvgPool2D<Float>
131131
public var flatten = Flatten<Float>()
@@ -139,7 +139,7 @@ public struct PreActivatedResNet18: Layer {
139139
avgPool = AvgPool2D(poolSize: (7, 7), strides: (7, 7))
140140
if imageSize == 32 {
141141
l1 = Conv2DBatchNorm(filterShape: (3, 3, 3, 64), padding: .same)
142-
maxPool = MaxPool2D(poolSize: (1, 1), strides: (1, 1)) // no-op
142+
maxPool = MaxPool2D(poolSize: (1, 1), strides: (1, 1)) // no-op
143143
avgPool = AvgPool2D(poolSize: (4, 4), strides: (4, 4))
144144
}
145145
norm = BatchNorm(featureCount: 512)
@@ -181,7 +181,7 @@ public struct PreActivatedResNet34: Layer {
181181
public var l5a = PreActivatedResidualBasicBlockShortcut(featureCounts: (256, 512, 512, 512))
182182
public var l5b = PreActivatedResidualBasicBlock(featureCounts: (512, 512, 512, 512))
183183
public var l5c = PreActivatedResidualBasicBlock(featureCounts: (512, 512, 512, 512))
184-
184+
185185
public var norm: BatchNorm<Float>
186186
public var avgPool: AvgPool2D<Float>
187187
public var flatten = Flatten<Float>()
@@ -195,7 +195,7 @@ public struct PreActivatedResNet34: Layer {
195195
avgPool = AvgPool2D(poolSize: (7, 7), strides: (7, 7))
196196
if imageSize == 32 {
197197
l1 = Conv2DBatchNorm(filterShape: (3, 3, 3, 64), padding: .same)
198-
maxPool = MaxPool2D(poolSize: (1, 1), strides: (1, 1)) // no-op
198+
maxPool = MaxPool2D(poolSize: (1, 1), strides: (1, 1)) // no-op
199199
avgPool = AvgPool2D(poolSize: (4, 4), strides: (4, 4))
200200
}
201201
norm = BatchNorm(featureCount: 512)

Models/ImageClassification/WideResNet.swift

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,46 @@ public struct WideResNetBasicBlock: Layer {
4949
public var shortcut: Conv2D<Float>
5050

5151
public init(
52-
featureCounts: (Int, Int),
52+
featureCounts: (Int, Int),
5353
kernelSize: Int = 3,
5454
depthFactor: Int = 2,
5555
widenFactor: Int = 1,
5656
initialStride: (Int, Int) = (2, 2)
5757
) {
5858
if initialStride == (1, 1) {
59-
self.blocks = [BatchNormConv2DBlock(
60-
filterShape: (kernelSize, kernelSize,
61-
featureCounts.0, featureCounts.1 * widenFactor),
62-
strides: initialStride)]
59+
self.blocks = [
60+
BatchNormConv2DBlock(
61+
filterShape: (
62+
kernelSize, kernelSize,
63+
featureCounts.0, featureCounts.1 * widenFactor
64+
),
65+
strides: initialStride)
66+
]
6367
self.shortcut = Conv2D(
6468
filterShape: (1, 1, featureCounts.0, featureCounts.1 * widenFactor),
6569
strides: initialStride)
6670
} else {
67-
self.blocks = [BatchNormConv2DBlock(
68-
filterShape: (kernelSize, kernelSize,
69-
featureCounts.0 * widenFactor, featureCounts.1 * widenFactor),
70-
strides: initialStride)]
71+
self.blocks = [
72+
BatchNormConv2DBlock(
73+
filterShape: (
74+
kernelSize, kernelSize,
75+
featureCounts.0 * widenFactor, featureCounts.1 * widenFactor
76+
),
77+
strides: initialStride)
78+
]
7179
self.shortcut = Conv2D(
7280
filterShape: (1, 1, featureCounts.0 * widenFactor, featureCounts.1 * widenFactor),
7381
strides: initialStride)
7482
}
7583
for _ in 1..<depthFactor {
76-
self.blocks += [BatchNormConv2DBlock(
77-
filterShape: (kernelSize, kernelSize,
78-
featureCounts.1 * widenFactor, featureCounts.1 * widenFactor),
79-
strides: (1, 1))]
84+
self.blocks += [
85+
BatchNormConv2DBlock(
86+
filterShape: (
87+
kernelSize, kernelSize,
88+
featureCounts.1 * widenFactor, featureCounts.1 * widenFactor
89+
),
90+
strides: (1, 1))
91+
]
8092
}
8193
}
8294

@@ -95,7 +107,7 @@ public struct WideResNet: Layer {
95107
public var l2: WideResNetBasicBlock
96108
public var l3: WideResNetBasicBlock
97109
public var l4: WideResNetBasicBlock
98-
110+
99111
public var norm: BatchNorm<Float>
100112
public var avgPool: AvgPool2D<Float>
101113
public var flatten = Flatten<Float>()
@@ -104,13 +116,16 @@ public struct WideResNet: Layer {
104116
public init(depthFactor: Int = 2, widenFactor: Int = 8) {
105117
self.l1 = Conv2D(filterShape: (3, 3, 3, 16), strides: (1, 1), padding: .same)
106118

107-
l2 = WideResNetBasicBlock(featureCounts: (16, 16), depthFactor: depthFactor,
119+
l2 = WideResNetBasicBlock(
120+
featureCounts: (16, 16), depthFactor: depthFactor,
108121
widenFactor: widenFactor, initialStride: (1, 1))
109-
l3 = WideResNetBasicBlock(featureCounts: (16, 32), depthFactor: depthFactor,
122+
l3 = WideResNetBasicBlock(
123+
featureCounts: (16, 32), depthFactor: depthFactor,
110124
widenFactor: widenFactor)
111-
l4 = WideResNetBasicBlock(featureCounts: (32, 64), depthFactor: depthFactor,
125+
l4 = WideResNetBasicBlock(
126+
featureCounts: (32, 64), depthFactor: depthFactor,
112127
widenFactor: widenFactor)
113-
128+
114129
self.norm = BatchNorm(featureCount: 64 * widenFactor)
115130
self.avgPool = AvgPool2D(poolSize: (8, 8), strides: (8, 8))
116131
self.classifier = Dense(inputSize: 64 * widenFactor, outputSize: 10)
@@ -124,8 +139,8 @@ public struct WideResNet: Layer {
124139
}
125140
}
126141

127-
public extension WideResNet {
128-
enum Kind {
142+
extension WideResNet {
143+
public enum Kind {
129144
case wideResNet16
130145
case wideResNet16k8
131146
case wideResNet16k10
@@ -141,7 +156,7 @@ public extension WideResNet {
141156
case wideResNet40k8
142157
}
143158

144-
init(kind: Kind) {
159+
public init(kind: Kind) {
145160
switch kind {
146161
case .wideResNet16, .wideResNet16k8:
147162
self.init(depthFactor: 2, widenFactor: 8)

0 commit comments

Comments
 (0)