最近在看RNN时,对tf.unpack()维度变化不了解,经过试验大致了解了维度的变化
构造三维数据分布如下图所示:
情况一:
import tensorflow as tf
a = tf.constant([[[1,1,1],[1,1,1]],
[[2,2,2],[2,2,2]],
[[3,3,3],[3,3,3]],
[[4,4,4],[4,4,4]],
[[5,5,5],[5,5,5]]])
d = tf.unstack(a,5,axis=0)
with tf.Session() as sess:
#print(sess.run(c))
print(sess.run(d))
#print(sess.run(e))
结果
相当于用五个平面切割z轴,会得到5个平面
[array([[1, 1, 1],
[1, 1, 1]], dtype=int32), array([[2, 2, 2],
[2, 2, 2]], dtype=int32), array([[3, 3, 3],
[3, 3, 3]], dtype=int32), array([[4, 4, 4],
[4, 4, 4]], dtype=int32), array([[5, 5, 5],
[5, 5, 5]], dtype=int32)]
情况二:
import tensorflow as tf
a = tf.constant([[[1,1,1],[1,1,1]],
[[2,2,2],[2,2,2]],
[[3,3,3],[3,3,3]],
[[4,4,4],[4,4,4]],
[[5,5,5],[5,5,5]]])
d = tf.unstack(a,2,axis=1)
with tf.Session() as sess:
#print(sess.run(c))
print(sess.run(d))
#print(sess.run(e))
结果:
相当于用两个平面切割x轴,会得到2个平面
[array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]], dtype=int32), array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]], dtype=int32)]
情况三:
import tensorflow as tf
a = tf.constant([[[1,1,1],[1,1,1]],
[[2,2,2],[2,2,2]],
[[3,3,3],[3,3,3]],
[[4,4,4],[4,4,4]],
[[5,5,5],[5,5,5]]])
d = tf.unstack(a,3,axis=2)
with tf.Session() as sess:
#print(sess.run(c))
print(sess.run(d))
#print(sess.run(e))
结果:
相当于用三个平面切割x轴,会得到3个平面
[array([[1, 1],
[2, 2],
[3, 3],
[4, 4],
[5, 5]], dtype=int32), array([[1, 1],
[2, 2],
[3, 3],
[4, 4],
[5, 5]], dtype=int32), array([[1, 1],
[2, 2],
[3, 3],
[4, 4],
[5, 5]], dtype=int32)]