在 Tensorflow - Keras 中撰寫了一個基本的深度學習模型。
為什么訓練結束時報告的訓練集準確度 (0.4097) 與使用預測函式(或使用評估,給出相同數字)= 0.6463 直接計算相同訓練資料后直接報告的結果不同?
下面是 MWE;之后直接輸出。
from extra_keras_datasets import kmnist
import tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.layers import BatchNormalization
import numpy as np
# Model configuration
no_classes = 10
# Load KMNIST dataset
(input_train, target_train), (input_test, target_test) = kmnist.load_data(type='kmnist')
# Shape of the input sets
input_train_shape = input_train.shape
input_test_shape = input_test.shape
# Keras layer input shape
input_shape = (input_train_shape[1], input_train_shape[2], 1)
# Reshape the training data to include channels
input_train = input_train.reshape(input_train_shape[0], input_train_shape[1], input_train_shape[2], 1)
input_test = input_test.reshape(input_test_shape[0], input_test_shape[1], input_test_shape[2], 1)
# Parse numbers as floats
input_train = input_train.astype('float32')
input_test = input_test.astype('float32')
# Normalize input data
input_train = input_train / 255
input_test = input_test / 255
# Create the model
model = Sequential()
model.add(Flatten(input_shape=input_shape))
model.add(Dense(no_classes, activation='softmax'))
# Compile the model
model.compile(loss=tensorflow.keras.losses.sparse_categorical_crossentropy,
optimizer=tensorflow.keras.optimizers.Adam(),
metrics=['accuracy'])
# Fit data to model
history = model.fit(input_train, target_train,
batch_size=2000,
epochs=1,
verbose=1)
prediction = model.predict(input_train)
print("Prediction accuracy = ", np.mean( np.argmax(prediction, axis=1) == target_train))
model.evaluate(input_train, target_train, verbose=2)
最后幾行輸出:
30/30 [==============================] - 0s 3ms/step - loss: 1.8336 - accuracy: 0.4097
Prediction accuracy = 0.6463166666666667
1875/1875 - 1s - loss: 1.3406 - accuracy: 0.6463
編輯.
下面的初步答案解決了我的第一個問題,指出當您只運行 1 個 epoch 時,批量大小很重要。當運行小批量(或批量 = 1)或更多 epoch 時,您可以將擬合后的預測準確度推到非常接近擬合本身拋出的最終準確度。哪個好!
我最初問這個問題是因為我在使用更復雜的模型時遇到了問題。
我仍然無法理解在這種情況下發生了什么(是的,它涉及批量標準化)。要獲得我的 MWE,請將上面“創建模型”下面的所有內容替換為下面的代碼,以實作一些具有批量標準化的全連接層。
當您運行兩個 epoch 時 - 您會看到所有 30 個小批次的準確度非常穩定(30 因為訓練集中的 60,000 除以每批次的 2000)。在整個第二個訓練階段,我看到非常一致的 83% 準確率。
但是擬合后的預測是一個糟糕的 10% 左右。誰能解釋一下?
model = Sequential()
model.add(Dense(50, activation='relu', input_shape = input_shape))
model.add(BatchNormalization())
model.add(Dense(20, activation='relu'))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(no_classes, activation='softmax'))
# Compile the model
model.compile(loss=tensorflow.keras.losses.sparse_categorical_crossentropy,
optimizer=tensorflow.keras.optimizers.Adam(),
metrics=['accuracy'])
# Fit data to model
history = model.fit(input_train, target_train,
batch_size=2000,
epochs=2,
verbose=1)
prediction = model.predict(input_train)
print("Prediction accuracy = ", np.mean( np.argmax(prediction, axis=1) == target_train))
model.evaluate(input_train, target_train, verbose=2, batch_size = batch_size)
30/30 [==============================] - 46s 2s/step - loss: 0.5567 - accuracy: 0.8345
Prediction accuracy = 0.10098333333333333
uj5u.com熱心網友回復:
發生這種情況的一個原因是,報告的最后一個準確度考慮了整個時期,其引數不是恒定的,并且仍在優化中。
在評估模型時,引數停止變化,它們保持在最終(希望是最優化的)狀態。與上一個時期不同,在上一個時期,引數處于各種(希望是優化程度較低的)狀態,在時期開始時更是如此。
洗掉是因為我現在看到您在這種情況下沒有使用批處理規范。
我假設這是由于BatchNormalization.
參見例如here
在訓練期間,使用移動平均線。
在推理程序中,我們已經有了歸一化引數
這很可能是造成差異的原因。
請嘗試不使用它,看看是否仍然存在如此巨大的差異。
uj5u.com熱心網友回復:
只需添加到@Gulzar 答案:這種效果可以非常明顯,因為 OP 只使用了一個時期(很多引數在訓練的一開始就發生了變化),批量大小在評估方法(默認為 32)和擬合方法中不相等,批量大小遠小于整個資料(意味著在每個時期進行大量更新)。
只需在同一個實驗中添加更多的 epoch 就會減弱這種效果。
# Fit data to model
history = model.fit(input_train, target_train,
batch_size=2000,
epochs=40,
verbose=1)
結果
Epoch 40/40
30/30 [==============================] - 0s 11ms/step - loss: 0.5663 - accuracy: 0.8339
Prediction accuracy = 0.8348
1875/1875 - 2s - loss: 0.5643 - accuracy: 0.8348 - 2s/epoch - 1ms/step
[0.5643048882484436, 0.8348000049591064]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/417934.html
標籤:
