# Translating a basic MNIST tensorflow network into an MLJFlux builder

**URL:** <https://discourse.julialang.org/t/translating-a-basic-mnist-tensorflow-network-into-an-mljflux-builder/89338>\
**Category:** Machine Learning\
**Tags:** mlj\
**Created:** [October 27, 2022, 12:06am UTC](https://discourse.julialang.org/t/translating-a-basic-mnist-tensorflow-network-into-an-mljflux-builder/89338 "2022-10-27T00:06:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)\
**Post date:** [October 27, 2022, 12:06am UTC](https://discourse.julialang.org/t/translating-a-basic-mnist-tensorflow-network-into-an-mljflux-builder/89338/1 "2022-10-27T00:06:30Z")

</div>

On slack I have been asked:

How does one translate the following into MLJFlux. The code is from [this](https://www.tensorflow.org/tutorials/quickstart/beginner) tensorflow tutorial on the MNIST dataset for beginners.

```python
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.2), 
    tf.keras.layers.Dense(10) 
])

```

---

<div class="post-metadata">

**Author:** ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)\
**Post date:** [October 27, 2022, 12:08am UTC](https://discourse.julialang.org/t/translating-a-basic-mnist-tensorflow-network-into-an-mljflux-builder/89338/2 "2022-10-27T00:08:20Z")

</div>

You can define an equivalent MLJFlux chain builder (that works with images of any size) with:

```julia
import MLJFlux
using Flux

builder = MLJFlux.@builder Chain(
    Flux.flatten,
    Dense(prod(n_in)*n_channels => 128, relu),
    Dropout(0.2),
    Dense(128, 10),
)

```

Just change the `builder` in [this notebook](https://github.com/FluxML/MLJFlux.jl/blob/dev/examples/mnist/notebook.unexecuted.ipynb) to the one above to use with MNIST images.

Note that `ImageClassifier` automatically adds a `softmax` at the end, to get probabilities. If you don’t want that final layer, set `finaliser=identity` in the `ImageClassifier` constructor.
