How can Tensorflow be used to export the model built using Python?


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 a 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 a 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.

Example

Following is the code snippet −

print("The model is being exported")
export_model = tf.keras.Sequential(
   [binary_vectorize_layer, binary_model,
   layers.Activation('sigmoid')])
print("The model is being compiled")
export_model.compile(
   loss=losses.SparseCategoricalCrossentropy(from_logits=False),
   optimizer='adam',
   metrics=['accuracy'])
print("The model is being tested with `raw_test_ds`, which resuls in raw strings")
loss, accuracy = export_model.evaluate(raw_test_ds)
print("The accuracy of the model is : {:2.2%}".format(binary_accuracy))

Code credit − https://www.tensorflow.org/tutorials/load_data/text

Output

The model is being exported
The model is being compiled
The model is being tested with `raw_test_ds`, which resuls in raw strings
250/250 [==============================] - 4s 13ms/step - loss: 0.5296 - accuracy: 0.8078
The accuracy of the model is : 81.10%

Explanation

  • The ‘TextVectorization’ layer is applied to the dataset before it is fed to the model.

  • If the model needs to process raw strings, the ‘TextVectorization’ layer can be applied inside the model.

  • To achieve this, a new model is created with the help of the weights that were used during training.

Updated on: 19-Jan-2021

77 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements