Problem with Training and trying to plot loss function in Flux

Been testing some basic code and trying to plot its loss function but get some errors. Need your help to move on. Here is the code:

using Flux
actual(x) = 2x + 3
x_train, x_test = hcat(0:5…), hcat(6:10…)
y_train, y_test = actual.(x_train), actual.(x_test)

model = Dense(1, 1)
predict = model
loss(x, y) = Flux.Losses.mse(predict(x), y)
opt = Descent()
data = [(x_train, y_train)]
parameters = Flux.params(predict)
Flux.train!(loss, parameters, data, opt)’

I get error on this last block of code

epochs = Int64
loss_on_train = Float32
loss_on_test = Float32

for epoch in 1:2000
Flux.train!(loss, parameters,data, opt)
# we record our training loss
push!(epochs, epoch)
push!(loss_on_test, loss(x_test, y_test).data)
push!(loss_on_train, loss(x_train, y_train).data)
end

using Plots
plot(epochs, loss_on_train, lab=“Training”, c=:black, lw=2);
plot!(epochs, loss_on_test, lab=“Testing”, c=:teal, ls=:dot);
yaxis!(“Loss”, :log);
xaxis!(“Training epoch”)

Here is the error:
type Float32 has no field data

Hi @ak47! I’m willing to bet the error comes from these two lines: you don’t need to take the .data field of the loss, since it is already a number (Float32 in this case).

Thank you Gdalle. The error goes away but the plot still does not work…

Push records only the final loss and that seems the reason why it doesn’t plot. How do I modify this ?

I’m not sure what’s wrong, it works with the following code for me (which is the same as yours):

using Flux, Plots

actual(x) = 2x + 3
x_train, x_test = hcat(0:5...), hcat(6:10...)
y_train, y_test = actual.(x_train), actual.(x_test)

model = Dense(1, 1)
predict = model
loss(x, y) = Flux.Losses.mse(predict(x), y)
opt = Descent()
data = [(x_train, y_train)]
parameters = Flux.params(predict)
Flux.train!(loss, parameters, data, opt)

epochs = Int64[]
loss_on_train = Float32[]
loss_on_test = Float32[]

for epoch in 1:2000
    Flux.train!(loss, parameters,data, opt)
    # we record our training loss
    push!(epochs, epoch)
    push!(loss_on_test, loss(x_test, y_test))
    push!(loss_on_train, loss(x_train, y_train))
end

plot(epochs, loss_on_train, lab="Training", c=:black, lw=2);
plot!(epochs, loss_on_test, lab="Testing", c=:teal, ls=:dot);
yaxis!("Loss", :log);
xaxis!("Training epoch")

To make life easier for other people, it’s best to use backticks on Discourse when displaying code (like I just did). Maybe the problem you had came from a copy-pasting issue?

Thanks for your time.