Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can Tensorflow be used to export the built model using Python?
TensorFlow is a machine learning framework 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.
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 will be using the Illiad's dataset, which contains text data of three translation works from William Cowper, Edward (Earl of Derby) and Samuel Butler. The model is trained to identify the translator when a single line of text is given. The text files used have been preprocessing. This includes removing the document header and footer, line numbers and chapter titles.
Creating the Export Model
To export a TensorFlow model that can handle raw text input, we need to create a preprocessing layer and combine it with our trained model ?
import tensorflow as tf
from tensorflow.keras.layers import TextVectorization
from tensorflow.keras import layers
import tensorflow_text as tf_text
# Define parameters
vocab_size = 10000
MAX_SEQUENCE_LENGTH = 250
# Sample vocabulary (in practice, this comes from your training data)
vocab = ["the", "and", "to", "of", "a", "in", "is", "it", "you", "that"]
print("The customized pre-processing step")
preprocess_layer = TextVectorization(
max_tokens=vocab_size,
standardize=tf_text.case_fold_utf8,
split='whitespace', # Using basic whitespace tokenizer for demo
output_mode='int',
output_sequence_length=MAX_SEQUENCE_LENGTH)
preprocess_layer.set_vocabulary(vocab)
# Create a simple model for demonstration
model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(3) # 3 translators to classify
])
print("The model is being exported")
export_model = tf.keras.Sequential([
preprocess_layer,
model,
layers.Activation('sigmoid')
])
print("Export model created successfully")
The customized pre-processing step The model is being exported Export model created successfully
How It Works
The TextVectorization layer performs the same preprocessing steps that were applied during training, converting raw strings into integer sequences.
The
set_vocabularymethod applies the previously trained vocabulary to the preprocessing layer.The export model combines preprocessing, the trained model, and final activation into a single pipeline.
This exported model can now accept raw text strings directly without manual preprocessing.
Saving the Export Model
Once the export model is created, you can save it for deployment ?
# Save the export model
export_model.save('translator_model')
print("Model saved successfully")
# Load and test the saved model
loaded_model = tf.keras.models.load_model('translator_model')
print("Model loaded successfully")
Model saved successfully Model loaded successfully
Conclusion
Exporting a TensorFlow model with preprocessing layers ensures that raw text can be processed directly without manual preprocessing steps. This approach creates a complete pipeline from raw input to final predictions, making deployment much simpler.
