Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras not training on entire dataset

So I've been following Google's official tensorflow guide and trying to build a simple neural network using Keras. But when it comes to training the model, it does not use the entire dataset (with 60000 entries) and instead uses only 1875 entries for training. Any possible fix?

import tensorflow as tf
from tensorflow import keras
import numpy as np

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

train_images = train_images / 255.0
test_images = test_images / 255.0

class_names = ['T-shirt', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot']

model = keras.Sequential([
                          keras.layers.Flatten(input_shape=(28, 28)),
                          keras.layers.Dense(128, activation='relu'), 
                          keras.layers.Dense(10)
])

model.compile(optimizer='adam',
              loss= tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=10)

Output:

Epoch 1/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3183 - accuracy: 0.8866
Epoch 2/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3169 - accuracy: 0.8873
Epoch 3/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3144 - accuracy: 0.8885
Epoch 4/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3130 - accuracy: 0.8885
Epoch 5/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3110 - accuracy: 0.8883
Epoch 6/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3090 - accuracy: 0.8888
Epoch 7/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3073 - accuracy: 0.8895
Epoch 8/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3057 - accuracy: 0.8900
Epoch 9/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3040 - accuracy: 0.8905
Epoch 10/10
1875/1875 [==============================] - 3s 2ms/step - loss: 0.3025 - accuracy: 0.8915

<tensorflow.python.keras.callbacks.History at 0x7fbe0e5aebe0>

Here's the original google colab notebook where I've been working on this: https://colab.research.google.com/drive/1NdtzXHEpiNnelcMaJeEm6zmp34JMcN38

like image 602
Sabyasachi Bhoi Avatar asked Apr 09 '20 13:04

Sabyasachi Bhoi


People also ask

How do you train a keras model?

Fit Keras Model You can train or fit your model on your loaded data by calling the fit() function on the model. Training occurs over epochs, and each epoch is split into batches. One epoch comprises one or more batches, based on the chosen batch size, and the model is fit for many epochs.

How can keras training be stopped?

You can use model. stop_training parameter to stop the training.

What is the model compile () method used for in keras?

compile method. Configures the model for training. optimizer: String (name of optimizer) or optimizer instance.

Why is the training loss much higher than the testing loss keras?

Why is my training loss much higher than my testing loss? A Keras model has two modes: training and testing. Regularization mechanisms, such as Dropout and L1/L2 weight regularization, are turned off at testing time. They are reflected in the training time loss but not in the test time loss.


1 Answers

The number 1875 shown during fitting the model is not the training samples; it is the number of batches.

model.fit includes an optional argument batch_size, which, according to the documentation:

If unspecified, batch_size will default to 32.

So, what happens here is - you fit with the default batch size of 32 (since you have not specified anything different), so the total number of batches for your data is

60000/32 = 1875
like image 170
desertnaut Avatar answered Oct 27 '22 00:10

desertnaut