Revision

Back to Python


Import

Base import of TensorFlow requires tensorflow and is generally cast as tf.

import tensorflow as tf



Model Creation

A TensorFlow model is created as an instance of tf.keras.models.Sequential:

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)
])

The model uses different composants of TensforFlow that are in tf.keras.layers. The backward is implicitly created from the forward method and the composants used.



Training

model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)

The training of the model is easily done in TensorFlow using the compile method. It only requires to specify the optimizer, the loss and the metrics to check. Then the training is launch on the data using the fit method and specifying the input and output data.



Ressources

See: