【问题标题】:tensorflow version of Torch nn.DepthConcatTorch nn.DepthConcat 的张量流版本
【发布时间】: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


    【解决方案1】:

    我希望这对尝试构建具有不同输出大小的初始模块的其他人有所帮助。

    def depthconcat(inputs):
        concat_dim = 3
        shapes = []
        for input_ in inputs:
            shapes.append(tf.to_float(tf.shape(input_)[:3]))
        shape_tensor = tf.pack(shapes)
        max_dims = tf.reduce_max(shape_tensor, 0)
    
        padded_inputs = []
        for idx, input_ in enumerate(inputs):
            mean_diff = (max_dims - shapes[idx])/2.0
            pad_low = tf.floor(mean_diff)
            pad_high = tf.ceil(mean_diff)
            paddings = tf.to_int32(tf.pack([pad_low, pad_high], axis=1))
            paddings = tf.pad(paddings, paddings=[[0, 1], [0, 0]])
            padded_inputs.append(tf.pad(input_, paddings))
    
         return tf.concat(concat_dim, padded_inputs, name=name)
    

    【讨论】:

      猜你喜欢
      • 2016-07-02
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2020-08-27
      • 2015-06-08
      • 2021-11-10
      • 2021-10-07
      相关资源
      最近更新 更多