Search code examples
keras

How to set bias value of a particular layer in Keras


I was trying to set bias value of a particular layer in Keras, but I didn't find any way to do that.

For example weight can be set by following code:

model.layers[-1].set_weights(weights)

Is there any way to set bias, the way weights can be set (like above)?

Can anyone help me in this regard?


Solution

  • You can use K.set_value for this :
    K.set_value(model.layers[-1].weights[1], np.ones((bias_dim,)))

    Note that when you use set_weights, you have to provide a list containing both weights and biais, so if you would only want to set the weights without setting the biais, you could also use K.set_value.