whitebear
whitebear

Reputation: 12433

Not every node is calculated by tensorflow.keras

I run this source code.

It calculates 60000 train data and 10000 test data

I suppose it should be calculated 60000 times for each epoch,like this below

x_train shape: (60000, 28, 28, 1)
y_train shape: (60000,)
60000 train samples
10000 test samples
60000/60000 [==============================]
.
.
.

However,in fact the result is like this below

x_train shape: (60000, 28, 28, 1)
y_train shape: (60000,)
60000 train samples
10000 test samples
469/469 [==============================] - 32s 69ms/step - loss: 2.2910 - accuracy: 0.1347 - val_loss: 2.2686 - val_accuracy: 0.3145
Test loss: 2.2686383724212646
Test accuracy: 0.31450000405311584
elapsed time:34.918644189834595

Why only 469 times calculated??? Where is wrong??

Python 3.8.5 tensorflow 2.3.1

    import tensorflow
    import tensorflow.keras as myKeras
    from tensorflow.keras.datasets import mnist
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense, Dropout, Flatten
    from tensorflow.keras.layers import Conv2D, MaxPooling2D
    from tensorflow.keras import backend as K

    start = time.time()
    batch_size = 128
    num_classes = 10
    epochs = 1
    img_rows, img_cols = 28, 28
    # the data, split between train and test sets
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
  
    if K.image_data_format() == 'channels_first':
        x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
        x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
        input_shape = (1, img_rows, img_cols)
    else:
        x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
        x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
        input_shape = (img_rows, img_cols, 1)
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255
    print('x_train shape:', x_train.shape)
    print('y_train shape:', y_train.shape)
    print(x_train.shape[0], 'train samples')
    print(x_test.shape[0], 'test samples')
 
    y_train = myKeras.utils.to_categorical(y_train, num_classes)
    y_test = myKeras.utils.to_categorical(y_test, num_classes)
    model = Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3),
                    activation='relu',
                    input_shape=input_shape))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(loss=myKeras.losses.categorical_crossentropy,
                optimizer=myKeras.optimizers.Adadelta(),
                metrics=['accuracy'])
    model.fit(x_train, y_train,
            batch_size=batch_size,
            epochs=epochs,
            verbose=1,
            validation_data=(x_test, y_test))
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
    elapsed_time = time.time() - start
    print("elapsed time:{0}".format(elapsed_time))

Upvotes: 0

Views: 62

Answers (1)

Amit Rautray
Amit Rautray

Reputation: 176

You need to set the steps_per_epochs and validation_steps parameters correctly in the model.fit() functions.

steps_per_epoch the number of batch iterations before a training epoch is considered finished. If you have a training set of the fixed size you can ignore it but it may be useful if you have a huge data set or if you are generating random data augmentations on the fly, i.e. if your training set has a (generated) infinite size. If you have the time to go through your whole training data set I recommend skipping this parameter. validation_steps similar to steps_per_epoch but on the validation data set instead of the training data. If you have the time to go through your whole validation data set I recommend skipping this parameter.

validation_steps similar to steps_per_epoch but on the validation data set instead of on the training data. If you have the time to go through your whole validation data set I recommend skipping this parameter.

In your case set:

  1. batch_size = 200
  2. steps_per_epoch = 300
  3. validation_steps = 50

Upvotes: 1

Related Questions