【问题标题】:What is the alternative of numpy.newaxis in tensorflow?张量流中 numpy.newaxis 的替代方案是什么?
【发布时间】:2017-07-09 16:54:27
【问题描述】:

您好,我是 tensorflow 的新手。我想在tensorflow中实现下面的python代码。

import numpy as np
a = np.array([1,2,3,4,5,6,7,9,0])
print(a) ## [1 2 3 4 5 6 7 9 0]
print(a.shape) ## (9,)
b = a[:, np.newaxis] ### want to write this in tensorflow.
print(b.shape) ## (9,1)

【问题讨论】:

    标签: python python-3.x numpy tensorflow tensor


    【解决方案1】:

    我想应该是tf.expand_dims -

    tf.expand_dims(a, 1) # Or tf.expand_dims(a, -1)
    

    基本上,我们列出了要插入这个新轴的轴 ID,以及尾随轴/尺寸后推

    从链接的文档中,这里有几个扩展维度的示例 -

    # 't' is a tensor of shape [2]
    shape(expand_dims(t, 0)) ==> [1, 2]
    shape(expand_dims(t, 1)) ==> [2, 1]
    shape(expand_dims(t, -1)) ==> [2, 1]
    
    # 't2' is a tensor of shape [2, 3, 5]
    shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
    shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
    shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]
    

    【讨论】:

      【解决方案2】:

      对应的命令是tf.newaxis(或None,如numpy)。它在tensorflow的文档中没有自己的条目,但在tf.stride_slice的文档页面上简要提及。

      x = tf.ones((10,10,10))
      y = x[:, tf.newaxis] # or y = x [:, None]
      print(y.shape)
      # prints (10, 1, 10, 10)
      

      使用tf.expand_dims 也可以,但是如上面的链接所述,

      这些界面更加友好,强烈推荐。

      【讨论】:

        【解决方案3】:

        如果您对与 NumPy 中完全相同的类型(即None)感兴趣,那么tf.newaxisnp.newaxis 的完全替代品。

        例子:

        In [71]: a1 = tf.constant([2,2], name="a1")
        
        In [72]: a1
        Out[72]: <tf.Tensor 'a1_5:0' shape=(2,) dtype=int32>
        
        # add a new dimension
        In [73]: a1_new = a1[tf.newaxis, :]
        
        In [74]: a1_new
        Out[74]: <tf.Tensor 'strided_slice_5:0' shape=(1, 2) dtype=int32>
        
        # add one more dimension
        In [75]: a1_new = a1[tf.newaxis, :, tf.newaxis]
        
        In [76]: a1_new
        Out[76]: <tf.Tensor 'strided_slice_6:0' shape=(1, 2, 1) dtype=int32>
        

        这与您在 NumPy 中执行的操作完全相同。只需在您希望增加的相同维度上使用它即可。

        【讨论】:

          【解决方案4】:

          考虑tf.keras.layers.Reshape

          # as first layer in a Sequential model
          model = Sequential()
          model.add(Reshape((3, 4), input_shape=(12,)))
          # now: model.output_shape == (None, 3, 4)
          # note: `None` is the batch dimension
          
          # as intermediate layer in a Sequential model
          model.add(Reshape((6, 2)))
          # now: model.output_shape == (None, 6, 2)
          
          # also supports shape inference using `-1` as dimension
          model.add(Reshape((-1, 2, 2)))
          # now: model.output_shape == (None, 3, 2, 2)
          

          【讨论】:

            【解决方案5】:
            a = a[..., tf.newaxis].astype("float32")
            

            这也有效

            【讨论】:

              猜你喜欢
              • 2021-11-13
              • 2018-12-01
              • 1970-01-01
              • 1970-01-01
              • 2018-11-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多