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 multidimensional array or a list.
The ‘Fashion MNIST’ dataset contains images of clothing of different kinds. It contains grayscale images of more than 70 thousand clothes that belong to 10 different categories. These images are of low resolution (28 x 28 pixels).
We are using the 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. Following is the code −
Example
print("The model is fit to the data") model.fit(train_images, train_labels, epochs=15) print("The accuracy is being computed") test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2) print('\nThe test accuracy is :', test_acc)
Code credit − https://www.tensorflow.org/tutorials/keras/classification
Output
The model is fit to the data Epoch 1/15 1875/1875 [==============================] - 4s 2ms/step - loss: 0.6337 - accuracy: 0.7799 Epoch 2/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.3806 - accuracy: 0.8622 Epoch 3/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.3469 - accuracy: 0.8738 Epoch 4/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.3131 - accuracy: 0.8853 Epoch 5/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2962 - accuracy: 0.8918 Epoch 6/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2875 - accuracy: 0.8935 Epoch 7/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2705 - accuracy: 0.8998 Epoch 8/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2569 - accuracy: 0.9023 Epoch 9/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2465 - accuracy: 0.9060 Epoch 10/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2440 - accuracy: 0.9088 Epoch 11/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2300 - accuracy: 0.9143 Epoch 12/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2255 - accuracy: 0.9152 Epoch 13/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2114 - accuracy: 0.9203 Epoch 14/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2101 - accuracy: 0.9211 Epoch 15/15 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2057 - accuracy: 0.9224 The accuracy is being computed 313/313 - 0s - loss: 0.3528 - accuracy: 0.8806 The test accuracy is : 0.8805999755859375
Explanation
The model is trained by first feeding the training data and building a model. The ‘train_images’ and ‘train_labels’ are arrays of input data.
The model leanrs to map the image with respective labels.
The ‘test_images’ stores the test data.
Once the test dataset is utilized, the predictions made are matches with the actual labels of the data in test dataset.
The ‘model.fit’ method is called so that it can fit the moel to the training dataset.
The ‘model.evaluate’ function gives the accuracy and the loss associated with the training.