将 [None, 192] 的张量与 [1,128] 的张量连接起来
Concat tensor of[None, 192] with tensor of [1,128]
如何将形状为 [None, 128] 的张量与 [1,128] 张量连接起来。这里第一个张量将一些未知长度的数据,第二个张量是固定张量,不依赖于数据大小。最终输出的形状应为[None, 328]。这是神经网络串联的一部分。
我试过了
> c = Concatenate(axis = -1, name = 'DQN_Input')([ a, b])
这里 a.shape = (None, 192) 和 b.shape = (1,128)
但这不起作用。
错误是
ValueError: A Concatenate
layer requires inputs with matching
shapes except for the concat axis. Got inputs shapes: [(None, 192),
(1, 128)]
你可以做的是根据a
的第一个维度在b
上使用tf.repeat
来生成相同形状的张量。这是一个简单的工作示例:
import tensorflow as tf
a = tf.keras.layers.Input((192, ), name = 'a')
alpha = tf.keras.layers.Input((1,),name = 'Alpha')
b = tf.matmul(alpha, a, transpose_a=True)
b = tf.repeat(b, repeats=tf.shape(a)[0], axis=0)
c = tf.keras.layers.Concatenate(axis = -1, name = 'DQN_Input')([ a, b])
model = tf.keras.Model([a, alpha], c)
tf.print(model((tf.random.normal((5, 192)), tf.random.normal((5, 1)))).shape)
TensorShape([5, 384])
如何将形状为 [None, 128] 的张量与 [1,128] 张量连接起来。这里第一个张量将一些未知长度的数据,第二个张量是固定张量,不依赖于数据大小。最终输出的形状应为[None, 328]。这是神经网络串联的一部分。
我试过了
> c = Concatenate(axis = -1, name = 'DQN_Input')([ a, b])
这里 a.shape = (None, 192) 和 b.shape = (1,128) 但这不起作用。 错误是
ValueError: A
Concatenate
layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 192), (1, 128)]
你可以做的是根据a
的第一个维度在b
上使用tf.repeat
来生成相同形状的张量。这是一个简单的工作示例:
import tensorflow as tf
a = tf.keras.layers.Input((192, ), name = 'a')
alpha = tf.keras.layers.Input((1,),name = 'Alpha')
b = tf.matmul(alpha, a, transpose_a=True)
b = tf.repeat(b, repeats=tf.shape(a)[0], axis=0)
c = tf.keras.layers.Concatenate(axis = -1, name = 'DQN_Input')([ a, b])
model = tf.keras.Model([a, alpha], c)
tf.print(model((tf.random.normal((5, 192)), tf.random.normal((5, 1)))).shape)
TensorShape([5, 384])