Tensorflow學習 基本函式

2021-08-26 23:31:53 字數 4183 閱讀 7914

學習tensorflow對遇到的函式的總結:(不斷更新中)

目錄

tf.truncated_normal函式 

tf.variable函式

tf.nn.conv2d函式

tf.placeholder函式

tf.nn.max_pool_2*2函式 

tf.constant函式

tf.nn.relu函式

tf.matmul()函式

tf.nn.dropout函式

tf.nn.softmax函式

tf.reduce_mean、tf.reduce_max函式

tf.cast函式

tf.global_variables_initializer函式

tf.nn.l2_loss函式

tf.add_to_collection函式

tf.nn.bias_add函式

功能:從截斷的正態分佈中輸出隨機值。

tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=none, name=none)

只保留[mean-2*stddev,mean+2*stddev]範圍內的隨機數,和一般的正太分布的產生隨機資料比起來,這個函式產生的隨機數與均值的差距不會超過兩倍的標準差

功能:定義圖變數

tf.variable.init(initial_value, trainable=true, collections=none, validate_shape=true, name=none)

只有第乙個引數是必須的

引數名稱

引數型別

含義initial_value

所有可以轉換為tensor的型別

變數的初始值

trainable

bool

如果為true,會把它加入到graphkeys.trainable_variables,才能對它使用optimizer

collections

list

指定該圖變數的型別、預設為[graphkeys.global_variables]

validate_shape

bool

如果為false,則不進行型別和維度檢查

name

string

變數的名稱,如果沒有指定則系統會自動分配乙個唯一的值

功能:二維卷積函式

tf.nn.conv2d(input,filter,strides,padding,use_cudnn_on_gpu=none,name=none)

input:指需要做卷積的輸入影象,它要求是乙個tensor,具有[batch, in_height, in_width, in_channels]這樣的shape,具體含義是[訓練時乙個batch的數量, 高度, 寬度, 影象通道數],注意這是乙個4維的tensor,要求型別為float32和float64其中之一。

結果返回乙個tensor,這個輸出,就是我們常說的feature map,shape仍然是[batch, height, width, channels]這種形式

功能:此函式可以理解為形參,用於定義過程,在執行的時候再賦具體的值

tf.placeholder(dtype,shape=none,name=none)

功能:最大池化

tf.nn.max_pool(value,kszie,strides,padding,name=none)

value:需要池化的輸入,一般池化層接在卷積層後面,所以輸入通常是feature map,依然是[batch, height, width, channels]這樣的shape

返回乙個tensor,型別不變,shape仍然是[batch, height, width, channels]這種形式

功能:生成乙個給定值的常量

tf.constant(value, dtype=none, shape=none, name='const', verify_shape=false)

功能:relu啟用函式

tf.nn.relu(features, name=none)

功能:將矩陣a 乘於 矩陣b(矩陣乘法)

tf.matmul(a, b, transpose_a=false, transpose_b=false, adjoint_a=false, adjoint_b=false, a_is_sparse=false, b_is_sparse=false, name=none)

返回值:

乙個跟張量a和張量b型別一樣的張量且最內部矩陣是a和b中的相應矩陣的乘積。如果所有的轉置或adjoint引數都為假,則公式為output[..., :, :] = a[..., :, :] * b[..., :, :]

功能:矩陣點乘

功能:訓練時隨機丟棄一部分資料來減輕過擬合

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

功能:softmax回歸,將logistic的**二分類的概率的問題推廣到了n分類的概率的問題。

tf.nn.softmax(logits,axis=none,name=none, dim=none)

功能:在tensor的某一維度上求和求最大值

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)

功能:轉換輸入型別

tf.cast(x,dtype,name=none)

返回:tensor

功能:新增節點用於初始化所有的變數(graphkeys.variables)。返回乙個初始化所有全域性變數的操作(op)。在你構建完整個模型並在會話中載入模型後,執行這個節點。

tf.global_variables_initializer()

功能:利用 l2 範數來計算張量的誤差值,output = sum(t ** 2) / 2

tf.nn.l2_loss( t, name=none )

功能:將張量新增到集合中

tf.add_to_collection(name,value)

功能:將偏差項bias加到value上

tf.nn.bias_add(value,bias,name=none)

返回乙個tensor,資料型別和value相同

功能:區域性響應標準化

tf.nn.lrn(input, depth_radius=none, bias=none, alpha=none, beta=none,name=none)

返回乙個tensor,資料型別和input相同

tf.contrib.layers.l1_regularizer(scale, scope=none)

功能:返回乙個用來執行l1正則化的函式,函式的簽名是func(weights).

tf.contrib.layers.l2_regularizer(scale, scope=none)

功能:返回乙個執行l2正則化的函式.

tensorflow學習 《基本用法》

最近嘗試用dqn ddqn演算法時,發現tensorflow部分有點模糊,正好忙完了一段時間亂七八糟的事,靜下來心重新完整的學一遍tensorflow 首先tensorflow是做圖計算的 階數學例項 數學例項 0純量 只有大小 s 483 1向量 大小和方向 v 1.1,2.2,3.3 2矩陣 資...

tensorflow2 0學習筆記3 常用函式

筆記基於北大的tensorflow2.0教程,將課程的一些重要內容記下來,方便於學習。一 常用函式 強制資料型別轉換 tf.cast 張量名,dtype 資料型別 找張量最小值 tf.reduce min 張量名 找張量最大值 tf.reduce max 張量名 二維張量中,可以通過調整axis控制...

Tensorflow2 0學習筆記(2) 常用函式

對應元素的加減乘除 次方 print tf.square a 平方 a tf.ones 3 2 b tf.fill 2 3 1.print tf.matmul a,b tf.matmul 矩陣1,矩陣2 data tf.data.dataset.from tensor slices 輸入特徵,標籤 ...