我在keras中做cross_modality_pretrain。 我使用InceptionResNetv2预训练模型,得到conv2d_1的权重,其形状为(3 3 3 32)(高度,宽度,通道,输出)
model_base = InceptionResNetV2(include_top=False, weights='imagenet')
weight = model_base.get_weights()
weight_conv2d_1 = weight[0]
weight_conv2d_1 = np.mean(weight_conv2d_1, axis=2, keepdims=True)
我将其改为(3 3 20 32)
for i in range(20):
if i == 0:
weight_change = np.concatenate((weight_conv2d_1,), axis=2)
else:
weight_change = np.concatenate((weight_change, weight_conv2d_1), axis=2)
现在,我想设置新的重量,
weight[0] = weight_change
model_base.set_weights(weight)
但是,我得到错误:
ValueError: Cannot feed value of shape (3, 3, 10, 32) for Tensor u'Placeholder:0', which has shape '(3, 3, 3, 32)'
我该如何解决?谢谢!
答案 0 :(得分:0)
首先,错误是set_weight
输入应具有相同的get_weight
形状,但它们是(3 3 3 32)和(3 3 20 32)。
其次,我尝试通过更改模型配置参数model.weights()
来解决此问题,将模型配置参数从(3 3 3 32)更改为(3 3 20 32)。 model.weights()
返回list[tf.Variable < shape (3 3 3 32)>]
。在python中,类型list
是可变的,但是我失败了,我无法改变它。我怎么解决呢?谢谢!
最后,我通过model.get_config
解决问题以获取配置参数,更改它并通过Model.from_config()
重建新模型。