How can Tensorflow be used to compile and fit the model using Python?

PythonServer Side ProgrammingProgramming

Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

Practical Data Science using Python

22 Lectures 6 hours

Tensorflow is a machine learning framework that is provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes.

It has optimization techniques that help in performing complicated mathematical operations quickly.

This is because it uses NumPy and multi-dimensional arrays. These multi-dimensional arrays are also known as ‘tensors’. The framework supports working with deep neural network. It is highly scalable, and comes with many popular datasets. It uses GPU computation and automates the management of resources. It comes with multitude of machine learning libraries, and is well-supported and documented. The framework has the ability to run deep neural network models, train them, and create applications that predict relevant characteristics of the respective datasets.

The ‘tensorflow’ package can be installed on Windows using the below line of code −

pip install tensorflow

Tensor is a data structure used in TensorFlow. It helps connect edges in a flow diagram. This flow diagram is known as the ‘Data flow graph’. Tensors are nothing but multidimensional array or a list.

We are using Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook. Following is the code snippet −

Example

print("The vocab_size is actually vocab_size+1 since 0 is used as padding")
int_model = create_model(vocab_size=VOCAB_SIZE + 1, num_labels=4)
print("The model is compiled")
int_model.compile(
loss=losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
print("The model is fit to the data")
history = int_model.fit(int_train_ds, validation_data=int_val_ds, epochs=5)

Output

The vocab_size is actually vocab_size+1 since 0 is used as padding
The model is compiled
The model is fit to the data
Epoch 1/5
188/188 [==============================] - 7s 37ms/step - loss: 1.3020 - accuracy: 0.3877 -
val_loss: 0.8041 - val_accuracy: 0.6625
Epoch 2/5
188/188 [==============================] - 5s 25ms/step - loss: 0.7200 - accuracy: 0.7003 -
val_loss: 0.5815 - val_accuracy: 0.7685
Epoch 3/5
188/188 [==============================] - 5s 25ms/step - loss: 0.4517 - accuracy: 0.8471 -
val_loss: 0.5137 - val_accuracy: 0.8040
Epoch 4/5
188/188 [==============================] - 5s 25ms/step - loss: 0.2709 - accuracy: 0.9311 -
val_loss: 0.5091 - val_accuracy: 0.8065
Epoch 5/5
188/188 [==============================] - 5s 25ms/step - loss: 0.1453 - accuracy: 0.9717 -
val_loss: 0.5320 - val_accuracy: 0.8025

Explanation

• The ‘create_model’ method is used to create a model.

• This model is compiled using the ‘compile’ method.

• The ‘fit’ method is called on this compiled model to fit the data to the model.

Updated on 19-Jan-2021 07:30:45