【问题标题】:what is torch's unsqueeze equivalence with tensorflow?什么是火炬与张量流的解压等价?
【发布时间】:2020-09-28 02:14:38
【问题描述】:

torch 与 tensorflow 的 unsqueeze 等价性是什么?

#tensorflow auto-broadcasts singleton dimensions
lower_bounds = tf.argmax(set_1[:, :2].unsqueeze(1), set_2[:, :2].unsqueeze(0))  # (n1, n2, 2)
upper_bounds = tf.argmin(set_1[:, 2:].unsqueeze(1), set_2[:, 2:].unsqueeze(0))  # (n1, n2, 2)

【问题讨论】:

    标签: tensorflow pytorch tensorflow2.0 torch


    【解决方案1】:

    也许你想试试这个: tf.expand_dims(x, axis)

    【讨论】:

      【解决方案2】:

      tf.expand_dims 就是你要找的。​​p>

      tf.expand_dims(
          input, axis, name=None
      )
      

      给定一个张量输入,此操作在输入形状的维度索引轴处插入一个大小为 1 的维度。维度索引轴从零开始;如果您为轴指定负数,则从末尾向后计数。

      例子

      t = [[1, 2, 3],[4, 5, 6]]       # [2, 3]
      tf.shape(tf.expand_dims(t, 0))  # [1, 2, 3]
      tf.shape(tf.expand_dims(t, 1))  # [2, 1, 3]
      tf.shape(tf.expand_dims(t, -1)) # [2, 3, 1]
      

      【讨论】:

        猜你喜欢
        • 2020-09-28
        • 1970-01-01
        • 2021-07-26
        • 2016-10-30
        • 2022-07-20
        • 2017-02-23
        • 2019-01-19
        • 1970-01-01
        • 2020-11-24
        相关资源
        最近更新 更多