2

I am performing CNN in google colab notebook in the pro version. Though the x_train takes the shape (60,000, 28,28). The model gets trained on only 1875 rows. Did any one faced this issue before? My model runs fine on local machine's jupyter notebook. It runs on all 60,000 rows

    import tensorflow as tf
    mnist = tf.keras.datasets.mnist

    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train = x_train.astype('float32') / 255.0
    y_train = y_train.astype('float32') / 255.0

    print("x_train.shape:", x_train.shape)

    #Build the model
    from tensorflow.keras.layers import Dense, Flatten, Dropout
    model = tf.keras.models.Sequential([
            tf.keras.layers.Flatten(input_shape=(28,28)),
            tf.keras.layers.Dense(128, activation='relu'),
            tf.keras.layers.Dropout(0.2),
            tf.keras.layers.Dense(10, activation='softmax')
    ])

    r = model.fit(x_train, y_train, validation_data=(x_test,y_test), epochs = 10)


    Output:

    x_train.shape: (60000, 28, 28)

    Epoch 1/10
    1875/1875 [==============================] - 3s 2ms/step - loss: 2.2912e-06 - accuracy:                            0.0987 - val_loss: 7716.5078 - val_accuracy: 0.0980
amiftekher
  • 30
  • 4
Naveen
  • 29
  • 2

2 Answers2

2

1875 is a number of batches. By default batches contain 32 samles.
60000 / 32 = 1875

Yoskutik
  • 1,859
  • 2
  • 17
  • 43
  • I think 32 are the number of batches and there exists 32 times each batch(1875) = 60,000 of them. But i want to train on the entire data set. Do not want to split into batches. Can i do that? Training gets done on 60,000 rows on my local machine. Problem is with google colab notebook. – Naveen May 23 '20 at 10:19
  • Well, I guess setting `batch_size=60000` in the `fit` function is the only way – Yoskutik May 23 '20 at 10:25
  • @Naveen you *do* train on the entire dataset, and splitting it into batches is the standard way to do so. Setting `batch_size=60000` will most probably lead to memory issues, and is not the way to approach the problem. – desertnaut May 23 '20 at 12:43
0

If you use keras, instead if tensorflow.keras, the log will show:

x_train.shape: (60000, 28, 28)
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 6s 107us/step - loss: 0.9655 - val_loss: 20.2422

But both of them are internally the same, one is showing the number of samples to be trained (keras), another is showing the number of iterations (tf.keras).

You can not probably train on all the 60000 samples at a time, we need to batch the inputs so that we are not out of GPU memory. You can try increasing your batch_size as much as you can, but after a point you'll get an error like OOMError, CUDA out of memory, etc.

Zabir Al Nazi
  • 10,298
  • 4
  • 33
  • 60