In [1]:
import tensorflow as tf
import numpy as np
In [2]:
A = np.array([[[1,2,3],[0.1,0.2,0.3],[0.01,0.02,0.03]],[[11,12,13],[0.11,0.12,0.13],[0.011,0.012,0.013]],[[111,112,113],[0.111,0.112,0.113],[0.0111,0.0112,0.0113]]])
B = np.array([[[4,5,6],[0.4,0.5,0.6],[0.04,0.05,0.06]],[[14,15,16],[0.14,0.15,0.16],[0.014,0.015,0.016]],[[114,115,116],[0.114,0.115,0.116],[0.0114,0.0115,0.0116]]])
C = np.array([[[7,8,9],[0.7,0.8,0.9],[0.07,0.08,0.09]],[[17,18,19],[0.17,0.18,0.19],[0.017,0.018,0.019]],[[117,118,119],[0.117,0.118,0.119],[0.0117,0.0118,0.0119]]])
In [3]:
"""
tf.stack()
tf.stack(values, axis=0, name=’stack’)
将 a list of R 维的Tensor堆成 R+1维的Tensor。
Given a list of length N of tensors of shape (A, B, C);
if axis == 0 then the output tensor will have the shape (N, A, B, C)
这时 res[i,:,:,:] 就是原 list中的第 i 个 tensor
1
. if axis == 1 then the output tensor will have the shape (A, N, B, C).
这时 res[:,i,:,:] 就是原list中的第 i 个 tensor
"""
sess = tf.InteractiveSession()
A
Out[3]:
In [4]:
D = tf.stack([A,B])
D1 = tf.stack([A,B],axis=1)
D2 = tf.stack([A,B],axis=2)
In [5]:
D.eval()
Out[5]:
In [6]:
D1.eval()
Out[6]:
In [7]:
D2.eval()
Out[7]:
In [8]:
X = np.array([[[1,2,3],[4,5,6],[7,8,9]],[[11,12,13],[14,15,16],[17,18,19]],[[21,22,23],[24,25,26],[27,28,29]]])
Y = np.array([[[101,102,103],[104,105,106],[107,108,109]],[[111,112,113],[114,115,116],[117,118,119]],[[121,122,123],[124,125,126],[127,128,129]]])
Z = tf.stack([X,Y]).eval()
X.shape
Out[8]:
In [9]:
Z
Out[9]:
In [10]:
y = tf.transpose(Z,perm=[1,2,3,0]).eval()
In [11]:
y.shape
Out[11]:
In [12]:
y
Out[12]:
In [23]:
X = np.array([[[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[13,14,15,16],[17,18,19,20],[21,22,23,24]]])
X.shape
Out[23]:
In [25]:
X_trans_1 = tf.transpose(X,perm=[2,0,1]).eval()
print(X_trans_1)
In [28]:
X_reshape_1 = tf.reshape(X_trans_1,[4,-1]).eval()
In [29]:
X_reshape_1
Out[29]:
In [ ]: