tensorflow使用教程

趁着最近有點時間,花幾天時間把tensorflow教程整理出來(紅色表示沒有完成)

(內容來源,tensorflow google操作手冊,英文,鏈接:https://tensorflow.google.cn/programmers_guide/estimators,需要翻牆)現在的版本是1.6

High level apis:

estimators是一個高級接口,極大簡化了機器學習的編程,它能夠實現:訓練,評估,預測,export for serving

兩種使用方法:1:使用pre-made estimators,2:自己寫(custom) estimators  都基於(tf.estimator.Esrimator類)

優點:可以使用local host也可使用分佈式集成環境,可在CPU GPU TPU上直接運行;簡化開發人員的交流;使用estimators比low-level的Tensorflow API更容易;自己創建圖,不用人工創建;提供安全的distributed training loop去控制how when:建graph 初始化變量;創建checkpoint ,save summaries給tensorBoard去可視化。用estimarots寫app的時候,比如把數據輸入管道和模型分開,這種隔離簡化了應用不同數據集時候的實驗。

pre-made estimators:

不需要自己創建計算圖或者sessions(低層接口的使用方法),estimators全都自己處理了,稍稍改動就可以使用不同模型。

structure:主要包括以下4點:

一,寫一個或多個數據集載入函數,類如:你可以創建一個函數用於載入數據集,另一個函數載入測試集。每一個數據集載入函數需要返回兩個對象:1:一個字典,keys:特徵名,values: 張量,存相應的特徵數據。2:一個張量,存一個或者多個標籤。(詳細見Importing Data,https://tensorflow.google.cn/programmers_guide/datasets)

def input_fn(dataset):
   
...  # manipulate dataset, extracting feature names and the label

   return feature_dict, label

二,定義feature columns,每一個tf.feature_column(https://tensorflow.google.cn/api_docs/python/tf/feature_column)定義一個特徵名,類型,和輸入的預處理。就是給特徵起個名,在編程的時候直接調用名稱,不用在乎具體張量shape。

# Define three numeric feature columns.
population
= tf.feature_column.numeric_column('population')
crime_rate
= tf.feature_column.numeric_column('crime_rate')
median_education
= tf.feature_column.numeric_column('median_education',
                    normalizer_fn
='lambda x: x - global_education_mean')

三,實例化一個pre-made estimator.舉例:實例化一個estimator叫linearclassifier:

# Instantiate an estimator, passing the feature columns.
estimator
= tf.estimator.Estimator.LinearClassifier(
    feature_columns
=[population, crime_rate, median_education],
   
)

四,調用training,evaluation ,or inference method.舉例:

 
# my_training_set is the function created in Step 1
estimator
.train(input_fn=my_training_set, steps=2000)


estimator的核心是model function,一個建立graphs去訓練評價預測的函數。pre-made以及有人寫好,custom estimators得自己寫()https://tensorflow.google.cn/get_started/custom_estimators

建議得工作流:如果一個pre-made estimator存在的話,直接用它建立模型用其結果建立一個baseline(對比)

創建,用pre-made去測試整個管道的完整性和可靠性(對數據來說)

適當的改變pre-made,參數或者模型,看看哪一個能得出更好的結果

可能的話,建立custom estimator去更進一步提高模型。



發佈了30 篇原創文章 · 獲贊 14 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章