简体   繁体   English

如何在 tensorflow 中连接两个具有动态形状的张量?

[英]How to concat two tensor with dynamic shape in tensorflow?

I have two tensor I want to contact in tensorflow.我有两个张量我想在 tensorflow 中联系。

So the shape of these tensor are: (Bs,dynamics,n_features)所以这些张量的形状是: (Bs,dynamics,n_features)

The batch_size and n_features are guarantee to be same, so I think this operation is possible. batch_sizen_features保证是相同的,所以我认为这个操作是可能的。

However, I think tensorflow will run a pre-check?但是,我认为 tensorflow 会进行预检查吗? (sorry I am new to tensorflow), tensorflow will check if two tensor with shape (None,None,n_features) could be concatenated in axis 1, which is not possible, because it doesn't know what is the result of None+None (对不起,我是 tensorflow 的新手),tensorflow 将检查两个形状为(None,None,n_features)的张量是否可以在轴 1 上连接,这是不可能的,因为它不知道None+None的结果是什么

Anybody has any idea to this?有人对此有任何想法吗?

Concatenating should not be a problem in your case:在您的情况下,连接应该不是问题:

import tensorflow as tf

x1 = tf.random.normal((5, 10, 20))
x2 = tf.random.normal((5, 33, 20))

@tf.function
def concatenate(x1, x2):
  return tf.concat([x1, x2], axis=1)

concatenate(x1, x2).shape

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM