來(lái)源:tensorflow學(xué)習(xí)筆記(二):tensor 變換
矩陣操作
#所有的reduce_...瞒渠,如果不加axis的話(huà)伍玖,都是對(duì)整個(gè)矩陣進(jìn)行運(yùn)算
tf.reduce_sum(a, 1) #對(duì)axis1
tf.reduce_mean(a,0) #每列均值
第二個(gè)參數(shù)是axis窍箍,如果為0的話(huà)椰棘,res[i]=∑ja[j,i]即(res[i]=∑a[:,i])邪狞, 如果是1的話(huà)帆卓,res[i]=∑ja[i,j] NOTE:返回的都是行向量,(axis等于幾剑令,就是對(duì)那維操作,i.e.:沿著那維操作)
#關(guān)于concat,可以用來(lái)進(jìn)行降維 3D->2D , 2D->1D
tf.concat(concat_dim, data)
#arr = np.zeros([2,3,4,5,6])
In [6]: arr2.shape
Out[6]: (2, 3, 4, 5)
In [7]: np.concatenate(arr2, 0).shape
Out[7]: (6, 4, 5) :(2*3, 4, 5)
In [9]: np.concatenate(arr2, 1).shape
Out[9]: (3, 8, 5) :(3, 2*4, 5)
#tf.concat()
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
# 將t1, t2進(jìn)行concat悔橄,axis為0腺毫,等價(jià)于將shape=[2, 2, 3]的Tensor concat成
#shape=[4, 3]的tensor癣疟。在新生成的Tensor中tensor[:2,:]代表之前的t1
#tensor[2:,:]是之前的t2
tf.concat(0, [t1, t2]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
# 將t1, t2進(jìn)行concat潮酒,axis為1睛挚,等價(jià)于將shape=[2, 2, 3]的Tensor concat成
#shape=[2, 6]的tensor急黎。在新生成的Tensor中tensor[:,:3]代表之前的t1
#tensor[:,3:]是之前的t2
tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
concat是將list中的向量給連接起來(lái)扎狱,axis表示將那維的數(shù)據(jù)連接起來(lái),而其他維的結(jié)構(gòu)保持不變
#squeeze 降維 維度為1的降掉
tf.squeeze(arr, [])
降維淤击, 將維度為1 的降掉
arr = tf.Variable(tf.truncated_normal([3,4,1,6,1], stddev=0.1))
arr2 = tf.squeeze(arr, [2,4])
arr3 = tf.squeeze(arr) #降掉所以是1的維
#split
tf.split(split_dim, num_split, value, name='split')
# 'value' is a tensor with shape [5, 30]
# Split 'value' into 3 tensors along dimension 1
split0, split1, split2 = tf.split(1, 3, value)
tf.shape(split0) ==> [5, 10]
#embedding
mat = np.array([1,2,3,4,5,6,7,8,9]).reshape((3,-1))
ids = [[1,2], [0,1]]
res = tf.nn.embedding_lookup(mat, ids)
res.eval()
array([[[4, 5, 6],
[7, 8, 9]],
[[1, 2, 3],
[4, 5, 6]]])
#擴(kuò)展維度匠抗,如果想用廣播特性的話(huà),經(jīng)常會(huì)用到這個(gè)函數(shù)
# 't' is a tensor of shape [2]
#一次擴(kuò)展一維
shape(tf.expand_dims(t, 0)) ==> [1, 2]
shape(tf.expand_dims(t, 1)) ==> [2, 1]
shape(tf.expand_dims(t, -1)) ==> [2, 1]
# 't2' is a tensor of shape [2, 3, 5]
shape(tf.expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(tf.expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(tf.expand_dims(t2, 3)) ==> [2, 3, 5, 1]
tf.slice()
tf.slice(input_, begin, size, name=None)
先看例子
import tensorflow as tf
import numpy as np
sess = tf.InteractiveSession()
a = np.array([[1,2,3,4,5],[4,5,6,7,8],[9,10,11,12,13]])
tf.slice(a,[1,2],[-1,2]).eval()
#array([[ 6, 7],
# [11, 12]])
理解tf.slice()最好是從返回值上去理解汞贸,現(xiàn)在假設(shè)input的shape是[a1, a2,a3], begin的值是[b1, b2, b3],size的值是[s1, s2, s3],那么tf.slice()返回的值就是 input[b1:b1+s1, b2:b2+s2, b3:b3+s3]矢腻。 如果 si=?1,那么 返回值就是 input[b1:b1+s1,..., bi: ,...]
注意:input[1:2] 取不到input[2]
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)這時(shí) res[i,:,:,:] 就是原 list中的第 i 個(gè) tensor. if axis == 1 then the output tensor will have the shape (A, N, B, C).這時(shí) res[:,i,:,:] 就是原list中的第 i 個(gè) tensor
Etc.
# 'x' is [1, 4]
# 'y' is [2, 5]
# 'z' is [3, 6]
stack([x, y, z]) => [[1, 4], [2, 5], [3, 6]] # Pack along first dim.
stack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]]
tf.gather()
tf.gather(params, indices, validate_indices=None, name=None)
indices must be an integer tensor of any dimension (usually 0-D or 1-D). Produces an output tensor with shape indices.shape + params.shape[1:]
# Scalar indices, 會(huì)降維
output[:, ..., :] = params[indices, :, ... :]
# Vector indices
output[i, :, ..., :] = params[indices[i], :, ... :]
# Higher rank indices,會(huì)升維
output[i, ..., j, :, ... :] = params[indices[i, ..., j], :, ..., :]