为keras TimeDistributed图层按名称设置权重

问题描述 投票:0回答:1

我在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函数的集合。有没有办法改变这个?如何访问单个时间片的可训练权重并将其分配给所有时间片?

python tensorflow keras
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.