Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在TensorFlow中使用tf.stripped_slice()更改形状?_Python_Tensorflow - Fatal编程技术网

Python 如何在TensorFlow中使用tf.stripped_slice()更改形状?

Python 如何在TensorFlow中使用tf.stripped_slice()更改形状?,python,tensorflow,Python,Tensorflow,例如:我们有两个大小为2x2x3像素的RGB图像。四个像素中的每一个由3个整数表示。积分可以作为二维阵列提供。前4个积分表示红色值,后4个积分表示绿色值,最后4个积分表示4个像素的蓝色值 图1: [11, 12, 13, 14, 15, 16, 17, 18, 19, 191, 192, 193] 图2: [21, 22, 23, 24, 25, 26, 27, 28, 29, 291, 292, 293] 在TensorFlow中,这两个图像存储在一个张量中 img_tensor = tf

例如:我们有两个大小为2x2x3像素的RGB图像。四个像素中的每一个由3个整数表示。积分可以作为二维阵列提供。前4个积分表示红色值,后4个积分表示绿色值,最后4个积分表示4个像素的蓝色值

图1:

[11, 12, 13, 14, 15, 16, 17, 18, 19, 191, 192, 193]
图2:

[21, 22, 23, 24, 25, 26, 27, 28, 29, 291, 292, 293]
在TensorFlow中,这两个图像存储在一个张量中

img_tensor = tf.constant([[11, 12, 13, 14, 15, 16, 17, 18, 19, 191, 192, 193],
[21, 22, 23, 24, 25, 26, 27, 28, 29, 291, 292, 293]])
# Tensor("Const_10:0", shape=(2, 12), dtype=int32)
使用
tf.stridded_slice()
后,我需要以下格式:

[[[[11, 15, 19],
[12, 16, 191]],
[[13, 17, 192],
[14, 18, 193]]],
[[[21, 25, 29],
[22, 26, 291]],
[[23, 27, 292],
[24, 28, 293]]]]
# Goal is: Tensor("...", shape=(2, 2, 2, 3), dtype=int32)
到目前为止,我所尝试的:

new_img_tensor = tf.strided_slice(img_tensor, [0, 0], [3, -1], [1, 4])
但结果是不完整的:

[[11 15 19]
 [21 25 29]]
# Tensor("StridedSlice_2:0", shape=(2, 3), dtype=int32)

是否有一种方法可以使用
tf将尺寸从2D更改为4-D。Straded_slice()

似乎您需要
重塑
+
转置
而不是
Straded_slice

tf.InteractiveSession()
tf.transpose(tf.reshape(img_tensor, (2, 3, 2, 2)), (0, 2, 3, 1)).eval()

#array([[[[ 11,  15,  19],
#         [ 12,  16, 191]],

#        [[ 13,  17, 192],
#         [ 14,  18, 193]]],


#       [[[ 21,  25,  29],
#         [ 22,  26, 291]],

#        [[ 23,  27, 292],
#         [ 24,  28, 293]]]])