tensorflow基本語法理解
最近在看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)]