tensorflow函數/重要功能實現


一、基礎函數

1.1 、tf.reduce_sum(input_tensor, axis)   Computes the sum of elements across dimensions of a tensor,沿着維度sxis計算和

x= [[1, 1, 1], [1, 1, 1]],其秩為2

//求和,在所有維度操作,也就相當於對所有元素求和
tf.reduce_sum(x) ==> 6

//在維度0上操作,在這個例子中實際就是按列(維度0)求和
tf.reduce_sum(x, 0) ==> [2, 2, 2]
//也等價在維度-2操作
tf.reduce_sum(x, -2) ==> [2, 2, 2]


//在維度1上操作,在這個例子中實際就是按行(維度1)求和 
tf.reduce_sum(x, 1) ==> [3, 3]
//也等價在維度-1操作
tf.reduce_sum(x, 1) ==> [3, 3]

1.2、tf.concat(values, axis):Concatenates tensors along one dimension, 在維度axis連接矩陣,不改變矩陣維數,比如這個維數是指原來是2維的,拼接后也是2維的

t1 = [[1, 2, 3], [4, 5, 6]]  //2*3維 
t2 = [[7, 8, 9], [10, 11, 12]]  //2*3維
tf.concat([t1, t2], 0) == > [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]  
//在維度0上連接,那么第一個維度會增加,在這里就是行會增多,結果是4*3維矩陣.

x=tf.ones((3,2,2)) //shape (3,2,2)
C=[x,x,x]
print(tf.concat(C,2).shape) == > (3,2,6)
// 再看這個例子,三維矩陣的連接,在第3個維度上,也就是維度2, 結果第三個維度會增加,也就是(3,2,6)

1.3、維度增加與刪減

tf.expand_dims(input, axis=None, name=None, dim=None) :Inserts a dimension of 1 into a tensor’s shape,在第axis位置增加一個維度

tf.squeeze(input, squeeze_dims=None, name=None) Removes dimensions of size 1 from the shape of a tensor。從tensor中刪除所有大小是1的維度, 如果不想刪除所有尺寸1尺寸,可以通過指定squeeze_dims來刪除特定尺寸1尺寸。

tf.stack:會改變前后矩陣的維數,比如拼接之前是2維的,拼接之后就都變成3維了。

https://blog.csdn.net/mch2869253130/article/details/89232653

 

1.4、從tensor提取切片

tf.slice(input_, begin, size, name = None),作用是從輸入數據input中提取出一塊切片,切片的尺寸是size,切片的開始位置是begin。切片的尺寸size表示輸出tensor的數據維度,其中size[i]表示在第i維度上面的元素個數。開始位置begin表示切片相對於輸入數據input_的每一個偏移量,比如數據input是

參考 https://blog.csdn.net/qq_30868235/article/details/80849422

 

1.5、值壓縮函數

tf.clip_by_value(A, min, max):輸入一個張量A,把A中的每一個元素的值都壓縮在min和max之間。小於min的讓它等於min,大於max的元素的值等於max。

 https://blog.csdn.net/UESTC_C2_403/article/details/72190248

 

1.6、張量擴展復制

tf.tile(input, multiples, name=None):

  input:待擴展的張量,A Tensor. 1-D or higher.

  multiples:擴展參數,A Tensor. Must be one of the following types: int32, int64. 1-D. Length must be the same as the number of dimensions in input。

例如input是一個3維的張量。那么mutiples就必須是一個1x3的1維張量。這個張量的三個值依次表示input的第1,第2,第3維數據擴展幾倍。 

參考: https://blog.csdn.net/tsyccnh/article/details/82459859 

 

1.7、tf.where

tf.where(condition, x=None, y=None, name=None): Return the elements, either from x or y, depending on the condition.

condition、x、y維度相同,其中condition必須是bool型。當condition某個位置為true時返回x相應位置的元素,false時返回y位置的元素。

參考:https://blog.csdn.net/ustbbsy/article/details/79564828

 

1.8、tf.range

用於創建數字序列變量,有以下兩種形式:

tf.range(limit, delta=1, dtype=None, name='range')
tf.range(start, limit, delta=1, dtype=None, name='range')

該數字序列開始於 start 並且將以 delta 為增量擴展到不包括 limit 時的最大值結束,類似python的range函數。

參考:https://www.cnblogs.com/cvtoEyes/p/9002843.html

 

1.9、Tensorflow 中 crf_decode 和 viterbi_decode 的使用

https://blog.csdn.net/baobao3456810/article/details/83388516

viterbi_decode 和 crf_decode 實現了相同功能,前者是numpy的實現,后者是 tensor 的實現。

 

1.10、 tf.reshape

摘自: https://blog.csdn.net/lxg0807/article/details/53021859

tf.reshape(tensor, shape, name=None)

函數的作用是將tensor變換為參數shape的形式。其中shape為一個列表形式,特殊的一點是列表中可以存在-1。-1代表的含義是不用我們自己指定這一維的大小,函數會自動計算,但列表中只能存在一個-1。(當然如果存在多個-1,就是一個存在多解的方程了)

好了我想說的重點還有一個就是根據shape如何變換矩陣。其實簡單的想就是,reshape(t, shape) => reshape(t, [-1]) => reshape(t, shape),首先將矩陣t變為一維矩陣,然后再對矩陣的形式更改就可以了。

官方例子

# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
t = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9], tf.int32) 
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]]] # tensor 't' is [7] # shape `[]` reshapes to a scalar reshape(t, []) ==> 7

 

 1.11、tf.tile()

 tensorflow中的tile()函數是用來對張量(Tensor)進行擴展的,其特點是對當前張量內的數據進行一定規則的復制。最終的輸出張量維度不變。

https://blog.csdn.net/tsyccnh/article/details/82459859

 

1.12、tf.reduce_mean

tf.reduce_mean 函數用於計算張量tensor沿着指定的數軸(tensor的某一維度)上的的平均值,主要用作降維或者計算tensor(圖像)的平均值

https://blog.csdn.net/dcrmg/article/details/79797826

 

1.13、 tf.string_to_hash_bucket_fast( input, num_buckets, name=None)

利用hash將字符串特征轉換為整型特征,其中num_buckets為桶的個數,即hash后整型特征取值范圍.

tf.string_to_hash_bucket(tf.cast("tb",tf.string), 5) #將'tb' hash映射到桶[0, 1, 2, 3]中

 https://www.w3cschool.cn/tensorflow_python/tensorflow_python-b7kc2mrg.html

 

二、網絡層實現

2.1.  一維卷積、二維卷積

2.1.1、 一維卷積(tf.nn.conv1d)和二維卷積(tf.nn.conv1d)的比較

二維卷積是將一個特征圖在width和height兩個方向上進行滑窗操作,對應位置進行相乘並求和;而一維卷積則是只在width或者說height方向上進行滑窗並相乘求和。 

2.2.2、  tf.nn.conv2d、tf.layers.conv2d

https://blog.csdn.net/Mundane_World/article/details/80894618

Tensorflow中很多具有相同功能的函數,有不同的API。例如,2-D卷積,目前conv2d方法就有4個: 

tf.nn.conv2d, tf.layers.conv2d, tf.contrib.layers.conv2d, slim.conv2d

它們在底層都調用了gen_nn_ops.conv2d(),實際上除了參數不一樣外,其它沒有大的區別,都實現了同樣的功能。slim.conv2d已廢棄。

參考:https://blog.csdn.net/mao_xiao_feng/article/details/53444333

一維卷積示例:https://blog.csdn.net/u011734144/article/details/84066928

2.2、全連接層

//out_dim=64維,激活函數為relu
self.output_tensor = tf.layers.Dense(64, activation=tf.nn.relu)(self.input_tensor)

一般都會在全連接層加Dropout 層防止過擬合,提升泛化能力。而很少見到卷積層后接Drop out (原因主要是 卷積參數少,不易過擬合),今天找了些博客,特此記錄

 

2.3、Drop層

output_tensor = tf.layers.dropout(inputs=input_tensor,rate=dropout_rate,training=is_training) #方法1(推薦),注意rate是指訓練過程中丟掉神經元的比例
output_tensor= tf.nn.dropout(input_tensor, keep_prob) #方法2, keep_prob為訓練過程中神經元保留的比例

Dropout原理:在不同的訓練過程中隨機扔掉一部分神經元,也就是讓某個神經元的激活值以一定的概率p,讓其停止工作,這次訓練過程中不更新權值,也不參加神經網絡的計算。但是它的權重得保留下來(只是暫時不更新而已),因為下次樣本輸入時它可能又得工作了。Dropout 層一般加在全連接層 防止過擬合,提升模型泛化能力。而很少見到卷積層后接Drop out (原因主要是 卷積參數少,不易過擬合).

https://blog.csdn.net/qq_27292549/article/details/81092653

 

三、重要功能實現

3.1、tensor 標准化

3.1.1、tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None) ,對tensor利用L2范數(即歐氏距離)對指定維度 dim進行標准化。

https://blog.csdn.net/abiggg/article/details/79368982

 

3.2、對網絡層正則化 

在損失函數上加上正則項是防止過擬合的一個重要方法。tensorflow中對參數使用正則項分為兩步: 

a) 創建一個正則方法(函數/對象)
b) 將這個正則方法(函數/對象),應用到參數上

L2正則函數 tf.contrib.layers.l2_regularizer(scale, scope=None),scale: 正則項的系數,scope: 可選的scope name。L1正則類似。

使用過程示例:

//第一種方式
//1. 定義正則函數
l2_regularizer = tf.contrib.layers.l2_regularizer(scale=0.1)

//2. 在網絡層(全連接層)應用L2正則
self.fc1 = tf.layers.Dense(units=128
                   ,activation=tf.nn.relu
                  ,kernel_initializer=tf.contrib.layers.xavier_initializer()
                   ,bias_initializer=tf.zeros_initializer()
                    ,kernel_regularizer=l2_regularizer)(self.input)

//3. 在loss函數加入L2正則損失
self.l2_loss =  tf.losses.get_regularization_loss() // 使用get_regularization_loss函數獲取定義的全部L2 loss
self.ori_loss = ... //正常的損失函數
self.loss = self.ori_loss + self.l2_reg_lambda * self.l2_loss //在最終的 loss中加入L2 loss


//第二種方式
//1. 定義L2 loss變量
l2_loss = tf.constant(0.0)
//2. 在網絡層(全連接層)應用L2正則 with tf.name_scope("fc1"): W = tf.get_variable( "W_hidden", shape=[size1, size2], initializer=tf.contrib.layers.xavier_initializer()) b = tf.Variable(tf.constant(0.1, shape=[self.hidden_dim]), name="b") l2_loss += tf.nn.l2_loss(W) l2_loss += tf.nn.l2_loss(b) self.fc1_output = tf.nn.relu(tf.nn.xw_plus_b(self.input, W, b, name="fc1_output ")) //3. 在loss函數加入L2正則損失 self.ori_loss = ... //正常的損失函數 self.loss = self.ori_loss + self.l2_reg_lambda * self.l2_loss //在最終的 loss中加入L2 loss

參考 https://stackoverflow.com/questions/44232566/add-l2-regularization-when-using-high-level-tf-layers

 https://zhuanlan.zhihu.com/p/27994404

3.3、Batch Normalization 批規范化

構建方式

//示例,對全連接層使用batch normalization
with tf.variable_scope('fc1', reuse=tf.AUTO_REUSE):
    liner = tf.layers.Dense(64, activation=None)(self.input)
    norm_liner = tf.layers.batch_normalization(liner, training=is_training)
    self.fc1 = tf.nn.relu(norm_liner)

參考:http://ai.51cto.com/art/201705/540230.htm

https://www.cnblogs.com/guoyaohua/p/8724433.html

 

3.4、殘差

https://zhuanlan.zhihu.com/p/42706477 待

 

reshape


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM