Python TensorFlow跨步_切片不会在整个范围内向后迭代

Python TensorFlow跨步_切片不会在整个范围内向后迭代,python,tensorflow,Python,Tensorflow,我有以下测试。我想倒过来,最后一行是第一行,依此类推 x_val = np.arange(3 * 2 * 3).astype(np.int64).reshape((3, 2, 3)) print(x_val) x = tf.placeholder(tf.float32, x_val.shape, name='input') x_ = tf.strided_slice(x, [3], [0], [-1]) _ = tf.identity(x_, name='

我有以下测试。我想倒过来,最后一行是第一行,依此类推

    x_val = np.arange(3 * 2 * 3).astype(np.int64).reshape((3, 2, 3))
    print(x_val)
    x = tf.placeholder(tf.float32, x_val.shape, name='input')
    x_ = tf.strided_slice(x, [3], [0], [-1])
    _ = tf.identity(x_, name='output')
    with tf.Session() as sess:
        variables_lib.global_variables_initializer().run()
        output_dict = []
        for out_name in ['output:0']:
            output_dict.append(sess.graph.get_tensor_by_name(out_name))
        expected = sess.run(output_dict, feed_dict={"input:0": x_val})
        print('test_strided_slice1', expected[0].shape, expected[0])
我希望我的输出为:

[
  [
    [12. 13. 14.]
    [15. 16. 17.]
  ]
  [
    [ 6.  7.  8.]
    [ 9. 10. 11.]
  ]
  [
    [ 0  1  2]
    [ 3  4  5]
  ]
]
然而,我得到:

[
  [
    [12. 13. 14.]
    [15. 16. 17.]
  ]
  [
    [ 6.  7.  8.]
    [ 9. 10. 11.]
  ]
]
正如你所看到的,第一排现在应该是最后一排,但却被遗漏了

如果我以0:3:1的比例通过,我会得到所有的行。但如果我倒过来,我就少了一个

不提供“结束”索引会导致测试失败。将“end”设置为-1也会导致输出为空


关于如何实现这一点,您有什么建议吗?

在大多数情况下,通过Python索引语法使用它更方便,因此您可以执行以下操作:

x_ = x[::-1]
但是,直接使用它可以做您想要做的事情。为此,需要使用
end\u mask
参数。在该整数值中,如果设置了第i位(从最低有效位开始),则忽略第i维对应的
end
值,并尽可能获取切片。所以你可以做:

x_ = tf.strided_slice(x, [3], [0], [-1], end_mask=1)
注:我已将
begin
中的
4
更改为
3
,因为这是切片开始的实际索引(尽管它也适用于
4
)。如果您只想从末尾到开头提取切片,还可以使用
start\u mask
,其工作原理类似于
end\u mask

x_ = tf.strided_slice(x, [0], [0], [-1], start_mask=1, end_mask=1)
一个小例子:

将tensorflow导入为tf
使用tf.Graph()作为默认值():
x=tf.重塑(tf.范围(18),[3,2,3])
x_uu=tf.跨步_u切片(x、[0]、[0]、-1],开始_u掩码=1,结束_u掩码=1)
使用tf.Session()作为sess:
打印(sess.run(x_))
输出:

[[12 13 14]
[15 16 17]]
[[ 6  7  8]
[ 9 10 11]]
[[ 0  1  2]
[ 3  4  5]]]

您可以编辑您的代码吗?我无法复制你的结果。对此我很抱歉。最后一刻的绝望改变。非常感谢。这就是我希望得到的。