將兩個N維張量列表沿着axis軸組合成一個n+1維的張量,例如下面tensor(2,3)與tensor1(2,3),一個y(2,2,3)
import tensorflow as tf
tensor=[[1,2,3],[4,5,6]]
tensor2=[[10,20,30],[40,50,60]]
y=tf.stack([tensor,tensor2])
y2=tf.stack([tensor,tensor2],axis=1)
print(y)#Tensor("stack_1:0", shape=(2, 2, 3), dtype=int32)
with tf.Session() as sess:
print(sess.run(y))
# [[[ 1 2 3] [ 4 5 6]]
# [[10 20 30] [40 50 60]]]
print(sess.run(y2))
#[[[ 1 2 3][10 20 30]]
# [[ 4 5 6][40 50 60]]]
將輸入的值按照指定行或者列進行拆分,並輸出含有num個元素的列表list(axis=0)表示按照行拆分,axis=1按照列拆分,Num是輸出list的個數,必須與預計輸出的個數相等,否則會報錯
import tensorflow as tf
tensor=[[1,2,3],[4,5,6]]
y=tf.unstack([tensor])
y2=tf.unstack([tensor],axis=1)
print(y)#<tf.Tensor 'unstack:0' shape=(2, 3) dtype=int32>]
with tf.Session() as sess:
print(sess.run(y))
#[array([[1, 2, 3],[4, 5, 6]])
print(sess.run(y2))
#array([[1, 2, 3]]), array([[4, 5, 6]])
轉載自:https://blog.csdn.net/Dian1pei2xiao3/article/details/90214179