|
| 1 | +// Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import XCTest |
| 16 | +@testable import DeepLearning |
| 17 | + |
| 18 | +final class InitializerTests: XCTestCase { |
| 19 | + func testInitializers() { |
| 20 | + let scalar = Tensor<Float>(1) |
| 21 | + let matrix: Tensor<Float> = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]] |
| 22 | + let broadcastScalar = Tensor<Float>(broadcasting: 10, rank: 3) |
| 23 | + let some4d = Tensor<Float>(shape: [2, 1, 2, 1], scalars: [2, 3, 4, 5]) |
| 24 | + XCTAssertEqual(ShapedArray(shape: [2, 1, 2, 1], scalars: [2, 3, 4, 5]), some4d.array) |
| 25 | + XCTAssertEqual(ShapedArray(shape: [], scalars: [1]), scalar.array) |
| 26 | + XCTAssertEqual(ShapedArray(shape: [2, 3], scalars: [1, 2, 3, 4, 5, 6]), matrix.array) |
| 27 | + XCTAssertEqual(ShapedArray(shape: [1, 1, 1], scalars: [10]), broadcastScalar.array) |
| 28 | + } |
| 29 | + |
| 30 | + func testFactoryInitializers() { |
| 31 | + let x = Tensor<Float>(ones: [1, 10]) |
| 32 | + XCTAssertEqual(ShapedArray(repeating: 1, shape: [1, 10]), x.array) |
| 33 | + } |
| 34 | + |
| 35 | + func testNumericInitializers() { |
| 36 | + let x = Tensor<Float>(oneHotAtIndices: [0, 2, -1, 1], depth: 3) |
| 37 | + XCTAssertEqual(ShapedArray( |
| 38 | + shape: [4, 3], |
| 39 | + scalars: [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0]), x.array) |
| 40 | + } |
| 41 | + |
| 42 | + func testScalarToTensorConversion() { |
| 43 | + let tensor = Tensor<Float>(broadcasting: 42, rank: 4) |
| 44 | + XCTAssertEqual([1, 1, 1, 1], tensor.shape) |
| 45 | + XCTAssertEqual([42], tensor.scalars) |
| 46 | + } |
| 47 | + |
| 48 | + func testArrayConversion() { |
| 49 | + let array3D = ShapedArray(repeating: 1.0, shape: [2, 3, 4]) |
| 50 | + let tensor3D = Tensor(array3D) |
| 51 | + XCTAssertEqual(array3D, tensor3D.array) |
| 52 | + } |
| 53 | + |
| 54 | + func testDataTypeCast() { |
| 55 | + let x = Tensor<Int32>(ones: [5, 5]) |
| 56 | + let ints = Tensor<Int64>(x) |
| 57 | + let floats = Tensor<Float>(x) |
| 58 | + let u32s = Tensor<UInt32>(floats) |
| 59 | + XCTAssertEqual(ShapedArray(repeating: 1, shape: [5, 5]), ints.array) |
| 60 | + XCTAssertEqual(ShapedArray(repeating: 1, shape: [5, 5]), floats.array) |
| 61 | + XCTAssertEqual(ShapedArray(repeating: 1, shape: [5, 5]), u32s.array) |
| 62 | + } |
| 63 | + |
| 64 | + func testBoolToNumericCast() { |
| 65 | + let bools = Tensor<Bool>(shape: [2, 2], scalars: [true, false, true, false]) |
| 66 | + let ints = Tensor<Int64>(bools) |
| 67 | + let floats = Tensor<Float>(bools) |
| 68 | + let i8s = Tensor<Int8>(bools) |
| 69 | + XCTAssertEqual(ShapedArray(shape: [2, 2], scalars: [1, 0, 1, 0]), ints.array) |
| 70 | + XCTAssertEqual(ShapedArray(shape: [2, 2], scalars: [1, 0, 1, 0]), floats.array) |
| 71 | + XCTAssertEqual(ShapedArray(shape: [2, 2], scalars: [1, 0, 1, 0]), i8s.array) |
| 72 | + } |
| 73 | + |
| 74 | + static var allTests = [ |
| 75 | + ("testInitializers", testInitializers), |
| 76 | + ("testFactoryInitializers", testFactoryInitializers), |
| 77 | + ("testNumericInitializers", testNumericInitializers), |
| 78 | + ("testScalarToTensorConversion", testScalarToTensorConversion), |
| 79 | + ("testArrayConversion", testArrayConversion), |
| 80 | + ("testDataTypeCast", testDataTypeCast), |
| 81 | + ("testBoolToNumericCast", testBoolToNumericCast) |
| 82 | + ] |
| 83 | +} |
0 commit comments