【发布时间】:2019-06-18 04:27:43
【问题描述】:
我想通过显式定义层的权重矩阵来创建一个 conv2d 层(Conv2D 的 use_bias 参数设置为 False)。我一直在尝试使用 layer.set_weights([K]) 来做到这一点,其中 K 是 (?, 7, 7, 512, 512) 张量。
在简单的 Tensorflow API 中,可以通过将张量传递给 tf.nn.conv2d(input, filter,..) 中的 filter 参数来完成
此外,我还有更多问题,我应该展示我是否应该解决 K 张量中的批处理维度,因为它是由网络生成的
基本上,我想实现一个超网络,在该超网络中,我从 Tensor K 中的另一个网络生成了上面指定的 Conv2D 层的权重。权重 Tensor K 的形状为 [height, width, Filters, Channels]
template= Input(shape=(448,684,3))
hyper_net= VGG16(weights='imagenet', include_top=False,
input_tensor=None, input_shape=(448,684, 3))
k1= hyper_net(template)
kconv1= hyper_net.get_layer(name='block5_conv1')
config_conv1= kconv1.get_config()
k1conv1 = Conv2D.from_config(config_conv1)(k1)
kconv2= hyper_net.get_layer(name='block5_conv2')
config_conv2= kconv2.get_config()
k1conv2 = Conv2D.from_config(config_conv2)(k1conv1)
k1pool1= MaxPooling2D(pool_size=(2,3))(k1conv2)
k1pool1= Reshape((7,7,512,1))(k1pool1)
print(k1pool1.shape)
K= Conv3D(512, (1,1,1), strides=(1, 1, 1), padding='valid',
activation=None, use_bias=True, kernel_initializer='he_normal', bias_initializer='zeros', kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01))(k1pool1)
ortho= Input(tensor=tf.convert_to_tensor(O))
base_model = VGG16(weights='imagenet', include_top=False,
input_tensor=None, input_shape=(1760, 1760, 3))
o1= base_model(ortho)
Oconv1= Conv2D(512, (7, 7), activation='relu', padding='valid',use_bias=False)
Oconv1.set_weights([K])
它给出的错误是:
ValueError: You called `set_weights(weights)` on layer "conv2d_4" with a weight list of length 1, but the layer was expecting 0 weights. Provided weights: [<tf.Tensor 'conv3d_9/add:0' shape=(?, 7, 7, 512, ...
【问题讨论】:
-
K 中的
?是什么。应该是4dims。 -
?表示批次维度,因为 K 张量是网络的输出
-
在定义权重时不需要 batch_size。它是以下暗淡
(kernel, Kernel, input_channels, output_channels)。 -
是的。这就是我尝试处理批处理维度的原因。这样可以将单独的网络(具有batch维度)生成的权重更改为(kernel, Kernel, input_channels, output_channels)
标签: tensorflow machine-learning keras conv-neural-network convolution