An Example for tf.unstack

Example 1.

import tensorflow as tf
import numpy as np

x = np.array([[[0,1,2],[1,1,0]],[[0,1,-1],[1,1,2]]])
#shape = 2 * 2 * 3

xx = tf.convert_to_tensor(x,dtype=tf.float32)

y = tf.unstack(xx, 2, 1)
# y is a list
init = tf.global_variables_initializer() 
init_local = tf.local_variables_initializer()

with tf.Session() as sess:
    sess.run([init, init_local])
    print(sess.run(y))

The resut is:

[array([[ 0.,  1.,  2.],
       [ 0.,  1., -1.]], dtype=float32), array([[1., 1., 0.],
       [1., 1., 2.]], dtype=float32)]

Example 2.

import tensorflow as tf
import numpy as np

x = np.array([[[0,1,2],[1,1,0],[1,2,0]],[[0,1,-1],[1,1,2],[1,2,0]]])
#shape = 2 * 3 * 3

xx = tf.convert_to_tensor(x,dtype=tf.float32)

y = tf.unstack(xx, 3, 1)
#y is list and the size is 3
init = tf.global_variables_initializer() 
init_local = tf.local_variables_initializer()

with tf.Session() as sess:
    sess.run([init, init_local])
    print(sess.run(y))

The result is:

[array([[ 0.,  1.,  2.],
       [ 0.,  1., -1.]], dtype=float32), array([[1., 1., 0.],
       [1., 1., 2.]], dtype=float32), array([[1., 2., 0.],
       [1., 2., 0.]], dtype=float32)]

Example 3.

import tensorflow as tf
import numpy as np

x = np.array([
             [[[0,2,2],[1,2,0],[1,1,0]],[[0,1,-1],[1,1,2],[1,1,0]]],
             [[[0,1,2],[1,1,0],[1,2,0]],[[0,2,-1],[1,0,2],[1,3,0]]],
             [[[0,3,2],[1,3,0],[1,3,0]],[[0,3,-1],[1,2,2],[1,2,0]]]
            ])
#shape = 4 * 2 * 3 * 3
#mean: 4 batch, 2 documents, 3 sententces, word 3 dimensions

xx = tf.convert_to_tensor(x,dtype=tf.float32)

y = tf.unstack(xx, 3, 2)
#y is list and the size is 3
init = tf.global_variables_initializer() 
init_local = tf.local_variables_initializer()

with tf.Session() as sess:
    sess.run([init, init_local])
    print(sess.run(y))

The result is:

[array([[[ 0.,  2.,  2.],
        [ 0.,  1., -1.]],

       [[ 0.,  1.,  2.],
        [ 0.,  2., -1.]],

       [[ 0.,  3.,  2.],
        [ 0.,  3., -1.]]], dtype=float32), array([[[1., 2., 0.],
        [1., 1., 2.]],

       [[1., 1., 0.],
        [1., 0., 2.]],

       [[1., 3., 0.],
        [1., 2., 2.]]], dtype=float32), array([[[1., 1., 0.],
        [1., 1., 0.]],

       [[1., 2., 0.],
        [1., 3., 0.]],

       [[1., 3., 0.],
        [1., 2., 0.]]], dtype=float32)]

,