繁体   English   中英

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

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

我的目标是获取shape(1, 2, ...n)的张量列表,并将它们连接成shape(len(list), 1, 2, ..., n)的张量。

tf.concat(list, -1)不起作用。 它返回shape(1, 2, ..., n-1*n) ,这是可以理解的。

tf.concat(list, 0)不起作用。 它返回我不想要的shape(1*2, ..., n) 我尝试使用这个中间体并使用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

或类似的东西

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

我试过使用features = tf.reshape(f, [len(list), -1])和 get shape(len(list), 1, 2, n-1*n)这也是错误的,但可以理解。

我唯一能想到的就是复制这样的形状, tf.shape([len(list), list[0].shape]) ,但这会导致错误

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

我现在试过了

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

仍然出错

有没有办法做到这一点,而无需通过形状的 n 个维度对 go 进行 hacky 循环?

看起来很老套,但这行得通

        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.

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