-
-
Save tanmayb123/366bc39caa18d71ed7ea0bd1d07e0a79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import TensorFlow | |
struct SinModel: Layer { | |
var rnn: RNN<SimpleRNNCell<Float>> = RNN(SimpleRNNCell<Float>(inputSize: 1, hiddenSize: 2)) | |
var reg: Dense<Float> = Dense<Float>(inputSize: 2, outputSize: 1, activation: tanh) | |
init() {} | |
struct Input: Differentiable { | |
var input: [Tensor<Float>] | |
@noDerivative var timesteps: Int | |
@differentiable | |
init(_ input: [Tensor<Float>], _ timesteps: Int) { | |
self.input = input | |
self.timesteps = timesteps | |
} | |
} | |
typealias Output = Tensor<Float> | |
@differentiable | |
func call(_ input: Input) -> Tensor<Float> { | |
return reg.call(rnn.call(input.input)[input.timesteps]) | |
} | |
} | |
func generateData() -> ([SinModel.Input], [Tensor<Float>]) { | |
var inputs: [SinModel.Input] = [] | |
var outputs: [Tensor<Float>] = [] | |
for i in 0...1000 { | |
let x = Float(i) | |
inputs.append(SinModel.Input([Float]([sin(x / 1000.0), sin((x + 1.0) / 1000.0)]).map{ Tensor<Float>([$0]).reshaped(to: [1, 1]) }, 1)) | |
outputs.append(Tensor<Float>([Float]([sin((x + 2.0) / 1000.0)]).map{ Tensor<Float>([$0]).reshaped(to: [1, 1]) })) | |
} | |
return (inputs, outputs) | |
} | |
var model = SinModel() | |
var optimizer = Adam<SinModel>(for: model) | |
let data = generateData() | |
for (input, output) in zip(data.0, data.1) { | |
let (loss, grads) = model.valueWithGradient { model -> Tensor<Float> in | |
return meanSquaredError(predicted: model.call(input), expected: output) | |
} | |
optimizer.update(&model.allDifferentiableVariables, along: grads) | |
print(loss) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error: