# DomainError with Loss is Inf on data item 1, stopping training:

**URL:** https://discourse.julialang.org/t/domainerror-with-loss-is-inf-on-data-item-1-stopping-training/103343
**Category:** Machine Learning
**Created:** [August 29, 2023, 7:37pm UTC](https://discourse.julialang.org/t/domainerror-with-loss-is-inf-on-data-item-1-stopping-training/103343 "2023-08-29T19:37:55Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![chadagreene](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chadagreene/32/34924_2.png) [@chadagreene](https://discourse.julialang.org/u/chadagreene)
#### Post date: [August 29, 2023, 7:37pm UTC](https://discourse.julialang.org/t/domainerror-with-loss-is-inf-on-data-item-1-stopping-training/103343/1 "2023-08-29T19:37:55Z")

</div>

I’m trying to learn Julia and ML principles simultaneously. I’ve successfully trained a model to fit a simple straight line, following the [procedure described in the intro docs](https://fluxml.ai/Flux.jl/stable/models/overview/), but when I try to go a step further and add a little bit of random noise to the training data, I try to iteratively train the model:

```julia
for epoch in 1:200
   train!(loss, predict, data, opt)
end

```

which immediately throws this error:

```julia
DomainError with Loss is Inf on data item 1, stopping training:

```

After just one iteration, it appears that Flux came up with a bias of -4.8589914f16, which is a very big number, and my best interpretation is that the second iteration sends the bias and weight values to infinity.

How should I troubleshoot this error?

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [August 30, 2023, 11:22am UTC](https://discourse.julialang.org/t/domainerror-with-loss-is-inf-on-data-item-1-stopping-training/103343/2 "2023-08-30T11:22:19Z")

</div>

A full code example would be helpful. What kind of noise do you add?

When I add some Gaussian noise to the training data by modifying  
`actual(x) = 4x + 2 + randn()`  
it still trains well.

Take a look at the loss and gradients during training by writing the `train!` method essentially by hand, e.g.

```julia
predict = Dense(1 => 1)

opt = Flux.setup(Descent(), predict)
for epoch in 1:200
   l,gs = Flux.withgradient(m->loss(m, x_train, y_train), predict)
   @show l, gs

   Flux.Optimise.update!(opt, predict, gs[1])
end

```

```julia
(l, gs) = (7.3990827f-9, ((weight = Float32[-0.00052022934;;], bias = Float32[-0.00014666717], σ = nothing),))
(l, gs) = (7.128392f-9, ((weight = Float32[0.0005106926;;], bias = Float32[0.00014368694], σ = nothing),))
(l, gs) = (6.862917f-9, ((weight = Float32[-0.0005009969;;], bias = Float32[-0.00014134249], σ = nothing),))
[...]

```
