Download training.py from starcode-tech/mnist-image-classification: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/spaces/starcode-tech/mnist-image-classification/resolve/main/training.py
- Command line
-
hf download hf://spaces/starcode-tech/mnist-image-classification/training.py
-
curl -L -o training.py https://huggingface.co/spaces/starcode-tech/mnist-image-classification/resolve/main/training.py
1.08 kB
| from tensorflow import keras | |
| (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() | |
| x_train = x_train / 255.0 | |
| x_test = x_test / 255.0 | |
| model = keras.models.Sequential([ | |
| keras.layers.Flatten(input_shape=(28, 28)), # Diese Schicht nimmt unser 2D-Bild und verwandelt es in ein 1D-Array | |
| keras.layers.Dense(512, activation='relu'), # Als Nächstes kommen zwei Schichten mit 512 künstlichen Neuronen. Als Funktion wählen wir 'relu' f(x) = max(0,x) | |
| keras.layers.Dense(512, activation='relu'), | |
| keras.layers.Dense(10, activation='softmax') # Die letzte Schicht besteht aus 10 Neuronen, die für unsere 10 Zahlen stehen. Die 'softmax' Funktion wandelt die Ergebnisse der vorherigen Schicht in Wahrscheinlichkeiten | |
| ]) | |
| model.compile(optimizer=keras.optimizers.Adam(0.001), | |
| loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), | |
| metrics=[keras.metrics.SparseCategoricalAccuracy()]) | |
| model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=6) | |
| model.save_weights('./weights/mnist.weights.h5') |