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

Port tests from apple/swift #136

Merged
merged 4 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions Tests/DeepLearningTests/InitializerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2019 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest
@testable import DeepLearning

final class InitializerTests: XCTestCase {
func testInitializers() {
let scalar = Tensor<Float>(1)
let matrix: Tensor<Float> = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
let broadcastScalar = Tensor<Float>(broadcasting: 10, rank: 3)
let some4d = Tensor<Float>(shape: [2, 1, 2, 1], scalars: [2, 3, 4, 5])
XCTAssertEqual(ShapedArray(shape: [2, 1, 2, 1], scalars: [2, 3, 4, 5]), some4d.array)
XCTAssertEqual(ShapedArray(shape: [], scalars: [1]), scalar.array)
XCTAssertEqual(ShapedArray(shape: [2, 3], scalars: [1, 2, 3, 4, 5, 6]), matrix.array)
XCTAssertEqual(ShapedArray(shape: [1, 1, 1], scalars: [10]), broadcastScalar.array)
}

func testFactoryInitializers() {
let x = Tensor<Float>(ones: [1, 10])
XCTAssertEqual(ShapedArray(repeating: 1, shape: [1, 10]), x.array)
}

func testNumericInitializers() {
let x = Tensor<Float>(oneHotAtIndices: [0, 2, -1, 1], depth: 3)
XCTAssertEqual(ShapedArray(
shape: [4, 3],
scalars: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0]), x.array)
}

func testScalarToTensorConversion() {
let tensor = Tensor<Float>(broadcasting: 42, rank: 4)
XCTAssertEqual([1, 1, 1, 1], tensor.shape)
XCTAssertEqual([42], tensor.scalars)
}

func testArrayConversion() {
let array3D = ShapedArray(repeating: 1.0, shape: [2, 3, 4])
let tensor3D = Tensor(array3D)
XCTAssertEqual(array3D, tensor3D.array)
}

func testDataTypeCast() {
let x = Tensor<Int32>(ones: [5, 5])
let ints = Tensor<Int64>(x)
let floats = Tensor<Float>(x)
let u32s = Tensor<UInt32>(floats)
XCTAssertEqual(ShapedArray(repeating: 1, shape: [5, 5]), ints.array)
XCTAssertEqual(ShapedArray(repeating: 1, shape: [5, 5]), floats.array)
XCTAssertEqual(ShapedArray(repeating: 1, shape: [5, 5]), u32s.array)
}

func testBoolToNumericCast() {
let bools = Tensor<Bool>(shape: [2, 2], scalars: [true, false, true, false])
let ints = Tensor<Int64>(bools)
let floats = Tensor<Float>(bools)
let i8s = Tensor<Int8>(bools)
XCTAssertEqual(ShapedArray(shape: [2, 2], scalars: [1, 0, 1, 0]), ints.array)
XCTAssertEqual(ShapedArray(shape: [2, 2], scalars: [1, 0, 1, 0]), floats.array)
XCTAssertEqual(ShapedArray(shape: [2, 2], scalars: [1, 0, 1, 0]), i8s.array)
}

static var allTests = [
("testInitializers", testInitializers),
("testFactoryInitializers", testFactoryInitializers),
("testNumericInitializers", testNumericInitializers),
("testScalarToTensorConversion", testScalarToTensorConversion),
("testArrayConversion", testArrayConversion),
("testDataTypeCast", testDataTypeCast),
("testBoolToNumericCast", testBoolToNumericCast)
]
}
2 changes: 1 addition & 1 deletion Tests/DeepLearningTests/LayerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ final class LayerTests: XCTestCase {
[[ 0.066890605, 0.049586136, 0.024610005, 0.09341654]],
[[ 0.065792546, 0.009325638, 0.06439907, 0.114802904]],
[[ 0.055909205, 0.00035158166, 0.054020774, 0.09812111]]])
let (𝛁rnn, 𝛁inputs) = pullback(.init(inputs.map { SimpleRNNCell<Float>.State($0) }))
let (𝛁rnn, _) = pullback(.init(inputs.map { SimpleRNNCell<Float>.State($0) }))
XCTAssertEqual(𝛁rnn.cell.weight,
[[ 0.0, 0.0, 0.0, 0.0],
[-0.0051169936, 0.0014167001, 0.0074189613, 0.017496519],
Expand Down
Loading