# Problem with Training and trying to plot loss function in Flux

**URL:** https://discourse.julialang.org/t/problem-with-training-and-trying-to-plot-loss-function-in-flux/85826
**Category:** New to Julia
**Created:** [August 16, 2022, 1:49pm UTC](https://discourse.julialang.org/t/problem-with-training-and-trying-to-plot-loss-function-in-flux/85826 "2022-08-16T13:49:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)
#### Post date: [August 16, 2022, 1:49pm UTC](https://discourse.julialang.org/t/problem-with-training-and-trying-to-plot-loss-function-in-flux/85826/1 "2022-08-16T13:49:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [August 16, 2022, 2:53pm UTC](https://discourse.julialang.org/t/problem-with-training-and-trying-to-plot-loss-function-in-flux/85826/2 "2022-08-16T14:53:37Z")

</div>

> [@ak47](#):
>
> push!(loss\_on\_test, loss(x\_test, y\_test).data)  
> push!(loss\_on\_train, loss(x\_train, y\_train).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).

---

<div class="post-metadata">

### Author: ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)
#### Post date: [August 17, 2022, 4:57am UTC](https://discourse.julialang.org/t/problem-with-training-and-trying-to-plot-loss-function-in-flux/85826/3 "2022-08-17T04:57:31Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)
#### Post date: [August 17, 2022, 5:47am UTC](https://discourse.julialang.org/t/problem-with-training-and-trying-to-plot-loss-function-in-flux/85826/4 "2022-08-17T05:47:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [August 17, 2022, 10:31am UTC](https://discourse.julialang.org/t/problem-with-training-and-trying-to-plot-loss-function-in-flux/85826/5 "2022-08-17T10:31:00Z")

</div>

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

```julia
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?

---

<div class="post-metadata">

### Author: ![ak47](https://avatars.discourse-cdn.com/v4/letter/a/0ea827/32.png) [@ak47](https://discourse.julialang.org/u/ak47)
#### Post date: [August 17, 2022, 4:13pm UTC](https://discourse.julialang.org/t/problem-with-training-and-trying-to-plot-loss-function-in-flux/85826/6 "2022-08-17T16:13:22Z")

</div>

Thanks for your time.
