简体   繁体   English

在Keras顺序模型中,如何用我自己的权重初始化第二个conv层?

[英]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: 我已经成功为conv_1图层设置了新的权重,但是在设置conv_2图层的权重conv_2收到错误消息:

    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) . 在这里, w1.shape == (3, 3, 1, 8)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的值重复八次,以获得形状为(3,3,8,6)的数组:

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

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

相关问题 如何在Keras顺序模型上设置自定义权重? - How to set custom weights on a Keras Sequential Model? 弄清楚如何为自己的数据集在Keras的Conv2D层中定义input_shape - Trouble figuring out how to define the input_shape in the Conv2D layer in Keras for my own dataset Keras:如何在顺序模型中获取图层形状 - Keras: How to get layer shapes in a Sequential model 在Keras Sequential模型中,Conv2D似乎要求内核比前一层更厚。 为什么? - In a Keras Sequential model, Conv2D seems to require the kernel be narrower than the previous layer is thick. Why? 如何将 Conv2d 层 output 作为 Keras model 的输入? - How to feed a Conv2d layer output as input for a Keras model? Keras Sequential 模型输入层 - Keras Sequential model input layer 如何在自定义 Keras 模型函数中共享层权重 - How to share layer weights in custom Keras model function 如何删除keras子类模型中的最后一层但保留权重? - How to remove last layer in keras subclass model but keep weights? 如何为每个输入获取 keras model 中层的权重 - How to get the weights of layer in a keras model for each input 如何在 tensorflow/keras 中使用预定的内核列表初始化 Conv2D 层? - How to initialize a Conv2D layer with predetermined list of kernels in tensorflow/keras?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM