首页 > 解决方案 > 根据图形时间未知的大小拆分张量

问题描述

在张量流中,我想做以下事情:

但是在创建图形时,我不知道每个一维张量的大小,这会产生问题。这是我正在做的一个片段:

    def stack(tensors):
    sizes = tf.convert_to_tensor([t.shape[0].value for t in tensors])
    tensor_stacked = tf.concat(tensors, axis=0)
    res = my_function(tensor_stacked)
    return tf.split(res, sizes, 0)


tensor_A = tf.placeholder(
    tf.int32,
    shape=[None],
    name=None
)

tensor_B = tf.placeholder(
    tf.int32,
    shape=[None],
    name=None
) 

res = stack([tensor_A, tensor_B])

这将在带有消息的“concat”行上失败

TypeError:无法将类型对象转换为张量。内容:[无,无]。考虑将元素转换为支持的类型。

有什么办法可以在 tensorflow 中做到这一点?在图形时间,“大小”变量将始终包含未知大小,因为一维张量的长度永远未知

标签: tensorflow

解决方案


好的,同时我找到了答案

显然它足以取代对 to 的tensor.shape[0]调用tf.shape(tensor)[0]

所以现在我有:

def stack(tensors):
    sizes = tf.convert_to_tensor([tf.shape(t)[0] for t in tensors])
    print(sizes)
    tensor_stacked = tf.concat(tensors, axis=0)
    res = my_function(tensor_stacked)
    return tf.split(res, sizes, 0)

推荐阅读