# Reproducing TF model with Flux- Slow?

**URL:** https://discourse.julialang.org/t/reproducing-tf-model-with-flux-slow/100566
**Category:** Machine Learning
**Created:** [June 19, 2023, 4:44pm UTC](https://discourse.julialang.org/t/reproducing-tf-model-with-flux-slow/100566 "2023-06-19T16:44:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![NAS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nas/32/47432_2.png) [@NAS](https://discourse.julialang.org/u/NAS)
#### Post date: [June 19, 2023, 4:44pm UTC](https://discourse.julialang.org/t/reproducing-tf-model-with-flux-slow/100566/1 "2023-06-19T16:44:37Z")

</div>

Hello everyone. I’m new to Flux and as an example to learn I’m just trying to reproduce an example TF model. So far it seems to be working but I have one major snag, the Flux training is orders of magnitude slower. For example, the TF training epochs are about 30s each and the Flux training epochs are about 13 minutes each.

Here is what I am trying to reproduce:

```python
INPUT_SHAPE = [train_df.shape[1]] ## 1024
BATCH_SIZE = 5120

model = tf.keras.Sequential([
    tf.keras.layers.BatchNormalization(input_shape=INPUT_SHAPE),    
    tf.keras.layers.Dense(units=512, activation='relu'),
    tf.keras.layers.Dense(units=512, activation='relu'),
    tf.keras.layers.Dense(units=512, activation='relu'),
    tf.keras.layers.Dense(units=num_of_labels,activation='sigmoid') #num_of_labels = 1500
])

# Compile model
model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
    loss='binary_crossentropy',
    metrics=['binary_accuracy', tf.keras.metrics.AUC()],
)

history = model.fit(
    train_df, labels_df,
    batch_size=BATCH_SIZE,
    epochs=5
)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/f/dfd055788d5899b2ac297e7988352a94dec88319.png)

My “translation” is :

```julia
INPUT_SHAPE = size(train_df)[2] #1024
BATCH_SIZE = 5120

model = Chain(
    BatchNorm(INPUT_SHAPE),
    Dense(1024=>512,relu),
    Dense(512=>512,relu),
    Dense(512=>512,relu),
    Dense(512=>1500,sigmoid)
) 

obs = Matrix(train_df) |> permutedims
labels = Matrix(labels_df) |> permutedims

loader = Flux.DataLoader((data = obs,label = labels) ,batchsize = BATCH_SIZE)

optim = Flux.setup(Flux.Adam(0.001, (0.9, 0.999), 1.0e-7), model)

for epoch in 1:5
    println("epoch: $epoch")
    @showprogress for(data,label) in loader
        grads = Flux.gradient(model) do m
            result = m(data)
            Flux.Losses.binarycrossentropy(result,label)
        end
        Flux.update!(optim,model,grads[1])
    end
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/e/4ef93c3ee7ce9b2868766e49bf460e9b8cc79bd1.png)

As I mentioned, the TF training epochs run about 30s each and the Flux for ~13 minutes.

I feel like I have to be missing something simple. Any feedback you may have would be greatly appreciated.

Thanks

---

<div class="post-metadata">

### Author: ![NAS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nas/32/47432_2.png) [@NAS](https://discourse.julialang.org/u/NAS)
#### Post date: [June 24, 2023, 2:47pm UTC](https://discourse.julialang.org/t/reproducing-tf-model-with-flux-slow/100566/2 "2023-06-24T14:47:31Z")

</div>

Solved-

Turns out this:

```julia
labels = Matrix(labels_df) |> permutedims

```

Was a `Matrix{Any}` instead of `Matrix{Float32}` which caused the slowdown.

Flux documentation even says:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/7/47a0e1a3831c6d8ecf1246585b9975056d728c4d.png)

After correcting, training speed is on par with TF version

---

<div class="post-metadata">

### Author: ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)
#### Post date: [June 24, 2023, 3:42pm UTC](https://discourse.julialang.org/t/reproducing-tf-model-with-flux-slow/100566/3 "2023-06-24T15:42:39Z")

</div>

nice finding! you can mark the thread as solved if you want to
