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

PythonServer Side ProgrammingProgramming

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.

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.

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 customized pre-processing step")
preprocess_layer = TextVectorization(
   max_tokens=vocab_size,
   standardize=tf_text.case_fold_utf8,
   split=tokenizer.tokenize,
   output_mode='int',
   output_sequence_length=MAX_SEQUENCE_LENGTH)
preprocess_layer.set_vocabulary(vocab)
print("The model is being exported")
   export_model = tf.keras.Sequential(
   [preprocess_layer, model,
   layers.Activation('sigmoid')])

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

Output

The customized pre-processing step
The model is being exported

Explanation

  • If we want our model to take raw strings as input, we eed to create a ‘textVectorization’ layer which performs the same function as that of preprocessing.

  • The vocabulary has already been trained, which means we can use the ‘set_vocabulary’ method to train the new vocabulary.

raja
Published on 19-Jan-2021 08:00:50
Advertisements