如果您想查看层的权重和偏置如何随时间变化,可以添加回调函数,在每个训练时期记录它们的值。
例如,使用这样的模型:
import numpy as np
model = Sequential([Dense(16, input_shape=(train_inp_s.shape[1:])), Dense(12), Dense(6), Dense(1)])
在拟合过程中添加回调函数的 **kwarg:
gw = GetWeights()
model.fit(X, y, validation_split=0.15, epochs=10, batch_size=100, callbacks=[gw])
回调函数的定义位置在
class GetWeights(Callback):
def __init__(self):
super(GetWeights, self).__init__()
self.weight_dict = {}
def on_epoch_end(self, epoch, logs=None):
for layer_i in range(len(self.model.layers)):
w = self.model.layers[layer_i].get_weights()[0]
b = self.model.layers[layer_i].get_weights()[1]
print('Layer %s has weights of shape %s and biases of shape %s' %(
layer_i, np.shape(w), np.shape(b)))
if epoch == 0:
self.weight_dict['w_'+str(layer_i+1)] = w
self.weight_dict['b_'+str(layer_i+1)] = b
else:
self.weight_dict['w_'+str(layer_i+1)] = np.dstack(
(self.weight_dict['w_'+str(layer_i+1)], w))
self.weight_dict['b_'+str(layer_i+1)] = np.dstack(
(self.weight_dict['b_'+str(layer_i+1)], b))
此回调函数将根据层编号为所有层的权重和偏差构建一个字典,因此您可以看到在模型训练过程中它们随着时间如何变化。您会注意到每个权重和偏差数组的形状取决于模型层的形状。每个模型层保存一个权重数组和一个偏差数组。第三个轴(深度)显示它们随时间的演变。
这里我们使用了10个epochs和一个有16、12、6和1个神经元层的模型:
for key in gw.weight_dict:
print(str(key) + ' shape: %s' %str(np.shape(gw.weight_dict[key])))
w_1 shape: (5, 16, 10)
b_1 shape: (1, 16, 10)
w_2 shape: (16, 12, 10)
b_2 shape: (1, 12, 10)
w_3 shape: (12, 6, 10)
b_3 shape: (1, 6, 10)
w_4 shape: (6, 1, 10)
b_4 shape: (1, 1, 10)
y=dense2(dense1(input_x))
更好的方式吗? - Toke Faurby