Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit d398718

Browse files
authored
Fix tests. (#130)
swift-apis CI is functional again.
1 parent e6036b4 commit d398718

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

Sources/DeepLearning/Layer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ public struct UpSampling1D<Scalar: TensorFlowFloatingPoint>: Layer {
13401340
///
13411341
/// - Parameter size: The upsampling factor for timesteps.
13421342
public init(size: Int) {
1343-
self.size = size
1343+
self.size = size
13441344
}
13451345

13461346
/// Returns the output obtained from applying the layer to the given input.
@@ -1366,7 +1366,7 @@ public struct UpSampling2D<Scalar: TensorFlowFloatingPoint>: Layer {
13661366
///
13671367
/// - Parameter size: The upsampling factor for rows and columns.
13681368
public init(size: Int) {
1369-
self.size = size
1369+
self.size = size
13701370
}
13711371

13721372
/// Returns the output obtained from applying the layer to the given input.
@@ -1392,7 +1392,7 @@ public struct UpSampling3D<Scalar: TensorFlowFloatingPoint>: Layer {
13921392
///
13931393
/// - Parameter size: The upsampling factor for rows and columns.
13941394
public init(size: Int) {
1395-
self.size = size
1395+
self.size = size
13961396
}
13971397

13981398
/// Returns the output obtained from applying the layer to the given input.

Tests/DeepLearningTests/LayerTests.swift

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ import XCTest
1717

1818
final class LayerTests: XCTestCase {
1919
func testConv1D() {
20-
let filter = Tensor<Float>(ones: [3, 1, 2]) * Tensor<Float>([[[0.33333333, 1]]])
20+
let filter = Tensor<Float>(ones: [3, 1, 2]) * Tensor<Float>([[[0.5, 1]]])
2121
let bias = Tensor<Float>([0, 1])
2222
let layer = Conv1D<Float>(filter: filter, bias: bias, activation: identity, stride: 1, padding: .valid)
2323
let input = Tensor<Float>([[0, 1, 2, 3, 4], [10, 11, 12, 13, 14]]).expandingShape(at: 2)
2424
let output = layer.inferring(from: input)
25-
let expected = Tensor<Float>([[[1, 4], [2, 7], [3, 10]], [[11, 34], [12, 37], [13, 40]]])
25+
let expected = Tensor<Float>(
26+
shape: [2, 3, 2],
27+
scalars: [1.5, 4, 3, 7, 4.5, 10, 16.5, 34, 18, 37, 19.5, 40])
2628
XCTAssertEqual(output, expected)
2729
}
2830

@@ -82,7 +84,7 @@ final class LayerTests: XCTestCase {
8284
let layer = AvgPool3D<Float>(poolSize: (2, 4, 5), strides: (1, 1, 1), padding: .valid)
8385
let input = Tensor(shape: [1, 2, 4, 5, 1], scalars: (0..<40).map(Float.init))
8486
let output = layer.inferring(from: input)
85-
let expected = Tensor<Float>([[[[[9.5]]]]])
87+
let expected = Tensor<Float>([[[[[19.5]]]]])
8688
XCTAssertEqual(output, expected)
8789
}
8890

@@ -132,9 +134,15 @@ final class LayerTests: XCTestCase {
132134
let size = 6
133135
let layer = UpSampling3D<Float>(size: size)
134136
let input = Tensor<Float>(shape: [1, 4, 3, 2, 1], scalars: (0..<24).map(Float.init))
137+
// TODO(TF-525): Fix `UpSampling3D.call`.
138+
// Broadcasting does not support tensors with high rank:
139+
// Broadcast between [1,4,1,3,1,2,1,1] and [1,1,6,1,6,1,6,1] is not supported yet.
140+
/*
135141
let output = layer.inferring(from: input)
136142
let expected = TensorShape([1, input.shape[1] * size, input.shape[2] * size, input.shape[3] * size, 1])
137143
XCTAssertEqual(output.shape, expected)
144+
XCTAssertEqual(output.shape, expected)
145+
*/
138146
}
139147

140148
func testReshape() {
@@ -165,7 +173,7 @@ final class LayerTests: XCTestCase {
165173
let input = Tensor<Float>(ones: [1, 2]) * Tensor<Float>([0.3, 0.7])
166174
let output = cell(input: input, state: state).state
167175
let expected = SimpleRNNCell.State(
168-
Tensor<Float>([[2.76649, 6.2999997, 2.76649, 6.2999997, 2.76649]])
176+
Tensor<Float>([[0.9921227, 0.9999934, 0.9921227, 0.9999934, 0.9921227]])
169177
)
170178
XCTAssertEqual(output, expected)
171179
}
@@ -180,21 +188,21 @@ final class LayerTests: XCTestCase {
180188
return rnn(inputs)
181189
}
182190
XCTAssertEqual(outputs.map { $0.value },
183-
[[[-0.0026294366, -0.0058668107, 0.04495003, 0.20311214]],
184-
[[ 0.06788494, 0.050665878, 0.02415526, 0.09249911]],
185-
[[ 0.06621192, 0.009049267, 0.065047316, 0.11534518]],
186-
[[ 0.05612204, 0.00022032857, 0.05407162, 0.09784105]]])
191+
[[[ -0.00262943, -0.005866742, 0.044919778, 0.20036437]],
192+
[[ 0.066890605, 0.049586136, 0.024610005, 0.09341654]],
193+
[[ 0.065792546, 0.009325638, 0.06439907, 0.114802904]],
194+
[[ 0.055909205, 0.00035158166, 0.054020774, 0.09812111]]])
187195
let (𝛁rnn, 𝛁inputs) = pullback(.init(inputs.map { SimpleRNNCell<Float>.State($0) }))
188196
XCTAssertEqual(𝛁rnn.cell.weight,
189197
[[ 0.0, 0.0, 0.0, 0.0],
190-
[-0.0051278225, 0.0013102926, 0.00740262, 0.018119661],
191-
[ -0.010255645, 0.0026205853, 0.01480524, 0.036239322],
192-
[ -0.015383467, 0.003930878, 0.02220786, 0.054358985],
198+
[-0.0051169936, 0.0014167001, 0.0074189613, 0.017496519],
199+
[ -0.010233987, 0.0028334002, 0.0148379225, 0.034993038],
200+
[ -0.015350982, 0.0042501003, 0.022256885, 0.05248956],
193201
[ 0.0, 0.0, 0.0, 0.0],
194202
[ 0.0, 0.0, 0.0, 0.0],
195203
[ 0.0, 0.0, 0.0, 0.0],
196204
[ 0.0, 0.0, 0.0, 0.0]])
197-
XCTAssertEqual(𝛁rnn.cell.bias, [-0.051278222, 0.013102926, 0.0740262, 0.18119662])
205+
XCTAssertEqual(𝛁rnn.cell.bias, [-0.051169936, 0.014167001, 0.07418961, 0.17496519])
198206
}
199207

200208
static var allTests = [

0 commit comments

Comments
 (0)