Skip to content

Instantly share code, notes, and snippets.

@tanmayb123
Created April 24, 2019 00:46
Show Gist options
  • Save tanmayb123/366bc39caa18d71ed7ea0bd1d07e0a79 to your computer and use it in GitHub Desktop.
Save tanmayb123/366bc39caa18d71ed7ea0bd1d07e0a79 to your computer and use it in GitHub Desktop.
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)
}
@tanmayb123
Copy link
Author

Error:

main.swift:41:17: error: 'Adam' requires the types 'SinModel.AllDifferentiableVariables' and 'SinModel.CotangentVector' be equivalent
var optimizer = Adam<SinModel>(for: model)
                ^
Optimizer.swift:48:14: note: requirement specified as 'Model.AllDifferentiableVariables' == 'Model.CotangentVector' [with Model = SinModel]
public class Adam<Model: Layer>: Optimizer
             ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment