# 1.矩陣操作

## 1.1矩陣生成

### tf.ones | tf.zeros

tf.ones(shape,type=tf.float32,name=None)
tf.zeros([2, 3], int32)

sess = tf.InteractiveSession()
x = tf.ones([2, 3], int32)
print(sess.run(x))
#[[1 1 1],
# [1 1 1]]

### tf.ones_like | tf.zeros_like

tf.ones_like(tensor,dype=None,name=None)
tf.zeros_like(tensor,dype=None,name=None)

tensor=[[1, 2, 3], [4, 5, 6]]
x = tf.ones_like(tensor)
print(sess.run(x))
#[[1 1 1],
# [1 1 1]]

### tf.fill

tf.fill(shape,value,name=None)

print(sess.run(tf.fill([2,3],2)))
#[[2 2 2],
# [2 2 2]]

### tf.constant

tf.constant(value,dtype=None,shape=None,name=’Const’)

a = tf.constant(2,shape=[2])
b = tf.constant(2,shape=[2,2])
c = tf.constant([1,2,3],shape=[6])
d = tf.constant([1,2,3],shape=[3,2])
sess = tf.InteractiveSession()
print(sess.run(a))
#[2 2]
print(sess.run(b))
#[[2 2]
# [2 2]]
print(sess.run(c))
#[1 2 3 3 3 3]
print(sess.run(d))
#[[1 2]
# [3 3]
# [3 3]]


### tf.random_normal | tf.truncated_normal | tf.random_uniform

tf.random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.random_uniform(shape,minval=0,maxval=None,dtype=tf.float32,seed=None,name=None)

random_normal: 正太分佈隨機數，均值mean,標準差stddev
truncated_normal:截斷正態分佈隨機數，均值mean,標準差stddev,不過只保留[mean-2*stddev,mean 2*stddev]範圍內的隨機數
random_uniform:均勻分佈隨機數，範圍為[minval,maxval]

sess = tf.InteractiveSession()
x = tf.random_normal(shape=[1,5],mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)
print(sess.run(x))
#===>[[-0.36128798  0.58550537 -0.88363433 -0.2677258   1.05080092]]

### tf.get_variable

get_variable(name, shape=None, dtype=dtypes.float32, initializer=None,
regularizer=None, trainable=True, collections=None,
caching_device=None, partitioner=None, validate_shape=True,
custom_getter=None):

name: 這個不用說了，變數的名字
shape: 變數的形狀，[]表示一個數，[3]表示長為3的向量，[2,3]表示矩陣或者張量(tensor)
dtype: 變數的資料格式，主要有tf.int32, tf.float32, tf.float64等等
initializer: 初始化工具，有tf.zero_initializer, tf.ones_initializer, tf.constant_initializer, tf.random_uniform_initializer, tf.random_normal_initializer, tf.truncated_normal_initializer等

## 1.2 矩陣變換

### tf.shape

tf.shape(Tensor)
Returns the shape of a tensor.返回張量的形狀。但是注意，tf.shape函式本身也是返回一個張量。而在tf中，張量是需要用sess.run(Tensor)來得到具體的值的。

labels = [1,2,3]
shape = tf.shape(labels)
print(shape)
sess = tf.InteractiveSession()
print(sess.run(shape))
# >>>Tensor("Shape:0", shape=(1,), dtype=int32)
# >>>[3]

### tf.expand_dims

tf.expand_dims(Tensor, dim)

shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]

sess = tf.InteractiveSession()
labels = [1,2,3]
x = tf.expand_dims(labels, 0)
print(sess.run(x))
x = tf.expand_dims(labels, 1)
print(sess.run(x))
#>>>[[1 2 3]]
#>>>[[1]
#    [2]
#    [3]]

### tf.pack

tf.pack(values, axis=0, name=”pack”)
Packs a list of rank-R tensors into one rank-(R 1) tensor

  # 'x' is [1, 4]
# 'y' is [2, 5]
# 'z' is [3, 6]
pack([x, y, z]) => [[1, 4], [2, 5], [3, 6]]  # Pack along first dim.
pack([x, y, z], axis=1) => [[1, 2, 3], [4, 5, 6]]

### tf.concat

tf.concat(concat_dim, values, name=”concat”)
Concatenates tensors along one dimension.

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat(0, [t1, t2])
#==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
tf.concat(1, [t1, t2])
#==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

### tf.sparse_to_dense

def sparse_to_dense(sparse_indices,
output_shape,
sparse_values,
default_value=0,
validate_indices=True,
name=None):

sparse_indices: 元素的座標[[0,0],[1,2]] 表示(0,0)，和(1,2)處有值
output_shape: 得到的密集矩陣的shape
sparse_values: sparse_indices座標表示的點的值，可以是0D或者1D張量。若0D，則所有稀疏值都一樣。若是1D，則len(sparse_values)應該等於len(sparse_indices)
default_values: 預設點的預設值

