简体   繁体   中英

How to Initialize with my own weights for the second conv layer in a Keras sequential model?

I have a model which has two convolutional layers. I have set new weights for conv_1 layer successfully but while setting the weights fo conv_2 layer I am getting an error message:

    model.add(Conv2D(8, (3, 3), input_shape=(28,28,1), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Conv2D(6, (3, 3), input_shape=(26,26,1), activation='relu'))

    model.layers[0].set_weights(w1)
    model.layers[2].set_weights(w2)

Here, w1.shape == (3, 3, 1, 8) and w2.shape == (3, 3, 1, 6) . The error message is:

ValueError: Layer weight shape (3, 3, 8, 6) not compatible with provided weight shape (3, 3, 1, 6)

I am not understanding why it is not setting the weights?

As I mentioned in the comments section one alternative is to use the same weights for all the channels in a filter. To do so, you can easily repeat the values of w2 eight times to get an array of shape (3,3,8,6) :

w2 = w2.repeat(8,axis=2)
w2.shape
# (3,3,8,6)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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