将具有相同值的两个张量转换为彼此

时间:2021-09-13 18:02:51

I have two same tensors in terms of values but they are different in terms of shape like these:

我在价值方面有两个相同的张量,但它们的形状不同,如下所示:

output_image1 = 
[[[[3. 1.]
   [2. 7.]]

  [[5. 4.]
   [9. 8.]]]


 [[[3. 3.]
   [1. 4.]]

  [[6. 5.]
   [7. 2.]]]]
output_image2 =
[[[[3]
   [1]
   [5]
   [4]]

  [[2]
   [7]
   [9]
   [8]]

  [[3]
   [3]
   [6]
   [5]]

  [[1]
   [4]
   [7]
   [2]]]]

output_image1.shape =  (2, 2, 2, 2)
output_image2.shape =  (1, 4, 4, 1)

How can I change shape of the image1 into the image2 with the same values. I mean from (2, 2, 2, 2) --> (1, 4, 4, 1) and having the same values like image2.

如何将image1的形状更改为具有相同值的image2。我的意思是从(2,2,2,2) - >(1,4,4,1)并且具有与image2相同的值。

2 个解决方案

#1


0  

This needs a transpose plus reshaping

这需要转置加重塑

It's easier to generate this than your image, but I think I got the mapping correct:

生成这个比你的图像更容易,但我认为我的映射是正确的:

In [154]: arr = np.arange(16).reshape(2,2,2,2)
In [155]: arr
Out[155]: 
array([[[[ 0,  1],
         [ 2,  3]],

        [[ 4,  5],
         [ 6,  7]]],


       [[[ 8,  9],
         [10, 11]],

        [[12, 13],
         [14, 15]]]])

A reshape(4,4) would create [0,1,2,3], etc, but you want [0,1,4,5], so we need to swap the middle axes. Order of 1st and last axes is unchanged.

重塑(4,4)会创建[0,1,2,3]等,但是你需要[0,1,4,5],所以我们需要交换中轴。第1轴和最后轴的顺序不变。

In [156]: arr.transpose(0,2,1,3)
Out[156]: 
array([[[[ 0,  1],
         [ 4,  5]],

        [[ 2,  3],
         [ 6,  7]]],


       [[[ 8,  9],
         [12, 13]],

        [[10, 11],
         [14, 15]]]])

Now just reshape:

现在重塑:

In [157]: arr.transpose(0,2,1,3).reshape(4,4)
Out[157]: 
array([[ 0,  1,  4,  5],
       [ 2,  3,  6,  7],
       [ 8,  9, 12, 13],
       [10, 11, 14, 15]])

or with the added singleton dimensions:

或添加单身尺寸:

In [158]: arr.transpose(0,2,1,3).reshape(1,4,4,1)
Out[158]: 
array([[[[ 0],
         [ 1],
         [ 4],
         [ 5]],

        [[ 2],
         [ 3],
         [ 6],
         [ 7]],

        [[ 8],
         [ 9],
         [12],
         [13]],

        [[10],
         [11],
         [14],
         [15]]]])

#2


0  

I found the answer, maybe its helpful for others. We should use this function:

我找到了答案,也许对其他人有帮助。我们应该使用这个功能:

output_image = tf.depth_to_space(
    output_image,
    2,
    name=None,
    data_format='NHWC'
)

#1


0  

This needs a transpose plus reshaping

这需要转置加重塑

It's easier to generate this than your image, but I think I got the mapping correct:

生成这个比你的图像更容易,但我认为我的映射是正确的:

In [154]: arr = np.arange(16).reshape(2,2,2,2)
In [155]: arr
Out[155]: 
array([[[[ 0,  1],
         [ 2,  3]],

        [[ 4,  5],
         [ 6,  7]]],


       [[[ 8,  9],
         [10, 11]],

        [[12, 13],
         [14, 15]]]])

A reshape(4,4) would create [0,1,2,3], etc, but you want [0,1,4,5], so we need to swap the middle axes. Order of 1st and last axes is unchanged.

重塑(4,4)会创建[0,1,2,3]等,但是你需要[0,1,4,5],所以我们需要交换中轴。第1轴和最后轴的顺序不变。

In [156]: arr.transpose(0,2,1,3)
Out[156]: 
array([[[[ 0,  1],
         [ 4,  5]],

        [[ 2,  3],
         [ 6,  7]]],


       [[[ 8,  9],
         [12, 13]],

        [[10, 11],
         [14, 15]]]])

Now just reshape:

现在重塑:

In [157]: arr.transpose(0,2,1,3).reshape(4,4)
Out[157]: 
array([[ 0,  1,  4,  5],
       [ 2,  3,  6,  7],
       [ 8,  9, 12, 13],
       [10, 11, 14, 15]])

or with the added singleton dimensions:

或添加单身尺寸:

In [158]: arr.transpose(0,2,1,3).reshape(1,4,4,1)
Out[158]: 
array([[[[ 0],
         [ 1],
         [ 4],
         [ 5]],

        [[ 2],
         [ 3],
         [ 6],
         [ 7]],

        [[ 8],
         [ 9],
         [12],
         [13]],

        [[10],
         [11],
         [14],
         [15]]]])

#2


0  

I found the answer, maybe its helpful for others. We should use this function:

我找到了答案,也许对其他人有帮助。我们应该使用这个功能:

output_image = tf.depth_to_space(
    output_image,
    2,
    name=None,
    data_format='NHWC'
)