### tf.random_shuffle

tf.random_shuffle(value,seed=None,name=None)

sess = tf.InteractiveSession()
a=[[1,2],[3,4],[5,6]]
x = tf.random_shuffle(a)
print(sess.run(x))
#===>[[3 4],[5 6],[1 2]]

### tf.argmax | tf.argmin

tf.argmax(input=tensor,dimention=axis)

a=tf.get_variable(name='a',
shape=[3,4],
dtype=tf.float32,
initializer=tf.random_uniform_initializer(minval=-1,maxval=1))
b=tf.argmax(input=a,dimension=0)
c=tf.argmax(input=a,dimension=1)
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
print(sess.run(a))
#[[ 0.04261756 -0.34297419 -0.87816691 -0.15430689]
# [ 0.18663144  0.86972666 -0.06103253  0.38307118]
# [ 0.84588599 -0.45432305 -0.39736366  0.38526249]]
print(sess.run(b))
#[2 1 1 2]
print(sess.run(c))
#[0 1 0]

### tf.equal

tf.equal(x, y, name=None):

### tf.cast

cast(x, dtype, name=None)

a = tf.Variable([1,0,0,1,1])
b = tf.cast(a,dtype=tf.bool)
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
print(sess.run(b))
#[ True False False  True  True]

### tf.matmul

matmul(a, b,
transpose_a=False, transpose_b=False,
a_is_sparse=False, b_is_sparse=False,
name=None):

### tf.reshape

reshape(tensor, shape, name=None)

# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
# tensor 't' is [[[1, 1], [2, 2]],
#                [[3, 3], [4, 4]]]
# tensor 't' has shape [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2],
[3, 3, 4, 4]]
# tensor 't' is [[[1, 1, 1],
#                 [2, 2, 2]],
#                [[3, 3, 3],
#                 [4, 4, 4]],
#                [[5, 5, 5],
#                 [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]
# -1 can also be used to infer the shape
# -1 is inferred to be 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 3:
reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]]

# 2. 神經網路相關操作

### tf.nn.embedding_lookup

embedding_lookup(params, ids, partition_strategy=”mod”, name=None,
validate_indices=True):

ids=[i1,i2,…,im]params=⎡⎣⎢⎢⎢⎢⎢w11,w21,⋯,wh1w12,w22,⋯,wh2⋮w1v,w2v,⋯,whv⎤⎦⎥⎥⎥⎥⎥res=⎡⎣⎢⎢⎢⎢⎢w1i1,w2i1,…,whi1w1i2,w2i2,…,whi2⋮w1im,w2im,…,whim⎤⎦⎥⎥⎥⎥⎥

\begin{align}
& ids=[i_1,i_2,\ldots,i_m]\\
& params =
\begin{bmatrix}
w_1^1,w_1^2,\cdots,w_1^h\\
w_2^1,w_2^2,\cdots,w_2^h\\
\vdots\\
w_v^1,w_v^2,\cdots,w_v^h
\end{bmatrix}\\
& res=
\begin{bmatrix}
w_{i_1}^1,w_{i_1}^2,\ldots,w_{i_1}^h\\
w_{i_2}^1,w_{i_2}^2,\ldots,w_{i_2}^h\\
\vdots\\
w_{i_m}^1,w_{i_m}^2,\ldots,w_{i_m}^h\\
\end{bmatrix}
\end{align}

### tf.trainable_variables

tf.get_variable(…), tf.Variable(…)的預設選項是True, 而 tf.constant(…)只能是False

import tensorflow as tf
from pprint import pprint
a = tf.get_variable('a',shape=[5,2])    # 預設 trainable=True
b = tf.get_variable('b',shape=[2,5],trainable=False)
c = tf.constant([1,2,3],dtype=tf.int32,shape=[8],name='c') # 因為是常量，所以trainable=False
d = tf.Variable(tf.random_uniform(shape=[3,3]),name='d')
tvar = tf.trainable_variables()
tvar_name = [x.name for x in tvar]
print(tvar)
# [<tensorflow.python.ops.variables.Variable object at 0x7f9c8db8ca20>, <tensorflow.python.ops.variables.Variable object at 0x7f9c8db8c9b0>]
print(tvar_name)
# ['a:0', 'd:0']
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
pprint(sess.run(tvar))
#[array([[ 0.27307487, -0.66074866],
#       [ 0.56380701,  0.62759042],
#       [ 0.50012994,  0.42331111],
#       [ 0.29258847, -0.09185416],
#       [-0.35913971,  0.3228929 ]], dtype=float32),
# array([[ 0.85308731,  0.73948073,  0.63190091],
#       [ 0.5821209 ,  0.74533939,  0.69830012],
#       [ 0.61058474,  0.76497936,  0.10329771]], dtype=float32)]


def gradients(ys,
xs,
aggregation_method=None):

gi=∑j=0len(y)∂yj∂xig=[g0,g1,…,glen(x)]

g_i=\sum_{j=0}^{len(y)} \frac{\partial y_j}{\partial x_i}\\
g=[g_0,g_1,…,g_{len(x)}]

### tf.clip_by_global_norm

def clip_by_global_norm(t_list, clip_norm, use_norm=None, name=None):

list_clipped[i]=t_list[i] * clip_norm / max(global_norm, clip_norm),其中
global_norm = sqrt(sum([l2norm(t)**2 for t in t_list]))

Lic=Lit∗Ncmax(Nc,Ng)Ng=∑i(Lit)2‾‾‾‾‾‾‾‾√

L_c^i=\frac{L_t^i*N_c}{max(N_c,N_g)} \\
N_g = \sqrt{\sum_i(L_t^i)^2}

LicL_c^i和LigL_g^i代表t_list[i]和list_clipped[i]，
NcN_c和NgN_g代表clip_norm 和 global_norm的值。

Lic={Lit,(Ng<=Nc)Lit∗NcNg,(Ng>Nc)Ng=∑i(Lit)2‾‾‾‾‾‾‾‾√

\begin{align}
&L_c^i=
\begin{cases}
L_t^i, (N_gN_c)
\end{cases}\\
&N_g = \sqrt{\sum_i(L_t^i)^2}
\end{align}

### tf.nn.dropout

dropout(x, keep_prob, noise_shape=None, seed=None, name=None)

x是一個張量，而keep_prob是一個（0,1]之間的值。x中的各個元素清零的概率互相獨立，為1-keep_prob,而沒有清零的元素，則會統一乘以1/keep_prob, 目的是為了保持x的整體期望值不變。

sess = tf.InteractiveSession()
a = tf.get_variable('a',shape=[2,5])
b = a
a_drop = tf.nn.dropout(a,0.8)
sess.run(tf.initialize_all_variables())
print(sess.run(b))
#[[ 0.28667903 -0.66874665 -1.14635754  0.88610041 -0.55590457]
# [-0.29704338 -0.01958954  0.80359757  0.75945008  0.74934876]]
print(sess.run(a_drop))
#[[ 0.35834879 -0.83593333 -1.43294692  1.10762548 -0.        ]
# [-0.37130421 -0.          0.          0.94931257  0.93668592]]

# 3.普通操作

### tf.linspace | tf.range

tf.linspace(start,stop,num,name=None)
tf.range(start,limit=None,delta=1,name=’range’)

tf.linspace在[start,stop]範圍內產生num個數的等差數列。不過注意，start和stop要用浮點數表示，不然會報錯
tf.range在[start,limit)範圍內以步進值delta產生等差數列。注意是不包括limit在內的。

sess = tf.InteractiveSession()
x = tf.linspace(start=1.0,stop=5.0,num=5,name=None)  # 注意1.0和5.0
y = tf.range(start=1,limit=5,delta=1)
print(sess.run(x))
print(sess.run(y))
#===>[ 1.  2.  3.  4.  5.]
#===>[1 2 3 4]

### tf.assign

assign(ref, value, validate_shape=None, use_locking=None, name=None)
tf.assign是用來更新模型中變數的值的。ref是待賦值的變數，value是要更新的值。即效果等同於 ref = value

sess = tf.InteractiveSession()
a = tf.Variable(0.0)
b = tf.placeholder(dtype=tf.float32,shape=[])
op = tf.assign(a,b)
sess.run(tf.initialize_all_variables())
print(sess.run(a))
# 0.0
sess.run(op,feed_dict={b:5.})
print(sess.run(a))
# 5.0

# 4.規範化

### tf.variable_scope

  with tf.variable_scope("foo"):
with tf.variable_scope("bar"):
v = tf.get_variable("v", [1])
assert v.name == "foo/bar/v:0"

def variable_scope(name_or_scope, reuse=None, initializer=None,
regularizer=None, caching_device=None, partitioner=None,
custom_getter=None):

name_or_scope: string or VariableScope: the scope to open.
reuse: True or None; if True, we go into reuse mode for this scope as well as all sub-scopes; if None, we just inherit the parent scope reuse. 如果reuse=True, 那麼就是使用之前定義過的name_scope和其中的變數，
initializer: default initializer for variables within this scope.
regularizer: default regularizer for variables within this scope.
caching_device: default caching device for variables within this scope.
partitioner: default partitioner for variables within this scope.
custom_getter: default custom getter for variables within this scope.