【发布时间】:2016-12-23 10:19:01
【问题描述】:
Torch 有一个函数nn.DepthConcat,它与nn.Concat 类似,除了它用零填充以使所有非通道暗淡的大小相同。我一直试图在 tensorflow 中实现这一点,但运气不佳。如果我在图形构建时知道所有张量的大小,这似乎可行:
def depthconcat(inputs):
concat_dim = 3
shapes = []
for input_ in inputs:
shapes.append(input_.get_shape())
shape_tensor = tf.pack(shapes)
max_dims = tf.reduce_max(shape_tensor, 0)
padded_inputs = []
for input_ in inputs:
paddings = max_dims - input_.get_shape()
padded_inputs.append(tf.pad(input_, paddings))
return tf.concat(concat_dim, padded_inputs)
但是,如果形状是在运行时确定的,我会收到以下错误:
Tensors in list passed to 'values' of 'Pack' Op have types [<NOT CONVERTIBLE TO TENSOR>, <NOT CONVERTIBLE TO TENSOR>, <NOT CONVERTIBLE TO TENSOR>, <NOT CONVERTIBLE TO TENSOR>] that don't all match.
如果TensorShape 对象在图形构建时完全定义,它似乎能够将其转换为张量。有什么建议?谢谢。
编辑:
从input_.get_shape() 更改为tf.shape(input_) 解决了图形创建时形状不明确的问题。现在我得到ValueError: Shape (4,) must have rank 2
【问题讨论】:
标签: tensorflow torch