当前位置: 首页 > 知识库问答 >
问题:

如何在keras中连接两个层?

袁志专
2023-03-14

我有一个两层神经网络的例子。第一层接受两个参数并有一个输出。第二个应作为第一层的结果使用一个参数和一个附加参数。应该是这样的:

x1  x2  x3
 \  /   /
  y1   /
   \  /
    y2

因此,我创建了一个具有两个层的模型,并尝试将它们合并,但它返回了一个错误:<代码>顺序模型中的第一层必须获得“input\u shape”或“batch\u input\u shape”参数 在线<代码>结果。添加(合并)。

型号:

first = Sequential()
first.add(Dense(1, input_shape=(2,), activation='sigmoid'))

second = Sequential()
second.add(Dense(1, input_shape=(1,), activation='sigmoid'))

result = Sequential()
merged = Concatenate([first, second])
ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
result.add(merged)
result.compile(optimizer=ada_grad, loss=_loss_tensor, metrics=['accuracy'])

共有3个答案

颛孙俊
2023-03-14

您可以使用模型进行实验。summary()(注意concatenate\u XX(concatenate)层的大小)

# merge samples, two input must be same shape
inp1 = Input(shape=(10,32))
inp2 = Input(shape=(10,32))
cc1 = concatenate([inp1, inp2],axis=0) # Merge data must same row column
output = Dense(30, activation='relu')(cc1)
model = Model(inputs=[inp1, inp2], outputs=output)
model.summary()

# merge row must same column size
inp1 = Input(shape=(20,10))
inp2 = Input(shape=(32,10))
cc1 = concatenate([inp1, inp2],axis=1)
output = Dense(30, activation='relu')(cc1)
model = Model(inputs=[inp1, inp2], outputs=output)
model.summary()

# merge column must same row size
inp1 = Input(shape=(10,20))
inp2 = Input(shape=(10,32))
cc1 = concatenate([inp1, inp2],axis=1)
output = Dense(30, activation='relu')(cc1)
model = Model(inputs=[inp1, inp2], outputs=output)
model.summary()

您可以在此处查看笔记本的详细信息:https://nbviewer.jupyter.org/github/anhhh11/DeepLearning/blob/master/Concanate_two_layer_keras.ipynb

班昱
2023-03-14

添加上述公认答案,以帮助使用tensorflow 2.0的用户


import tensorflow as tf

# some data
c1 = tf.constant([[1, 1, 1], [2, 2, 2]], dtype=tf.float32)
c2 = tf.constant([[2, 2, 2], [3, 3, 3]], dtype=tf.float32)
c3 = tf.constant([[3, 3, 3], [4, 4, 4]], dtype=tf.float32)

# bake layers x1, x2, x3
x1 = tf.keras.layers.Dense(10)(c1)
x2 = tf.keras.layers.Dense(10)(c2)
x3 = tf.keras.layers.Dense(10)(c3)

# merged layer y1
y1 = tf.keras.layers.Concatenate(axis=1)([x1, x2])

# merged layer y2
y2 = tf.keras.layers.Concatenate(axis=1)([y1, x3])

# print info
print("-"*30)
print("x1", x1.shape, "x2", x2.shape, "x3", x3.shape)
print("y1", y1.shape)
print("y2", y2.shape)
print("-"*30)

结果:

------------------------------
x1 (2, 10) x2 (2, 10) x3 (2, 10)
y1 (2, 20)
y2 (2, 30)
------------------------------
郜俊健
2023-03-14

之所以出现错误,是因为定义为Sequential()的结果只是模型的容器,而您尚未为其定义输入。

根据您试图构建的内容,设置结果,以获取第三个输入x3。

first = Sequential()
first.add(Dense(1, input_shape=(2,), activation='sigmoid'))

second = Sequential()
second.add(Dense(1, input_shape=(1,), activation='sigmoid'))

third = Sequential()
# of course you must provide the input to result which will be your x3
third.add(Dense(1, input_shape=(1,), activation='sigmoid'))

# lets say you add a few more layers to first and second.
# concatenate them
merged = Concatenate([first, second])

# then concatenate the two outputs

result = Concatenate([merged,  third])

ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)

result.compile(optimizer=ada_grad, loss='binary_crossentropy',
               metrics=['accuracy'])

然而,我构建具有这种类型输入结构的模型的首选方法是使用函数api。

以下是您开始使用的要求的实现:

from keras.models import Model
from keras.layers import Concatenate, Dense, LSTM, Input, concatenate
from keras.optimizers import Adagrad

first_input = Input(shape=(2, ))
first_dense = Dense(1, )(first_input)

second_input = Input(shape=(2, ))
second_dense = Dense(1, )(second_input)

merge_one = concatenate([first_dense, second_dense])

third_input = Input(shape=(1, ))
merge_two = concatenate([merge_one, third_input])

model = Model(inputs=[first_input, second_input, third_input], outputs=merge_two)
ada_grad = Adagrad(lr=0.1, epsilon=1e-08, decay=0.0)
model.compile(optimizer=ada_grad, loss='binary_crossentropy',
               metrics=['accuracy'])

回答评论中的问题:

  1. 结果和合并是如何连接的?假设您的意思是它们是如何连接的。

串联的工作原理如下:

  a        b         c
a b c   g h i    a b c g h i
d e f   j k l    d e f j k l

i、 e行刚刚连接。

 类似资料:
  • 问题内容: 如何在Java中连接两个数组? 问题答案: 使用Apache Commons Lang库 例如: 在Java 8中使用Stream: 或者像这样,使用flatMap: 为此,你必须使用反射:

  • 比如说,我有一个名为“Contact”的表,其中“first\u name”和“last\u name”作为列。基本上,“从联系人c中选择concat(c.firstname,,,c.lastname)作为全名”是我在hibernate中想要做的。 我可以将整个查询放在createQuery中,并获得所需的输出。但是,我不想在hibernate中执行sql查询。我在这里找到了一篇类似的帖子“我们可

  • 例如,我想在单个中组合和的流,因此结果应该是:。换句话说:如果第一个源已耗尽-从第二个源获取元素。我最近的尝试是: 也对datetime进行了类似的尝试,但结果相同。

  • 问题内容: 我有两个相等的大小。列表1由10个名称组成,列表2由其电话号码组成。 我想将姓名和号码合而为一。我该怎么做呢? 问题答案: 您可以用于将第二个列表的元素添加到第一个列表: 编辑: 根据上面的说明(“ 我想要新的Arraylist中具有名称和编号的单个String。 ”),您需要循环浏览第一个列表并将第二个列表中的项目追加到它。 像这样: 如果输入: 你会得到:

  • 问题内容: 我正在尝试连接Java中的字符串。为什么这不起作用? 问题答案: 你可以使用运算符来连接字符串: 被隐式转换为。

  • 这是我制作的hashmap(Ik它不是最好的,也没有太多的逻辑),我想知道如果用户输入像“DVIII”这样的数据,其中“d”的值为“500”,“viii”的值为“8”,然后将它们打印为DVIII=5008,那么如何连接hashmap的值