【深度學習】Tensorflow函式詳解
目錄
tf.train.GradientDescentOptimizer
tf.truncated_normal
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
從截斷的正態分佈中輸出隨機值。
生成的值服從具有指定平均值和標準偏差的正態分佈,如果生成的值大於平均值2個標準偏差的值則丟棄重新選擇。
在正態分佈的曲線中,橫軸區間(μ-σ,μ+σ)內的面積為68.268949%。
橫軸區間(μ-2σ,μ+2σ)內的面積為95.449974%。
橫軸區間(μ-3σ,μ+3σ)內的面積為99.730020%。
X落在(μ-3σ,μ+3σ)以外的概率小於千分之三,在實際問題中常認為相應的事件是不會發生的,基本上可以把區間(μ-3σ,μ+3σ)看作是隨機變數X實際可能的取值區間,這稱之為正態分佈的“3σ”原則。
在tf.truncated_normal中如果x的取值在區間(μ-2σ,μ+2σ)之外則重新進行選擇。這樣保證了生成的值都在均值附近。
引數:
- shape: 一維的張量,也是輸出的張量。
- mean: 正態分佈的均值。
- stddev: 正態分佈的標準差。
- dtype: 輸出的型別。
- seed: 一個整數,當設定之後,每次生成的隨機數都一樣。
- name: 操作的名字。
tf.random_normal
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
從正態分佈中輸出隨機值。
引數:
- shape: 一維的張量,也是輸出的張量。
- mean: 正態分佈的均值。
- stddev: 正態分佈的標準差。
- dtype: 輸出的型別。
- seed: 一個整數,當設定之後,每次生成的隨機數都一樣。
- name: 操作的名字。
tf.nn.conv2d
tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)
tf.nn.conv2d是TensorFlow裡面實現卷積的函式,這是搭建卷積神經網路比較核心的一個方法,非常重要。
除去name引數用以指定該操作的name,與方法有關的一共五個引數:
- 第一個引數input:指需要做卷積的輸入影象,它要求是一個Tensor,具有[batch, in_height, in_width, in_channels]這樣的shape,具體含義是[訓練時一個batch的圖片數量, 圖片高度, 圖片寬度, 影象通道數],注意這是一個4維的Tensor,要求型別為float32和float64其中之一
- 第二個引數filter:相當於CNN中的卷積核,它要求是一個Tensor,具有[filter_height, filter_width, in_channels, out_channels]這樣的shape,具體含義是[卷積核的高度,卷積核的寬度,影象通道數,卷積核個數],要求型別與引數input相同,有一個地方需要注意,第三維in_channels,就是引數input的第四維
- 第三個引數strides:卷積時在影象每一維的步長,這是一個一維的向量,長度4
- 第四個引數padding:string型別的量,只能是"SAME","VALID"其中之一,這個值決定了不同的卷積方式
- 第五個引數:use_cudnn_on_gpu:bool型別,是否使用cudnn加速,預設為true
結果返回一個Tensor,這個輸出,就是我們常說的feature map,shape仍然是[batch, height, width, channels]這種形式。
- padding 無論取 'SAME' 還是取 'VALID', 它在 conv2d 和 max_pool 上的表現是一致的;
- padding = 'SAME' 時,輸出並不一定和原圖size一致,但會保證覆蓋原圖所有畫素,不會捨棄邊上的莫些元素;
- padding = 'VALID' 時,輸出的size總比原圖的size小,有時不會覆蓋原圖所有元素(既,可能捨棄邊上的某些元素).
tf.nn.max_pool
tf.nn.max_pool(value, ksize, strides, padding, name=None)
max pooling是CNN當中的最大值池化操作,其實用法和卷積很類似。
- 第一個引數value:需要池化的輸入,一般池化層接在卷積層後面,所以輸入通常是feature map,依然是[batch, height, width, channels]這樣的shape
- 第二個引數ksize:池化視窗的大小,取一個四維向量,一般是[1, height, width, 1],因為我們不想在batch和channels上做池化,所以這兩個維度設為了1
- 第三個引數strides:和卷積類似,視窗在每一個維度上滑動的步長,一般也是[1, stride,stride, 1]
- 第四個引數padding:和卷積類似,可以取'VALID' 或者'SAME'
返回一個Tensor,型別不變,shape仍然是[batch, height, width, channels]這種形式
tf.reshape
tf.reshape(tensor,shape,name=None)
函式的作用是將tensor變換為引數shape形式,其中的shape為一個列表形式,特殊的是列表可以實現逆序的遍歷,即list(-1).-1所代表的含義是我們不用親自去指定這一維的大小,函式會自動進行計算,但是列表中只能存在一個-1。(如果存在多個-1,就是一個存在多解的方程)
tf.nn.softmax
tf.nn.softmax(logits, axis=None, name=None, dim=None)
- logits:
A non-empty Tensor. 一個非空張量
Must be one of the following types: half, float32, float64.必須是以下型別之一:half, float32, float64 - axis:
The dimension softmax would be performed on. 將被執行的softmax維度
The default is -1 which indicates the last dimension.預設值是-1,表示最後一個維度。 - name:
A name for the operation (optional).操作的名稱(可選)。 - dim:
Deprecated alias for axis. 棄用,axis的別名
通過Softmax迴歸,將logistic的預測二分類的概率的問題推廣到了n分類的概率的問題。通過公式
可以看出當n分類的個數變為2時,Softmax迴歸又退化為logistic迴歸問題。
下面的幾行程式碼說明一下用法
# -*- coding: utf-8 -*-
import tensorflow as tf
A = [1.0,2.0,3.0,4.0,5.0,6.0]
with tf.Session() as sess:
print(sess.run(tf.nn.softmax(A)))
# 輸出(其中所有輸出的和為1):
# [ 0.00426978 0.01160646 0.03154963 0.08576079 0.23312201 0.63369131]
tf.reduce_sum
tf.reduce_sum(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)
input_tensor:表示輸入
axis:表示在那個維度進行sum操作。
keep_dims:表示是否保留原始資料的維度,False相當於執行完後原始資料就會少一個維度。
reduction_indices:為了跟舊版本的相容,現在已經不使用了。
tf.reduce_max,tf.reduce_mean
求最大值
tf.reduce_max(input_tensor, reduction_indices=None, keep_dims=False, name=None)
求平均值
tf.reduce_mean(input_tensor, reduction_indices=None, keep_dims=False, name=None)
引數1--input_tensor:待求值的tensor。
引數2--reduction_indices:在哪一維上求解。
引數(3)(4)可忽略
tf.train.Optimizer
優化器(optimizers)類的基類。這個類定義了在訓練模型的時候新增一個操作的API。你基本上不會直接使用這個類,但是你會用到他的子類比如GradientDescentOptimizer
, AdagradOptimizer
, MomentumOptimizer
.等等這些。
後面講的時候會詳細講一下GradientDescentOptimizer
這個類的一些函式,然後其他的類只會講建構函式,因為類中剩下的函式都是大同小異的。
tf.train.GradientDescentOptimizer
__init__(learning_rate, use_locking=False,name=’GradientDescent’)
作用:建立一個梯度下降優化器物件
引數:
- learning_rate: A Tensor or a floating point value. 要使用的學習率
- use_locking: 要是True的話,就對於更新操作(update operations.)使用鎖
- name: 名字,可選,預設是”GradientDescent”.
compute_gradients(loss,var_list=None,gate_gradients=GATE_OP,aggregation_method=None,colocate_gradients_with_ops=False,grad_loss=None)
作用:對於在變數列表(var_list)中的變數計算對於損失函式的梯度,這個函式返回一個(梯度,變數)對的列表,其中梯度就是相對應變數的梯度了。這是minimize()函式的第一個部分,
引數:
- loss: 待減小的值
- var_list: 預設是在GraphKey.TRAINABLE_VARIABLES.
- gate_gradients: How to gate the computation of gradients. Can be GATE_NONE, GATE_OP, or GATE_GRAPH.
- aggregation_method: Specifies the method used to combine gradient terms. Valid values are defined in the class AggregationMethod.
- colocate_gradients_with_ops: If True, try colocating gradients with the corresponding op.
- grad_loss: Optional. A Tensor holding the gradient computed for loss.
apply_gradients(grads_and_vars,global_step=None,name=None)
作用:把梯度“應用”(Apply)到變數上面去。其實就是按照梯度下降的方式加到上面去。這是minimize()函式的第二個步驟。 返回一個應用的操作。
引數:
- grads_and_vars: compute_gradients()函式返回的(gradient, variable)對的列表
- global_step: Optional Variable to increment by one after the variables have been updated.
- name: 可選,名字
get_name()
minimize(loss,global_step=None,var_list=None,gate_gradients=GATE_OP,aggregation_method=None,colocate_gradients_with_ops=False,name=None,grad_loss=None)
作用:非常常用的一個函式
通過更新var_list來減小loss,這個函式就是前面compute_gradients() 和apply_gradients().的結合
tf.train.AdadeltaOptimizer
tf.train.AdagradOptimizer.__init__(learning_rate, initial_accumulator_value=0.1, use_locking=False, name=’Adagrad’)
- learning_rate: A Tensor or a floating point value. The learning rate.
- initial_accumulator_value: A floating point value. Starting value for the accumulators, must be positive.
- use_locking: If True use locks for update operations.
- name: Optional name prefix for the operations created when applying gradients. Defaults to "Adagrad".
tf.train.MomentumOptimizer
tf.train.MomentumOptimizer.__init__(learning_rate, momentum, use_locking=False, name=’Momentum’, use_nesterov=False)
引數:
- learning_rate: A Tensor or a floating point value. The learning rate.
- momentum: A Tensor or a floating point value. The momentum.
- use_locking: If True use locks for update operations.
- name: Optional name prefix for the operations created when applying gradients. Defaults to “Momentum”.
tf.train.AdamOptimizer
tf.train.AdamOptimizer.__init__(learning_rate=0.001, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False, name=’Adam’)
實現了Adam演算法的優化器
引數:
- learning_rate: A Tensor or a floating point value. The learning rate.
- beta1: A float value or a constant float tensor. The exponential decay rate for the 1st moment estimates.
- beta2: A float value or a constant float tensor. The exponential decay rate for the 2nd moment estimates.
- epsilon: A small constant for numerical stability.
- use_locking: If True use locks for update operations.
- name: Optional name for the operations created when applying gradients. Defaults to “Adam”.