Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // RUN: %target-swift-frontend -Xllvm -tf-dump-intermediates -O -emit-sil -verify %s | %FileCheck %s
- import TensorFlow
- // This test is intended to verify that all of the operations end up in the
- // graph: that there are no host/accelerator copies generated. This tests a
- // combination of the partitioning pass being able to recognize various forms,
- // but also checks that certain ops implementations are promotable as well.
- // Please keep it so no errors or warnings are generated by functions in this
- // file.
- /// b/76222306
- struct Classifier {
- // Parameters
- var w1 = Tensor<Float>(randomUniform: [784, 30])
- var w2 = Tensor<Float>(randomUniform: [30, 10])
- var b1 = Tensor<Float>(zeros: [1, 30])
- var b2 = Tensor<Float>(zeros: [1, 10])
- mutating func train(images: Tensor<Float>, labels: Tensor<Float>,
- learningRate: Float, epochCount: Int) -> Float {
- var loss: Float
- var epochCount = epochCount
- repeat {
- // Forward pass
- let z1 = images • w1 + b1
- // Code below from original test is commented for minimal reproducer.
- /*
- let h1 = sigmoid(z1)
- let z2 = h1 • w2 + b2
- let pred = sigmoid(z2)
- // Backward pass
- let dz2 = pred - labels
- let dw2 = h1.transposed(withPermutations: 1, 0) • dz2
- let db2 = dz2.sum(squeezingAxes: 0)
- let dz1 = matmul(dz2, w2.transposed(withPermutations: 1, 0)) * h1 * (1 - h1)
- let dw1 = images.transposed(withPermutations: 1, 0) • dz1
- let db1 = dz1.sum(squeezingAxes: 0)
- // Gradient descent
- w1 -= dw1 * learningRate
- b1 -= db1 * learningRate
- w2 -= dw2 * learningRate
- b2 -= db2 * learningRate
- loss = dz2.squared().mean(squeezingAxes: 1, 0).scalarized()
- */
- loss = 1
- epochCount -= 1
- } while epochCount > 0
- return loss
- }
- }
- public func mnist() {
- // Training data
- // expected-warning @+1 {{'Tensor<Float>' implicitly copied to the accelerator, use .toAccelerator}}
- let images = Tensor<Float>(randomNormal: [10, 784])
- let labels = Tensor<Float>(randomNormal: [10, 10])
- var classifier = Classifier()
- let loss = classifier.train(images: images, labels: labels,
- learningRate: 0.3, epochCount: 100)
- print(loss)
- }
Add Comment
Please, Sign In to add comment