Reputation: 23
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?
Upvotes: 2
Views: 4840
Reputation: 1
In tensorflow .get_weights()
method on layer returns weights and bias of that layer.
To set any layer weight and bias just use .set_weights()
method. for example to set layer_b
weights and bias from layer_a
do as follow:
layer_b.set_weights(layer_a.get_weights())
for reference you can refer set_weights
Upvotes: 0
Reputation: 1
you can do this by setting constraints for biases or weights or both.
in each layer there are bias_constraint and kernel_constraint that they are for biases and weights respectively. Here is the example if you want to set bias value to 1.
model.add(Dense(1 ,bias_constraint=tf.keras.constraints.MinMaxNorm(
min_value=1.0, max_value=1.0, rate=1.0, axis=0), activation='softmax'))
Upvotes: 0
Reputation: 660
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
.
Upvotes: 4