How can Tensorflow be used to create a dataset of raw strings from the Illiad dataset 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("Creating a test dataset that consists of raw strings")
test_ds = all_labeled_data.take(VALIDATION_SIZE).batch(BATCH_SIZE)
test_ds = configure_dataset(test_ds)
loss, accuracy = export_model.evaluate(test_ds)
print("The loss is : ", loss)
print("The accuracy is: {:2.2%}".format(accuracy))

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

Output

Creating a test dataset that consists of raw strings
79/79 [==============================] - 7s 10ms/step - loss: 0.5230 - accuracy: 0.7909
The loss is : 0.5458346605300903
The accuracy is: 78.16%

Explanation

  • A test dataset is built where data is grouped into certain batch size.

  • This data is evaluated using the ‘evaluate’ method.

  • The loss and accuracy associated with the training data is displayed on the console.

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