# TensorFlow-理解tf.unstack(value, num=None, axis=0, name="unstack")

2019/7/24 14:03:32 人评论 次浏览 分类：学习教程

tf.unstack在循环神经网络的搭建中出现过，这里记录下方便自己记忆

``````import tensorflow as tf
import numpy as np

X = tf.constant(np.array(range(24)).reshape(2, 3, 4))

X0 = tf.unstack(X, 2, 0)
X1 = tf.unstack(X, 3, 1)
X2 = tf.unstack(X, 4, 2)

with tf.Session() as sess:
results = sess.run([X, X0, X1, X2])
for t, x in zip([X, X0, X1, X2], results):
print(t, '\n', x, '\n')
``````

``````Tensor("Const:0", shape=(2, 3, 4), dtype=int64)
[[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]

[<tf.Tensor 'unstack:0' shape=(3, 4) dtype=int64>,
<tf.Tensor 'unstack:1' shape=(3, 4) dtype=int64>]
[array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11]]),
array([[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]])]

[<tf.Tensor 'unstack_1:0' shape=(2, 4) dtype=int64>,
<tf.Tensor 'unstack_1:1' shape=(2, 4) dtype=int64>,
<tf.Tensor 'unstack_1:2' shape=(2, 4) dtype=int64>]
[array([[ 0,  1,  2,  3],
[12, 13, 14, 15]]),
array([[ 4,  5,  6,  7],
[16, 17, 18, 19]]),
array([[ 8,  9, 10, 11],
[20, 21, 22, 23]])]

[<tf.Tensor 'unstack_2:0' shape=(2, 3) dtype=int64>,
<tf.Tensor 'unstack_2:1' shape=(2, 3) dtype=int64>,
<tf.Tensor 'unstack_2:2' shape=(2, 3) dtype=int64>,
<tf.Tensor 'unstack_2:3' shape=(2, 3) dtype=int64>]
[array([[ 0,  4,  8],
[12, 16, 20]]),
array([[ 1,  5,  9],
[13, 17, 21]]),
array([[ 2,  6, 10],
[14, 18, 22]]),
array([[ 3,  7, 11],
[15, 19, 23]])]
``````

暂无相关的资讯...

-->