tf.stack和tf.unstack

tf.stack和tf.unstack分别表示矩阵的合并和分解,下面用一个小示例演示用法

import tensorflow as tf
import sys
import os
import numpy as np

a = tf.constant([1 , 2 , 3])
b = tf.constant([4 , 5 , 6])

c = tf.stack([a , b] , axis=0)
d = tf.unstack(c , axis=0)
e = tf.unstack(c , axis=1)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(c))
    print(sess.run(d))
    print(sess.run(e))

输出结果:
[[1 2 3]
[4 5 6]]
[array([1, 2, 3]), array([4, 5, 6])]
[array([1, 4]), array([2, 5]), array([3, 6])]

你可能感兴趣的:(tensorflow)