簡體   English   中英

同一組權重的神經網絡損失顯着變化 - Keras

[英]Neural Network loss is significantly changing for same set of weights - Keras

model = keras.Sequential([
layers.Dense(10, activation = 'relu', weights=[zero_weights, zero_bias]),  
layers.Dense(24, activation = 'relu', weights=[one_weights, one_bias]),
layers.Dense(12, activation = 'relu', weights=[two_weights, two_bias]),
layers.Dense(1, weights=[three_weights, three_bias])])
# Compile Model
model.compile(loss='mse',
            optimizer=keras.optimizers.Adam(learning_rate=0.1),
            metrics=['mse'])
model.fit(inputs, targets,
          batch_size=5,
          epochs=100)
prediction_test = model.predict(X_test)
mse=mean_squared_error(prediction_test,y_test)
print(mse)

我使用預初始化權重作為神經網絡的初始權重,但每次訓練 model 時,損失值都會不斷變化。 如果初始權重相同,那么 model 每次訓練時都應該預測出完全相同的值。 但是mse一直在變化。 有什么我想念的嗎?

設置預初始化值並不妨礙訓練。

將 layer .trainable屬性設置為 False:

model.layers[0].trainable = False

您已將所有層初始化為一些權重(可能是您從上一次訓練中獲得的權重),但是每次您開始訓練過程時,您傳遞給訓練的數據都會以不同的方式進行混洗,因為 model.fit() api 有混洗參數默認設置為 True。 如果將其設置為 False,如果訓練數據相同,您將不會看到 model 權重在后續運行期間得到更新。

model.fit(inputs, targets,
          batch_size=5,
          epochs=100, shuffle=False)

暫無
暫無

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

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