简体   繁体   English

如何从模型中获取权重和偏差?

[英]How do I get weights and biases from my model?

I have a simple neural network, I need to get weights and biases from the model.我有一个简单的神经网络,我需要从模型中获取权重和偏差。 I have tried a few approaches discussed before but I keep getting the out of bounds value error.我已经尝试了一些之前讨论过的方法,但我不断收到越界值错误。 Not sure how to fix this, or what I'm missing.不知道如何解决这个问题,或者我缺少什么。

Network-网络-

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.layers[0].get_weights()[1]

Error - IndexError: list index out of range错误 - IndexError: list index out of range

This is what has been mentioned in a few questions,but I end up getting the out of bounds error for this.这是在几个问题中提到的,但我最终得到了越界错误。

I have another question, the index followed after model.layers[] , does it correspond to the layer?我还有一个问题,索引跟在model.layers[] ,它对应于图层吗? For instance model.layers[1] gives the weights corresponding to the second layer, something like that?例如model.layers[1]给出对应于第二层的权重,类似的东西?

I've been there, I have been looking at my old code to see if I could remember how did I solved that issue.我去过那里,我一直在查看我的旧代码,看看我是否记得我是如何解决这个问题的。 What I did was to print the length of the model.layer[index].get_weights()[X] to figure out where keras was saving the weights I needed.我所做的是打印model.layer[index].get_weights()[X]的长度以找出 keras 在哪里保存我需要的权重。 In my old code, model.layers[0].get_weights()[1] would return the biases, while model.layers[0].get_weights()[0] would return the actual weights.在我的旧代码中, model.layers[0].get_weights()[1]将返回偏差,而model.layers[0].get_weights()[0]将返回实际权重。 In any case, take into account that there are layers which weights aren't saved (as they don't have weights), so if asking for model.layers[0].get_weights()[0] doesn't work, try with model.layers[1].get_weights()[1] , as I'm not sure about flatten layers, but I do know that dense layers should save their weights.在任何情况下,请考虑到有些层没有保存权重(因为它们没有权重),所以如果要求model.layers[0].get_weights()[0]不起作用,请尝试使用model.layers[1].get_weights()[1] ,因为我不确定扁平层,但我知道密集层应该保存它们的权重。

The first layer (index 0) in your model is a Flatten layer, which does not have any weights, that's why you get errors.模型中的第一层(索引 0)是一个Flatten层,它没有任何权重,这就是您得到错误的原因。

To get the Dense layer, which is the second layer, you have to use index 1:要获得Dense层,即第二层,您必须使用索引 1:

model.layers[1].get_weights()[1]

只需model.get_weights() ,您将获得模型的所有权重和偏差

To get the weights and bias on a Keras sequential and for every iteration, you can do it as in the next example:要获得 Keras 序列和每次迭代的权重和偏差,您可以按照下一个示例进行操作:

# create model
model = Sequential()
model.add(Dense(numHiddenNeurons, activation="tanh", input_dim=4, kernel_initializer="uniform"))
model.add(Dense(1, activation="linear", kernel_initializer="uniform"))
# Compile model
model.compile(loss='mse', optimizer='adam', metrics=['accuracy', 'mse', 'mae', 'mape'])
weightsBiasDict = {}
    
weightAndBiasCallback = tf.keras.callbacks.LambdaCallback \
            (on_epoch_end=lambda epoch, logs: weightsBiasDict.update({epoch:model.get_weights()}))
    
# Fit the model
history= model.fit(X1, Y1, epochs=numIterations, batch_size=batch_size,  verbose=0, callbacks=weightAndBiasCallback)

weights and bias are accessible for every iteration on the dictionary weightsBiasDict字典weightsBiasDict上的每次迭代都可以访问权重和偏差

If you just need weights and bias values at the end of the training you can use model.layer[index].get_weights()[0] for weights and model.layer[index].get_weights()[1] for biases where index is the layer number on your network, starting at zero for the input layer.如果您在训练结束时只需要权重和偏差值,您可以使用model.layer[index].get_weights()[0]表示权重,使用model.layer[index].get_weights()[1]表示偏差,其中index是网络上的层编号,输入层从零开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM