keras model.layers.get_weight是否显示最佳权重?

jw5wzhpr  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(163)

我正在使用keras来训练NN模型。我尝试使用model.layers.get_weight来获得权重。我使用的代码是这样的:

def reset_seeds():
   np.random.seed(0) 
   python_random.seed(0)
   tf.random.set_seed(0)

reset_seeds() 

model1 = Sequential()
model1.add(Dense(100, input_dim= Train_X2_Tfidf.shape[1], activation='sigmoid'))
model1.add(Dense(1, activation='sigmoid'))
opt = Adam (learning_rate=0.01)
model1.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
model1.summary()

es = EarlyStopping(monitor="val_loss",mode='min',patience=10)
history1 = model1.fit(Train_X2_Tfidf, Train_Y2, epochs=400, verbose=1,
                    validation_split=0.2, batch_size=32, callbacks =[es])

我得到输出如下所示:

Epoch 1/400
72/72 [==============================] - 2s 8ms/step - loss: 0.5032 - accuracy: 0.7595 - val_loss: 0.3312 - val_accuracy: 0.8815
Epoch 2/400
72/72 [==============================] - 0s 5ms/step - loss: 0.2553 - accuracy: 0.9076 - val_loss: 0.2346 - val_accuracy: 0.9077
Epoch 3/400
72/72 [==============================] - 0s 5ms/step - loss: 0.1758 - accuracy: 0.9338 - val_loss: 0.2167 - val_accuracy: 0.9111
Epoch 4/400
72/72 [==============================] - 0s 6ms/step - loss: 0.1397 - accuracy: 0.9512 - val_loss: 0.2215 - val_accuracy: 0.9024
Epoch 5/400
72/72 [==============================] - 0s 5ms/step - loss: 0.1178 - accuracy: 0.9599 - val_loss: 0.2300 - val_accuracy: 0.9007
Epoch 6/400
72/72 [==============================] - 0s 5ms/step - loss: 0.1063 - accuracy: 0.9660 - val_loss: 0.2474 - val_accuracy: 0.8955
Epoch 7/400
72/72 [==============================] - 0s 5ms/step - loss: 0.0956 - accuracy: 0.9678 - val_loss: 0.2630 - val_accuracy: 0.8902
Epoch 8/400
72/72 [==============================] - 0s 6ms/step - loss: 0.0907 - accuracy: 0.9664 - val_loss: 0.2700 - val_accuracy: 0.8920
Epoch 9/400
72/72 [==============================] - 0s 5ms/step - loss: 0.0844 - accuracy: 0.9704 - val_loss: 0.2867 - val_accuracy: 0.8972
Epoch 10/400
72/72 [==============================] - 0s 6ms/step - loss: 0.0813 - accuracy: 0.9739 - val_loss: 0.2979 - val_accuracy: 0.8920
Epoch 11/400
72/72 [==============================] - 0s 5ms/step - loss: 0.0771 - accuracy: 0.9752 - val_loss: 0.3183 - val_accuracy: 0.8920
Epoch 12/400
72/72 [==============================] - 0s 5ms/step - loss: 0.0750 - accuracy: 0.9743 - val_loss: 0.3408 - val_accuracy: 0.8850
Epoch 13/400
72/72 [==============================] - 0s 5ms/step - loss: 0.0719 - accuracy: 0.9756 - val_loss: 0.3546 - val_accuracy: 0.8850

我得到的最佳时期是在时期3。然后我使用model.layers.get_weight来显示权重,并得到如下输出:

w_model1=model1.layers[1].get_weights()
w_model1

[array([[-0.4495648 ],
        [-0.4584201 ],
        [-0.43307182],
         ....
        [ 0.48044512]], dtype=float32), array([0.05600574], dtype=float32)]

我感到困惑的是,此输出权重是否显示模型获得的最佳权重?如果是,是否意味着此输出是第3阶段的权重?

fykwrbwg

fykwrbwg1#

不,这不是最佳权重,而是最后一个时期的权重。为了获得最佳权重,您必须使用ModelCheckpoint

model.compile(loss=..., optimizer=...,
              metrics=['accuracy'])

EPOCHS = 10
checkpoint_filepath = '/tmp/checkpoint'
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_filepath,
    save_weights_only=True,
    monitor='val_accuracy',
    mode='max',
    save_best_only=True)

# Model weights are saved at the end of every epoch, if it's the best seen
# so far.
model.fit(epochs=EPOCHS, callbacks=[model_checkpoint_callback])

# The model weights (that are considered the best) are loaded into the
# model.
model.load_weights(checkpoint_filepath)

model.layers[1].get_weights()是最佳权重。

相关问题