簡體   English   中英

不是每個節點都由 tensorflow.keras 計算

[英]Not every node is calculated by tensorflow.keras

我運行這個源代碼。

它計算了 60000 個訓練數據和 10000 個測試數據

我想它應該為每個時代計算 60000 次,如下所示

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

然而,實際上結果是這樣的

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

為什么只計算了 469 次??? 哪里錯了??

Python 3.8.5 張量流 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))

您需要在 model.fit() 函數中正確設置 steps_per_epochs 和 validation_steps 參數。

steps_per_epoch訓練時期被認為完成之前的批量迭代次數。 如果你有一個固定大小的訓練集,你可以忽略它,但如果你有一個巨大的數據集或者你正在動態生成隨機數據增強,即如果你的訓練集具有(生成的)無限大小,它可能很有用. 如果您有時間瀏覽整個訓練數據集,我建議您跳過此參數。 validation_steps 類似於 steps_per_epoch 但在驗證數據集而不是訓練數據上。 如果您有時間瀏覽整個驗證數據集,我建議您跳過此參數。

validation_steps類似於 steps_per_epoch 但在驗證數據集上而不是在訓練數據上。 如果您有時間瀏覽整個驗證數據集,我建議您跳過此參數。

在您的情況下設置:

  1. 批量大小 = 200
  2. step_per_epoch = 300
  3. 驗證步驟 = 50

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM