我在Keras中有一个模型,如下所示:
data = Input(shape=input_shape)
# 512 x 640 x 3
pad1 = TimeDistributed(ZeroPadding2D(padding=(100, 100)))(data)
# 712 x 840 x 3
conv1_1 = TimeDistributed(Conv2D(8, (3,3), padding="valid", activation="relu", name="block1_conv1", data_format="channels_last"))(pad1)
conv1_2 = TimeDistributed(Conv2D(8, (3,3), padding="same", activation="relu", name="block1_conv2", data_format="channels_last"))(conv1_1)
pool1 = TimeDistributed(MaxPooling2D((2,2), strides=(2,2), padding="same", name="block1_pool", data_format="channels_last"))(conv1_2)
我希望能够将conv1_1和conv1_2的可训练权重参数设置为每个时间步长的预训练值。我可以这样做吗? Keras似乎将这些层视为具有自己可训练参数的自身实体,而不是视为具有相同共享可训练权重的Conv2D函数的集合。有办法改变吗?如何访问单个时间片的可训练权重并将其分配到所有时间片?
答案 0 :(得分:1)
您可以这样做:
data = Input(shape=input_shape)
# 512 x 640 x 3
pad1 = TimeDistributed(ZeroPadding2D(padding=(100, 100)))(data)
# 712 x 840 x 3
nd_conv1_1 = Conv2D(8, (3,3), padding="valid", activation="relu", name="block1_conv1", data_format="channels_last")
nd_conv1_2 = Conv2D(8, (3,3), padding="same", activation="relu", name="block1_conv2", data_format="channels_last")
conv1_1 = TimeDistributed(nd_conv1_1)(pad1)
conv1_2 = TimeDistributed(nd_conv1_2)(conv1_1)
pool1 = TimeDistributed(MaxPooling2D((2,2), strides=(2,2), padding="same", name="block1_pool", data_format="channels_last"))(conv1_2)
nd_conv1_1.trainable = True/False
nd_conv1_2.trainable = True/False