tensorflow基本语法理解

来源:互联网 发布:韩火火淘宝店 编辑:程序博客网 时间:2024/05/22 00:10

最近在看RNN时,对tf.unpack()维度变化不了解,经过试验大致了解了维度的变化

构造三维数据分布如下图所示:

这里写图片描述

情况一:

import tensorflow as tfa = 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 tfa = 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 tfa = 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)]

原创粉丝点击