I'm trying to recreate this PyTorch RNN tutorial https://www.cpuheater.com/deep-…learning/introduction-to-recurrent-neural-networks-in-pytorch/ (initially without any of the more advanced RNN features in Flux just to get an idea of translation issues from PyTorch). When I try to run it, I get:
> ERROR: LoadError: MethodError: Cannot `convert` an object of type Flux.Tracker.TrackedReal{Float64} to an object of type Float64
> This may have arisen from a call to the constructor Float64(...),
> since type constructors fall back to convert methods.
> Stacktrace:
> [1] setindex! at ./multidimensional.jl:300 [inlined]
> [2] macro expansion at ./broadcast.jl:156 [inlined]
> [3] macro expansion at ./simdloop.jl:73 [inlined]
> [4] macro expansion at ./broadcast.jl:149 [inlined]
> [5] _broadcast! at ./broadcast.jl:141 [inlined]
> [6] broadcast_c! at ./broadcast.jl:213 [inlined]
> [7] broadcast! at ./broadcast.jl:206 [inlined]
> [8] accum!(::Array{Float64,2}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:29
> [9] back(::Flux.Tracker.Tracked{Array{Float64,2}}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:32
> [10] macro expansion at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:49 [inlined]
> [11] back(::Base.#*, ::Array{Flux.Tracker.TrackedReal{Float64},2}, ::Array{Flux.Tracker.TrackedReal{Float64},2}, ::TrackedArray{…,Array{Float64,2}}) at /home/phil/.julia/v0.6/Flux/src/tracker/array.jl:280
> [12] back_(::Flux.Tracker.Call{Base.#*,Tuple{Array{Flux.Tracker.TrackedReal{Float64},2},TrackedArray{…,Array{Float64,2}}}}, ::Array{Flux.Tracker.TrackedReal{Float64},2}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:25
> [13] back(::Flux.Tracker.Tracked{Array{Flux.Tracker.TrackedReal{Float64},2}}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:38
> [14] macro expansion at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:49 [inlined]
> [15] (::Flux.Tracker.##56#58)(::TrackedArray{…,Array{Flux.Tracker.TrackedReal{Float64},2}}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/array.jl:385
> [16] foreach(::Function, ::Tuple{TrackedArray{…,Array{Flux.Tracker.TrackedReal{Float64},2}}}, ::Tuple{Array{Flux.Tracker.TrackedReal{Float64},2}}, ::Vararg{Tuple{Array{Flux.Tracker.TrackedReal{Float64},2}},N} where N) at ./abstractarray.jl:1734
> [17] back_(::Flux.Tracker.Call{Flux.Tracker.Broadcasted{Base.#tanh,Array{ForwardDiff.Dual{Void,Flux.Tracker.TrackedReal{Float64},1},2}},Tuple{TrackedArray{…,Array{Flux.Tracker.TrackedReal{Float64},2}}}}, ::Array{Flux.Tracker.TrackedReal{Float64},2}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:25
> [18] back(::Flux.Tracker.Tracked{Array{Flux.Tracker.TrackedReal{Float64},2}}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:38
> [19] macro expansion at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:49 [inlined]
> [20] back(::Base.#*, ::Array{Flux.Tracker.TrackedReal{Float64},2}, ::TrackedArray{…,Array{Flux.Tracker.TrackedReal{Float64},2}}, ::TrackedArray{…,Array{Float64,2}}) at /home/phil/.julia/v0.6/Flux/src/tracker/array.jl:279
> [21] back_(::Flux.Tracker.Call{Base.#*,Tuple{TrackedArray{…,Array{Flux.Tracker.TrackedReal{Float64},2}},TrackedArray{…,Array{Float64,2}}}}, ::Array{Flux.Tracker.TrackedReal{Float64},2}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:25
> [22] back(::Flux.Tracker.Tracked{Array{Flux.Tracker.TrackedReal{Float64},2}}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:38
> [23] macro expansion at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:49 [inlined]
> [24] (::Flux.Tracker.##56#58)(::TrackedArray{…,Array{Flux.Tracker.TrackedReal{Float64},2}}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/array.jl:385
> [25] foreach(::Function, ::Tuple{TrackedArray{…,Array{Flux.Tracker.TrackedReal{Float64},2}},Float64}, ::Tuple{Array{Flux.Tracker.TrackedReal{Float64},2},Array{Flux.Tracker.TrackedReal{Float64},2}}, ::Vararg{Tuple{Array{Flux.Tracker.TrackedReal{Float64},2},Array{Flux.Tracker.TrackedReal{Float64},2}},N} where N) at ./abstractarray.jl:1734
> [26] back_(::Flux.Tracker.Call{Flux.Tracker.Broadcasted{##1#4,Array{ForwardDiff.Dual{Void,Flux.Tracker.TrackedReal{Float64},2},2}},Tuple{TrackedArray{…,Array{Flux.Tracker.TrackedReal{Float64},2}},Float64}}, ::Array{Flux.Tracker.TrackedReal{Float64},2}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:25
> [27] back(::Flux.Tracker.Tracked{Array{Flux.Tracker.TrackedReal{Float64},2}}, ::Array{Flux.Tracker.TrackedReal{Float64},2}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:38
> [28] back(::Base.#sum, ::Flux.Tracker.TrackedReal{Float64}, ::TrackedArray{…,Array{Flux.Tracker.TrackedReal{Float64},2}}) at /home/phil/.julia/v0.6/Flux/src/tracker/array.jl:190
> [29] back_(::Flux.Tracker.Call{Base.#sum,Tuple{TrackedArray{…,Array{Flux.Tracker.TrackedReal{Float64},2}}}}, ::Flux.Tracker.TrackedReal{Float64}, ::Flux.Tracker.TrackedReal{Float64}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:25
> [30] back(::Flux.Tracker.Tracked{Flux.Tracker.TrackedReal{Float64}}, ::Flux.Tracker.TrackedReal{Float64}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:36
> [31] macro expansion at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:49 [inlined]
> [32] back(::Base.#/, ::Flux.Tracker.TrackedReal{Float64}, ::Flux.Tracker.TrackedReal{Flux.Tracker.TrackedReal{Float64}}, ::Int64) at /home/phil/.julia/v0.6/Flux/src/tracker/scalar.jl:58
> [33] back_(::Flux.Tracker.Call{Base.#/,Tuple{Flux.Tracker.TrackedReal{Flux.Tracker.TrackedReal{Float64}},Int64}}, ::Flux.Tracker.TrackedReal{Float64}, ::Flux.Tracker.TrackedReal{Float64}) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:25
> [34] back(::Flux.Tracker.Tracked{Flux.Tracker.TrackedReal{Float64}}, ::Int64) at /home/phil/.julia/v0.6/Flux/src/tracker/back.jl:36
> [35] back!(::Flux.Tracker.TrackedReal{Flux.Tracker.TrackedReal{Float64}}) at /home/phil/.julia/v0.6/Flux/src/tracker/scalar.jl:11
> [36] train() at /home/phil/devel/ML/PyTorch/pytorch_examples/elman_rnn/Elman_RNN.jl:46
> [37] include_from_node1(::String) at ./loading.jl:576
> [38] include(::String) at ./sysimg.jl:14
> while loading /home/phil/devel/ML/PyTorch/pytorch_examples/elman_rnn/Elman_RNN.jl, in expression starting on line 59
My Code:
```
using Flux
using Flux.Tracker
input_size, hidden_size, output_size = 7, 6, 1
epochs = 200
seq_length = 20
lr = 0.1
data_time_steps = linspace(2,10, seq_length+1)
data = sin.(data_time_steps)
x = data[1:end-1]
y = data[2:end]
w1 = param(randn(input_size, hidden_size))
w2 = param(randn(hidden_size, output_size))
function forward(input, context_state, W1, W2)
xh = cat(2,input, context_state)
context_state = tanh.(xh*W1)
out = context_state*W2
return out, context_state
end
#context_state = zeros(hidden_size)
predictions = []
function train()
for i in 1:epochs
print("Epoch $i\n")
total_loss = 0
context_state = zeros(1,hidden_size)
for j in 1:length(x)
print(" j = $j\n")
input = x[j]
print("input (x[$j]) is: $input\n")
target= y[j]
print("target (y[$j]) is: $target\n")
pred, context_state = forward(input, context_state, w1, w2)
print("pred: $pred\n")
print("target: $target\n")
flush(STDOUT)
loss = sum((pred .- target).^2)/2
print("loss is: $loss\n")
total_loss += loss
back!(loss)
w1.data .-= lr.*w1.grad
w2.data .-= lr.*w2.grad
w1.grad .= 0.0
w2.grad .= 0.0
end
if(i % 10 == 0)
println("Epoch: $i loss: $total_loss")
end
end
end
train()
```