Posted at

Tensorflow API: tf.reverse

More than 1 year has passed since last update.

https://www.tensorflow.org/api_docs/python/tf/reverse


  • 入力のテンソルを反転させる

  • TensorFlow 1.0より機能が変更されている。

reverse(

tensor,
axis,
name=None
)


  • tensor: 入力テンソル

  • axis: 反転させる次元を、integerのリストで指定

  • name: 出力テンソルの名前


使用例


  • 実行環境: TensorFlow 1.2.0

import tensorflow as tf

# 3次元のテンソルを定義
x = tf.constant([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[11, 12, 13], [14, 15, 16], [17, 18, 19]]])

sess = tf.Session()
x_reverse_0 = tf.reverse(x, [0])
x_reverse_1 = tf.reverse(x, [1])
x_reverse_2 = tf.reverse(x, [2])
x_reverse_0_1 = tf.reverse(x, [0, 1])
x_reverse_0_1_2 = tf.reverse(x, [0, 1, 2])

# 結果の確認
print(str(sess.run(x)) + '\n')

print("tf.reverse(x, [0]):")
print(str(sess.run(x_reverse_0)) + '\n')

print("tf.reverse(x, [1]):")
print(str(sess.run(x_reverse_1)) + '\n')

print("tf.reverse(x, [2]):")
print(str(sess.run(x_reverse_2)) + '\n')

print("tf.reverse(x, [0, 1]):")
print(str(sess.run(x_reverse_0_1)) + '\n')

print("tf.reverse(x, [0, 1, 2]):")
print(str(sess.run(x_reverse_0_1_2)) + '\n')


出力

[[[ 1  2  3]

[ 4 5 6]
[ 7 8 9]]

[[11 12 13]
[14 15 16]
[17 18 19]]]

tf.reverse(x, [0]):
[[[11 12 13]
[14 15 16]
[17 18 19]]

[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]]]

tf.reverse(x, [1]):
[[[ 7 8 9]
[ 4 5 6]
[ 1 2 3]]

[[17 18 19]
[14 15 16]
[11 12 13]]]

tf.reverse(x, [2]):
[[[ 3 2 1]
[ 6 5 4]
[ 9 8 7]]

[[13 12 11]
[16 15 14]
[19 18 17]]]

tf.reverse(x, [0, 1]):
[[[17 18 19]
[14 15 16]
[11 12 13]]

[[ 7 8 9]
[ 4 5 6]
[ 1 2 3]]]

tf.reverse(x, [0, 1, 2]):
[[[19 18 17]
[16 15 14]
[13 12 11]]

[[ 9 8 7]
[ 6 5 4]
[ 3 2 1]]]