如何在Keras模型中的每个时期构建函数后检查更新权重的比率

时间:2019-02-21 10:37:07

标签: python tensorflow keras

我想查看我的keras模型权重,并希望在它们之间进行更新,我从这里得到了一些参考。 This is the link I tried,但会出现错误,未定义print_weights,因为如果要重复使用,我将通过函数构建模型,而我实际上想知道权重的更新比率,以便决定自己选择是否有更好的参数。请查看我的代码

def model(input1)    :
    model = Sequential () # we make a sequentail model

    model.add(Dense(256, input_dim=input1,activation  ='relu',activity_regularizer = regularizers.l2(1e-4)))
    model.add(Dropout(0.5))
    model.add(Dense(128, input_dim=input1,activation  ='relu',activity_regularizer = regularizers.l2(1e-4)))
    model.add(Dropout(0.5))
    model.add(Dense(64, input_dim=input1,activation  ='relu',activity_regularizer = regularizers.l2(1e-6)))
    model.add(Dropout(0.25))
    model.add(Dense(32, activation  ='relu',activity_regularizer = regularizers.l2(1e-8)))
    model.add(Dropout(0.25))
    model.add(Dense(16, activation  ='relu',activity_regularizer = regularizers.l2(1e-8)))
    model.add(Dropout(0.25))
    model.add(Dense(8, activation  ='relu'))

    model.add(Dense(2, activation  ='softmax')) #softmax layer to compute the probability of
                                                #labels
    print_weights = LambdaCallback(on_epoch_end=lambda batch, logs: print(model.layers[8].get_weights()))

    model.summary()
    model.compile(loss=keras.losses.squared_hinge, optimizer=keras.optimizers.Adam(lr=0.00007, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False), metrics=['accuracy'])
    return model

import matplotlib.pyplot as plt
model=model(19)
history =model.fit(X_train, y_train, epochs=2000, batch_size=300,validation_data=(X_test,y_test),
                  callbacks = [print_weights])

# Plot training & validation accuracy values
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])

plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

# Plot training & validation loss values
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-66-a7f6ec00aa0b> in <module>()
      2 model=model(19)
      3 history =model.fit(X_train, y_train, epochs=2000, batch_size=300,validation_data=(X_test,y_test),
----> 4                   callbacks = [print_weights])
      5 
      6 # Plot training & validation accuracy values

NameError: name 'print_weights' is not defined

请告诉我如何获取先前权重和当前权重之间的更新比率。我的体重以哪个比例增加。谢谢。

0 个答案:

没有答案