添加到上面接受的答案中,以便对使用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)
------------------------------
0
我有一个具有两层的神经网络的示例。第一层有两个参数,并有一个输出。第二个参数应接受一个参数作为第一层的结果,并附加一个参数。它看起来应该像这样:
因此,我创建了一个具有两层的模型并尝试将它们合并,但是它返回了一个错误:
The first layer in a Sequential model must get an "input_shape" or "batch_input_shape" argument.
在线result.add(merged)
。模型: