<jupyter_start><jupyter_text>TensorFlow 2 quickstart - Training  Welcome to PrimeHub!In this quickstart, we will perfome following actions to train a model: 1. Train a neural network that classifies images.1. Move trained model to PHFS Storage.  Prerequisites1. Enable PHFS Storage.**Contact your admin if any prerequisite is not enabled yet.**  1. Train a neural network that classifies images Firstly, let's import libraries.<jupyter_code>import tensorflow as tf
from datetime import datetime<jupyter_output><empty_output><jupyter_text>Load and prepare the MNIST dataset. Convert the samples from integers to floating-point numbers.<jupyter_code>mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0<jupyter_output><empty_output><jupyter_text>Build the `tf.keras.Sequential` model by stacking layers.<jupyter_code>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, activation=tf.nn.softmax)
])<jupyter_output><empty_output><jupyter_text>Choose an optimizer, loss function, and metrics function.<jupyter_code>loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
optimizer = 'adam'
metrics = ['accuracy']<jupyter_output><empty_output><jupyter_text>Compile the model.<jupyter_code>model.compile(loss=loss, optimizer=optimizer, metrics=metrics)<jupyter_output><empty_output><jupyter_text>The `Model.fit` method adjusts the model parameters to minimize the loss.<jupyter_code>num_epoch = 3
model.fit(x_train, y_train, epochs=num_epoch)<jupyter_output>Epoch 1/3
1875/1875 [==============================] - 5s 2ms/step - loss: 1.6603 - accuracy: 0.8347
Epoch 2/3
1875/1875 [==============================] - 4s 2ms/step - loss: 1.5307 - accuracy: 0.9364
Epoch 3/3
1875/1875 [==============================] - 4s 2ms/step - loss: 1.5118 - accuracy: 0.9541<jupyter_text>The `Model.evaluate` method checks the models performance.<jupyter_code>model.evaluate(x_test,  y_test, verbose=2)<jupyter_output>313/313 - 1s - loss: 1.5018 - accuracy: 0.9620<jupyter_text>Save the trained model.<jupyter_code>now = datetime.now()
date_time = now.strftime("%Y%m%d-%H%M%S")
SAVED_NAME = f"tf2-mnist-{date_time}" 
model.save(SAVED_NAME)
print(f"We successfully saved the model in {SAVED_NAME}.")<jupyter_output>INFO:tensorflow:Assets written to: tf2-mnist-20211027-154429/assets
We successfully saved the model in tf2-mnist-20211027-154429.<jupyter_text>2. Move trained model to PHFS Storage To deploy our model later, we need to move model to PHFS storage.<jupyter_code>SAVED_DIR = "~/phfs/example-models/tensorflow2"
! mkdir -p $SAVED_DIR
! mv $SAVED_NAME $SAVED_DIR<jupyter_output><empty_output><jupyter_text>Check the model under PHFS storage.<jupyter_code>! ls -lt $SAVED_DIR<jupyter_output>total 0
drwxr-xr-x 1 root root 0 Oct 27 15:44 tf2-mnist-20211027-154429
drwxr-xr-x 1 root root 0 Oct 27 15:34 tf2-mnist-20211027-153406
drwxr-xr-x 1 root root 0 Oct 27 15:33 tf2-mnist-20211027-153337
{'fraction_non_alphanumeric': 0.13901345291479822, 'fraction_numerical': 0.06790518898142217, 'mean_word_length': 6.2459396751740135, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '452741', 'license': 'Apache-2.0', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1130, 'n_words': 261, 'path': 'examples/notebook-examples/data/tensorflow2/0_quickstart/0_tensorflow2_training.ipynb', 'repo_name': '3r1ccc/primehub', 'size': '3122'}