简体   繁体   English

如何在 Keras 功能 API 中创建具有多个共享层的 model?

[英]How to create a model with multiple shared layers in Keras Functional API?

I would like to have a model with 2 inputs, several hidden layers with shared weights, then separate output layers.我想要一个带有 2 个输入的 model,几个具有共享权重的隐藏层,然后分离 output 层。

I have seen this question and its accepted answer: Share weights between two dense layers in keras .我已经看到了这个问题及其公认的答案: 在 keras 中的两个密集层之间共享权重 This is exactly what I would want to achieve, just with multiple shared dense layers.这正是我想要实现的,只需使用多个共享的密集层。

Basically, this is what they do:基本上,这就是他们所做的:
(I modified it a little to have 2 separate output layers) (我对其进行了一些修改,使其具有 2 个单独的 output 层)

ip_shape1 = tf.keras.layers.Input(shape=(5,))
ip_shape2 = tf.keras.layers.Input(shape=(5,))

dense = tf.keras.layers.Dense(1, activation="sigmoid", kernel_initializer="ones")

op1 = dense(ip_shape1)
op2 = dense(ip_shape2)

op1 = tf.keras.layers.Dense(1,activation=tf.nn.sigmoid)(op1)
op2 = tf.keras.layers.Dense(1,activation=tf.nn.sigmoid)(op2)

model = tf.keras.models.Model(inputs=[ip_shape1, ip_shape2], outputs=[op1,op2])

I would like to do the same, just with 2 shared hidden layers:我也想做同样的事情,只需要 2 个共享隐藏层:

ip_shape1 = tf.keras.layers.Input(shape=(5,))
ip_shape2 = tf.keras.layers.Input(shape=(5,))

dense = tf.keras.layers.Dense(1, activation="sigmoid", kernel_initializer="ones", input_shape=(5,))
dense = tf.keras.layers.Dense(1, activation="sigmoid", kernel_initializer="ones")(dense)

op1 = dense(ip_shape1)
op2 = dense(ip_shape2)

op1 = tf.keras.layers.Dense(1,activation=tf.nn.sigmoid)(op1)
op2 = tf.keras.layers.Dense(1,activation=tf.nn.sigmoid)(op2)

model = tf.keras.models.Model(inputs=[ip_shape1, ip_shape2], outputs=[op1,op2])

But when I try to do this, I get an error:但是当我尝试这样做时,我得到一个错误:

TypeError: Inputs to a layer should be tensors. Got: <tensorflow.python.keras.layers.core.Dense object at 0x7f7286dc7c70>

The error occurs in the following line:错误发生在以下行:

dense = tf.keras.layers.Dense(1, activation="sigmoid", kernel_initializer="ones")(dense)

Essentially, you are calling the dense layer with dense (another Keras layer).本质上,您正在使用dense (另一个 Keras 层)调用密集层。 Instead, the layer tf.keras.layers.Dense expects a tensor as input.相反,层tf.keras.layers.Dense需要一个张量作为输入。


I presume that you want to compose two shared dense layers.我假设你想组成两个共享的密集层。 This can be achieved as follows:这可以通过以下方式实现:

dense_1 = tf.keras.layers.Dense(1, activation="sigmoid", kernel_initializer="ones", input_shape=(5,))
dense_2 = tf.keras.layers.Dense(1, activation="sigmoid", kernel_initializer="ones")

op1 = dense_1(ip_shape1)
op1 = dense_2(op1)

op2 = dense_1(ip_shape2)
op2 = dense_2(op2)

NOTE: Not tested.注意:未经测试。

暂无
暂无

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

相关问题 如何在keras的功能性api中向预训练模型的底部添加图层? - How to add layers to the bottom of a pretrained model in keras' functional api? keras功能api和多个合并层 - keras functional api and multiple merge layers 如何重用keras函数模型的层 - How to reuse the layers of keras functional model 如何使用功能 API 模型实现 CNN 并解决 keras 层中的“_keras_shape”错误 - How do I implement CNN using Functional API model and resolve '_keras_shape' error in keras layers 如何使用预训练的 model 的第一层来提取 Keras model 内部的特征(功能 API) - How to use the first layers of a pretrained model to extract features inside a Keras model (Functional API) 将层列表添加到 keras 模型计算图(功能 API) - Adding a list of layers to a keras model computation graph (functional API) 如何在训练 Keras 功能 API model 时打印不同激活层的准确性? (张量流Python) - How to print accuracy of different activation layers while training a Keras functional API model? (Tensorflow Python) keras 功能 API model 仅传递.network 的输入和输出时如何知道层 - How does the keras functional API model knows about layers when are only passing inputs and outputs of the network 通过for循环使用功能api创建keras输入层? - create keras input layers using the functional api through a for loop? 如何使用功能 keras API 在预训练的非序列 model 中的激活层之后插入丢失层? - How to insert dropout layers after activation layers in a pre-trained non-sequential model using functional keras API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM