How can Tensorflow be used with Illiad dataset to check how well the test data performs 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. 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 a 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.

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("Testing the model on new data")
inputs = [
   "the allies, and his armour flashed about him so that he seemed to all",
   "And with loud clangor of his arms he fell.",
   "Join'd to th' Ionians with their flowing robes,",
]
print("The predict method is being called")
predicted_scores = export_model.predict(inputs)
predicted_labels = tf.argmax(predicted_scores, axis=1)
for input, label in zip(inputs, predicted_labels):
   print("The question is : ", input)
   print("The predicted label is : ", label.numpy())

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

Output

Testing the model on new data
The predict method is being called
The question is : the allies, and his armour flashed about him so that he seemed to all
The predicted label is : 2
The question is : And with loud clangor of his arms he fell.
The predicted label is : 0
The question is : Join'd to th' Ionians with their flowing robes,
The predicted label is : 1

Explanation

  • Once the data has been compiled, and fit the training data, it is tested on never-before seen data.

  • The ‘predict’ method is called on the test data.

  • Some samples of the predicted label are displayed along with its corresponding question.

raja
Published on 19-Jan-2021 08:05:37
Advertisements