Home > Blockchain >  Concat tensor of[None, 192] with tensor of [1,128]
Concat tensor of[None, 192] with tensor of [1,128]

Time:11-15

How to concatenate tensors of shapes [None, 128] with tensor of [1,128]. Here the first tensor will some data of unknown length and the second tensor is fixed tensor not dependant on data size. The final output should be of shape[None, 328]. This is a part of a neural network concatenation.

I tried

> c = Concatenate(axis = -1, name = 'DQN_Input')([ a, b])

Here a.shape = (None, 192) and b.shape = (1,128) But this does not work. The error is

ValueError: A Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 192), (1, 128)]

CodePudding user response:

What you can do is use tf.repeat on b based on the first dimension of a to generate the same shape tensor. Here is a simple working example:

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])
  • Related