For example, I want to initialize a layer of shape (2,3) with some weights:
hardcodedWeights = np.array([[1,2,3],[4,5,6]])
model = Sequential()
model.add(Dense(output_dim=3, input_dim=2, weights=[hardcodedWeights]))
This throws AssertionError: Provided weight array does not match layer weights (2 layer params vs 1 provided weights)
How should I correctly specify weights?
A Dense
has 2 weight arrays: the main weight matrix, and a bias vector. Therefore you have to provide a list of 2 numpy arrays as the weights
argument. I believe the error is explicit about this.
Besides, the shapes of the arrays you provide should match expected shapes.
If you have doubts about what these shapes are, you can simply instantiate your layer then call get_weights()
, and look at the output. The argument weights
, and also the method set_weights(weights)
, expect exactly the same format as the output of get_weights
.
I also want to init the weights in Convolution2D ,such as set the weights=0.05. But the weight format is the list of numpy array.I don't know how to convert the 0.05 to the right format. Thanks.
Convolution2D has weight
in shape (nb_filter, nb_channel, nb_row, nb_col)
and bias
in shape (nb_filter,)
. Try this:
weights = numpy.empty(shape)
weights.fill(0.05)
What is the new format for saved weights for Convolution2D in keras 2?
If you mean shape, Keras 2's Conv2D has weights:
kernel: self.kernel_size + (input_dim, self.filters)
bias: (self.filters,)
weights: [kernel, bias]
Thanks.
Hi,
I'm writing a custom layer on top of the Conv2D layer. I want to initialize the filter weights of my layer using a tensor. How can I do it?
I find the following method used in weight initialization:
self.kernel = self.add_weight(shape=kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
But there's no way to feed a custom tensor to it. Please help.
@yashkant
In the end of document:
from keras import backend as K
def my_init(shape, dtype=None):
return K.random_normal(shape, dtype=dtype)
model.add(Dense(64, kernel_initializer=my_init))
You can change it to directly return a predefined tensor.
Most helpful comment
A
Dense
has 2 weight arrays: the main weight matrix, and a bias vector. Therefore you have to provide a list of 2 numpy arrays as theweights
argument. I believe the error is explicit about this.Besides, the shapes of the arrays you provide should match expected shapes.
If you have doubts about what these shapes are, you can simply instantiate your layer then call
get_weights()
, and look at the output. The argumentweights
, and also the methodset_weights(weights)
, expect exactly the same format as the output ofget_weights
.