# Logistic regression in flux

**URL:** <https://discourse.julialang.org/t/logistic-regression-in-flux/20687>\
**Category:** New to Julia\
**Tags:** flux\
**Created:** [February 12, 2019, 12:44am UTC](https://discourse.julialang.org/t/logistic-regression-in-flux/20687 "2019-02-12T00:44:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![murphyk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/murphyk/32/6891_2.png) [@murphyk](https://discourse.julialang.org/u/murphyk)\
**Post date:** [February 12, 2019, 12:44am UTC](https://discourse.julialang.org/t/logistic-regression-in-flux/20687/1 "2019-02-12T00:44:42Z")

</div>

I’m trying to implement binary logistic regression in flux. I’m simplifying [the mlp.jl example from the model zoo.](https://github.com/FluxML/model-zoo/blob/master/vision/mnist/mlp.jl). I have tried 4 versions, only 1 works, please explain why.

V1.

```julia
D = 2; N = 20;
X = randn(D, N)
y = rand([0,1], N)
D, N = size(X)
data = repeated((X,Y),1)

### changeable part
model = Dense(D, 1, sigmoid)
loss(x, y) = Flux.binarycrossentropy(model(x), y)
Y = y
###

callback() = @show(loss(X, Y))
opt = Flux.Optimise.ADAM()
Flux.train!(loss, params(model), data, opt, cb = callback)

```

Fails:

```julia
LoadError: no method matching eps(::TrackedArray{…,Array{Float32,2}})

```

V2: same as above but

```julia
model = Chain(Dense(D, 1), sigmoid) 

```

Fails:

```julia
DimensionMismatch("multiplicative identity defined only for square matrices")

```

V3: same as above but

```julia
model = Dense(D, 2, softmax) 
loss(x, y) = crossentropy(model(x), y)
Y = onehotbatch(y, 0:1)

```

Fails:

```julia
  MethodError: no method matching similar(::Float32)

```

V4: success!

```julia
model = Chain(Dense(D, 2), softmax)
loss(x, y) = crossentropy(model(x), y)
Y = onehotbatch(y, 0:1)

```

I understand why V4 works, but why not V1-V3?

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [April 8, 2020, 4:50am UTC](https://discourse.julialang.org/t/logistic-regression-in-flux/20687/2 "2020-04-08T04:50:45Z")

</div>

@murphyk the following works on my computer:

```nohighlight
using Flux
D = 2; N = 20;
X = randn(D, N)
y = rand([0,1], N)
D, N = size(X)
data = Iterators.repeated((X,y),1)

```

V1: Change `Flux.binarycrossentropy` to `Flux.crossentropy`

```nohighlight
model = Dense(D, 1, sigmoid)
loss(x, y) = Flux.crossentropy(model(x), y)
Y = y

callback() = @show(loss(X, Y))
opt = Flux.Optimise.ADAM()
Flux.train!(loss, params(model), data, opt, cb = callback)

```

V2: put `sigmoid` inside Dense()

```nohighlight
model = Chain(Dense(D, 1, sigmoid) ) 
Flux.train!(loss, params(model), data, opt, cb = callback)

```
