简体   繁体   English

如何使用来自一个预先训练的MLP的最后一个隐藏层权重作为Keras的新MLP(转移学习)的输入?

[英]How to use the last hidden layer weights from one pre-trained MLP as input to a new MLP (transfer learning) with Keras?

I want to do transfer learning with simple MLP models. 我想使用简单的MLP模型进行迁移学习。 First I train a 1 hidden layer feed forward network on large data: 首先,我在大数据上训练了1个隐藏层前馈网络:

net = Sequential()
net.add(Dense(500, input_dim=2048, kernel_initializer='normal', activation='relu'))
net.add(Dense(1, kernel_initializer='normal'))
net.compile(loss='mean_absolute_error', optimizer='adam')
net.fit(x_transf, 
        y_transf,
        epochs=1000, 
        batch_size=8, 
        verbose=0)

Then I want to pass the unique hidden layer as input to a new network, in which I want to add a second layer. 然后,我想将唯一的隐藏层作为输入传递到新网络,在其中我要添加第二层。 The re-used layer should not be trainable. 重用的层不应是可训练的。

idx = 1  # index of desired layer
input_shape = net.layers[idx].get_input_shape_at(0) # get the input shape of desired layer
input_layer = net.layers[idx]
input_layer.trainable = False

transf_model = Sequential()
transf_model.add(input_layer)
transf_model.add(Dense(input_shape[1], activation='relu'))
transf_model.compile(loss='mean_absolute_error', optimizer='adam')
transf_model.fit(x, 
                 y,
                 epochs=10, 
                 batch_size=8, 
                 verbose=0)

EDIT: The above code returns: 编辑:上面的代码返回:

ValueError: Error when checking target: expected dense_9 to have shape (None, 500) but got array with shape (436, 1)

What's the trick to make this work? 使这项工作有效的诀窍是什么?

I would simply use Functional API to build such a model: 我将只使用Functional API来构建这样的模型:

shared_layer = net.layers[0] # you want the first layer, so index = 0
shared_layer.trainable = False

inp = Input(the_shape_of_one_input_sample) # e.g. (2048,)
x = shared_layer(inp)
x = Dense(800, ...)(x)
out = Dense(1, ...)(x)

model = Model(inp, out)

# the rest is the same...

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

相关问题 如何使用现有CNN模型中的预训练权重在Keras中进行迁移学习? - How can I use pre-trained weights from an existing CNN model for transfer learning in Keras? 如何使用预训练的 model 进行双输入迁移学习 - how to use pre-trained model for dual input transfer learning 如何使用预先训练的权重为 keras 中的特定通道设置权重? - How to set weights for specific channels in keras with pre-trained weights? 从预训练的 model 中移除顶层,迁移学习,tensorflow (load_model) - Remove top layer from pre-trained model, transfer learning, tensorflow (load_model) 在迁移学习预训练模型上训练新数据集 - Train new dataset on transfer learning pre-trained model Keras MLP分类器不学习 - Keras MLP classifier not learning 如何在 Keras 中更改预训练 CNN model 中层的 output? - How to change output of a layer in a pre-trained CNN model in Keras? keras 预训练 model 馈送新的输入占位符 - keras pre-trained model feed a new input placeholder 如何从预训练的词嵌入数据集创建 Keras 嵌入层? - How do I create a Keras Embedding layer from a pre-trained word embedding dataset? 如何使用Keras API提取“从输入层到隐藏层”和“从隐藏层到输出层”的权重? - How to extract weights “from input layer to hidden layer” and “from hidden layer to output layer” with Keras API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM