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

发布于 2025-01-15 18:08:20 字数 1238 浏览 3 评论 0原文

我的目标是获取 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'对attr'T'有约束,不在NodeDef'[N = 0,Tidx = DT_INT32]'中,KernelDef:'op:“ConcatV2”device_type:“ CPU”约束{名称:“T”allowed_values {列表{类型:DT_QINT32} host_memory_arg: "axis"' [Op:ConcatV2] name: concat

或类似的东西

tensorflow.python.framework.errors_impl.InvalidArgumentError: 输入重塑是一个具有 120 个值的张量,但请求的shape 有 2 个 [Op:Reshape]

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

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

ValueError:无法将具有混合类型的 Python 序列转换为张量。

我现在尝试了

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

,但仍然收到错误

是否有某种方法可以做到这一点,而无需制作一个 hacky 循环来遍历形状的 n 维?

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).

tf.concat(list, -1) does not work. It returns shape(1, 2, ..., n-1*n), which is understandable.

tf.concat(list, 0) does not work. It return shape(1*2, ..., n) which I do not want. I tried to take this intermediate and use features = tf.reshape(f, [len(list)]), but I get one of two exceptions.

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.

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2025-01-22 18:08:20

看起来很老套,但这很有效

        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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文