简体   繁体   English

如何沿轴 0 连接 tensorflow 个张量,同时保留其他 n>0 维度的形状

[英]How do you concatenate tensorflow tensors along axis 0 while preserving the shape of the other n>0 dimensions

My goal is to take a list of tensors of shape(1, 2, ...n) and concatenate them into a tensor of shape(len(list), 1, 2, ..., n) .我的目标是获取shape(1, 2, ...n)的张量列表,并将它们连接成shape(len(list), 1, 2, ..., n)的张量。

tf.concat(list, -1) does not work. tf.concat(list, -1)不起作用。 It returns shape(1, 2, ..., n-1*n) , which is understandable.它返回shape(1, 2, ..., n-1*n) ,这是可以理解的。

tf.concat(list, 0) does not work. tf.concat(list, 0)不起作用。 It return shape(1*2, ..., n) which I do not want.它返回我不想要的shape(1*2, ..., n) I tried to take this intermediate and use features = tf.reshape(f, [len(list)]) , but I get one of two exceptions.我尝试使用这个中间体并使用features = tf.reshape(f, [len(list)]) ,但我得到了两个例外之一。

tensorflow.python.framework.errors_impl.InvalidArgumentError: OpKernel 'ConcatV2' has constraint on attr 'T' not in NodeDef '[N=0, Tidx=DT_INT32]', KernelDef: 'op: "ConcatV2" device_type: "CPU" constraint { name: "T" allowed_values { list { type: DT_QINT32 } } } host_memory_arg: "axis"' [Op:ConcatV2] name: concat

or something like this或类似的东西

tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 120 values, but the requested shape has 2 [Op:Reshape]

I have tried using features = tf.reshape(f, [len(list), -1]) and get shape(len(list), 1, 2, n-1*n) which is also wrong, but understandable.我试过使用features = tf.reshape(f, [len(list), -1])和 get shape(len(list), 1, 2, n-1*n)这也是错误的,但可以理解。

Only other thing I can think of is copying the shape like this, tf.shape([len(list), list[0].shape]) , but that leads to error我唯一能想到的就是复制这样的形状, tf.shape([len(list), list[0].shape]) ,但这会导致错误

ValueError: Can't convert Python sequence with mixed types to Tensor.

I now tried我现在试过了

        f = tf.concat(list, 0)
        f = tf.expand_dims(f, 0)
        features = tf.reshape(f, [len(list)])

and still get an error仍然出错

Is there some way to do this without making a hacky loop to go through the n dimensions of the shape?有没有办法做到这一点,而无需通过形状的 n 个维度对 go 进行 hacky 循环?

Seems hacky, but this works看起来很老套,但这行得通

        if time_features is not None:
            s = [len(time_features)]
            for i in time_features[0].shape[:]:
                s.append(i)
            f = tf.concat(time_features, 0)
            features = tf.reshape(f, s)

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

相关问题 如何沿给定轴连接张量? - How do I concatenate tensors along a given axis? TensorFlow串联/堆叠N张量交错最后一个维度 - TensorFlow concatenate/stack N tensors interleaving last dimensions 如何在张量流的二维卷积中连接两个不同形状的张量? - How to concatenate two tensors with different shape in 2d convolution in tensorflow? 如何用TensorFlow连接两个具有不同形状的张量? - How to concatenate two tensors having different shape with TensorFlow? 你如何有效地处理张量的维度? - How do you efficiently work with the dimensions of tensors? Tensorflow:沿轴的堆叠(m,n,k,p)和(m,n,1,p)张量= 2 - Tensorflow: stack (m, n, k, p) and (m, n, 1, p) tensors along the axis=2 如何连接具有 2 个不同维度的 2d 张量 - How to concatenate 2d tensors with 2 different dimensions 一个如何在张量流中沿宽度和高度尺寸交错4D张量? - How does one interleave 4D tensors along the width & height dimensions in tensorflow? 当它们转换为Numpy数组时,如何访问Tiff中的Z轴? 形状只有二维 - How do you access the Z axis in Tiff's when they are converted to a numpy array? The shape is only in 2 dimensions 连接循环内生成的 N 个 pytorch 张量(形状相同) - Concatenate N pytorch tensors (of the same shape) generated from within loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM