1. 程式人生 > >學習使用tf.contrib.learn框架開發機器學習程式

學習使用tf.contrib.learn框架開發機器學習程式

最近在學習用TensorFlow開發CNN的時候,看到官方的一篇教程Creating Estimators in tf.contrib.learn,介紹了使用封裝好的一些工具開發訓練程式的方法,覺得很不錯,在這裡記錄一下學習的心得。
tf.contrib下面的東西很多,google自己沒有稱之為框架,但我感覺藉助於tf.contrib.learn.Estimator,也等於是定義了一個框架,因為好多訓練程式的流程是差不多的。

文件中建議的流程如下:

  • Instantiate an Estimator 建立Estimator
  • Construct a custom model function 自定義模型函式
  • Configure a neural network using tf.contrib.layers 配置用到的Layers
  • Choose an appropriate loss function from tf.contrib.losses 定義計算loss的方法
  • Define a training op for your model 定義訓練(即收斂loss)方法
  • Generate and return predictions (我怎麼覺得計算prediction應該放到loss前面)

建立Estimator

系統中已經有一些現成的,在learn.estimator目錄下,比如:

  • LinearClassifier: Constructs a linear classification model.
  • LinearRegressor: Constructs a linear regression model.
  • DNNClassifier: Construct a neural network classification model.
  • DNNRegressor: Construct a neural network regressions model.

看樣子應該是可以直接就用了,但是暫時還沒有去研究。

自定義Estimator

nn = tf.contrib.learn.Estimator(model_fn=model_fn, model_dir=None
, config=None, params=model_params, feature_engineering_fn=None)
  • model_fn: 模型函式,具體後面再討論
  • model_dir: 指定log和引數儲存檔案的位置
  • config: 待研究
  • params: 傳遞給model_fn的自定義引數,dict形式
  • * feature_engineering_fn*: 待研究

原文的例子:

# Learning rate for the model
LEARNING_RATE = 0.001

# Set model params
model_params = {"learning_rate": LEARNING_RATE}

nn = tf.contrib.learn.Estimator(
    model_fn=model_fn, params=model_params)

定義模型函式(model_fn)

model_fn的引數是:

def model_fn(features, targets, mode, params):
   # Logic to do the following:
   # 1. Configure the model via TensorFlow operations
   # 2. Define the loss function for training/evaluation
   # 3. Define the training operation/optimizer
   # 4. Generate predictions
   return predictions, loss, train_op
  • features: 訓練資料,也就是x,格式沒有統一定義,取決於演算法的需要;由呼叫fit(), evaluate(), or predict()的時候傳遞進來;
  • targets: 訓練資料的介面,也就是y;fit,evaluate的時候會傳遞;
  • mode: 用來區分fit/evaluate/predict
    • tf.contrib.learn.ModeKeys.TRAIN
    • tf.contrib.learn.ModeKeys.EVAL
    • tf.contrib.learn.ModeKeys.INFER
  • params: 自定義引數,沒有可以不傳

返回結果是一個三元組:

  • predictions (required in INFER and EVAL modes)
    predictions = {"results": tensor_of_predictions}
    In INFER mode, the dict that you return from model_fn will then be returned by predict(), so you can construct it in the format in which you’d like to consume it. 也就是說這個prediction dict的格式沒有要求,根據需要來定義。
    In EVAL mode, the dict is used by metric functions to compute metrics. Any MetricSpec objects passed to the metrics argument of evaluate() must have a prediction_key that matches the key name of the corresponding predictions in predictions. 看這個意思,是說evaluate的時候,要指定要用predictions中的哪個key來做比較(是不是這樣,待驗證)

  • loss (required in EVAL and TRAIN mode). 這個就是通常意義的loss;

  • train_op (required only in TRAIN mode). An Op that runs one step of training.

定義Hidden Layers

對於卷積神經網路來說,一般就是卷積層和池化層,tf.contrib.layers下有一些定義好的類:

  • tf.contrib.layers.convolution2d
  • tf.contrib.layers.max_pool2d
  • tf.contrib.layers.avg_pool2d
  • ….

定義全連線層

系統有一些預定義的類:

  • relu(inputs, num_outputs). Create a layer of num_outputs nodes fully connected to the previous layer inputs with a ReLu activation function (tf.nn.relu):
    hidden_layer = tf.contrib.layers.relu(inputs=input_layer, num_outputs=10)

  • relu6(inputs, num_outputs). Create a layer of num_outputs nodes fully connected to the previous layer hidden_layer with a ReLu 6 activation function (tf.nn.relu6):
    second_hidden_layer = tf.contrib.layers.relu6(inputs=hidden_layer, num_outputs=20)

  • linear(inputs, num_outputs). Create a layer of num_outputs nodes fully connected to the previous layer second_hidden_layer with no activation function, just a linear transformation:
    output_layer = tf.contrib.layers.linear(inputs=second_hidden_layer, num_outputs=3)

當然還可以通過定製fully_connected的引數來定義自己需要的全連線層:

output_layer = tf.contrib.layers.fully_connected(inputs=second_hidden_layer, num_outputs=10, activation_fn=tf.sigmoid)

定義loss函式

tf.contrib.losses 下包含一些預定義的loss, 比如:

  • absolute_difference(predictions, targets). Calculates loss using the absolute-difference formula (also known as L1 loss).
  • log_loss(predictions, targets). Calculates loss using the logistic loss forumula (typically used in logistic regression).
  • mean_squared_error(predictions, targets). Calculates loss using the mean squared error (MSE; also known as L2 loss).

定義train_op

一般按這樣來定義:

train_op = tf.contrib.layers.optimize_loss(
loss=loss,
global_step=tf.contrib.framework.get_global_step(),
learning_rate=params["learning_rate"],
optimizer="SGD")

  • loss: 就是前面定義的loss函式
  • global_step: An integer Variable representing the step counter to increment for each model training run. Can easily be created/incremented in TensorFlow via the get_global_step() function (具體作用待研究)
  • learning_rate: 應該就是類似於梯度下降的步長之類的意思,比如0.01
  • optimizer: 優化方法,指定在tf.contrib.layers.optimizers中定義的方法,比如

    • SGD. Implementation of gradient descent (tf.train.GradientDescentOptimizer) 梯度下降
    • Adagrad. Implementation of the AdaGrad optimization algorithm (tf.train.AdagradOptimizer)
    • Adam. Implementation of the Adam optimization algorithm (tf.train.AdamOptimizer)
    • Ftrl. Implementation of the FTRL-Proximal (“Follow The (Proximally) Regularized Leader”) algorithm (tf.train.FtrlOptimizer)
    • Momentum. Implementation of stochastic gradient descent with momentum (tf.train.MomentumOptimizer)
    • RMSProp. Implementation of the RMSprop algorithm (tf.train.RMSPropOptimizer)

      除了名稱之外,還可以有多種方法指定optimizer,參見文件。

綜合以上的內容,一個完整的model_fn的例子:

def model_fn(features, targets, mode, params):
“”“Model function for Estimator.”“”

# Connect the first hidden layer to input layer
# (features) with relu activation
first_hidden_layer = tf.contrib.layers.relu(features, 10)

#Connect the second hidden layer to first hidden layer with relu
second_hidden_layer = tf.contrib.layers.relu(first_hidden_layer, 10)

# Connect the output layer to second hidden layer (no activation fn)
output_layer = tf.contrib.layers.linear(second_hidden_layer, 1)

# Reshape output layer to 1-dim Tensor to return predictions
predictions = tf.reshape(output_layer, [-1])
predictions_dict = {“ages”: predictions}

# Calculate loss using mean squared error
loss = tf.contrib.losses.mean_squared_error(predictions, targets)

train_op = tf.contrib.layers.optimize_loss(
loss=loss,
global_step=tf.contrib.framework.get_global_step(),
learning_rate=params[“learning_rate”],
optimizer=”SGD”)

return predictions_dict, loss, train_op

訓練,評估和預測

# Fit
nn.fit(x=training_set.data, y=training_set.target, steps=5000)

# Score accuracy
ev = nn.evaluate(x=test_set.data, y=test_set.target, steps=1)
loss_score = ev["loss"]
print("Loss: %s" % loss_score) 

# Print out predictions
predictions = nn.predict(x=prediction_set.data,
                         as_iterable=True)
for i, p in enumerate(predictions):
  print("Prediction %s: %s" % (i + 1, p["ages"]))

相關推薦

學習使用tf.contrib.learn框架開發機器學習程式

最近在學習用TensorFlow開發CNN的時候,看到官方的一篇教程Creating Estimators in tf.contrib.learn,介紹了使用封裝好的一些工具開發訓練程式的方法,覺得很不錯,在這裡記錄一下學習的心得。 tf.contrib下面

Scikit-Learn與TensorFlow機器學習(高清版)PDF

Scikit-Learn與TensorFlow機器學習(高清版)PDF百度網盤連結:https://pan.baidu.com/s/1MVQvrYc9Dx-bFXrDVWU3OQ 提取碼:03cj 複製這段內容後開啟百度網盤手機App,操作更方便哦內容簡介 · · · · · · 通過具體的例子、很少的理論以

Scikit-Learn 與 TensorFlow 機器學習實用指南學習筆記 4 —— 資料探索與視覺化、發現規律

紅色石頭的個人網站:redstonewill.com 目前為止,我們已經對資料有了初步的認識,大體上明白了我們要處理的資料型別。現在,我們將進入更深入的研究。 首先,確保已經劃分了測試集並放置一邊,我們只會對訓練集進行操作。另外,如果訓練集很大,可以從中取樣一些作

Scikit-Learn 與 TensorFlow 機器學習實用指南學習筆記 3 —— 資料獲取與清洗

紅色石頭的個人網站:redstonewill.com 本章將完整地介紹一個端對端(End-to-End)機器學習專案。假如你是某個房地產公司剛僱傭的資料科學家,你所要做的事情主要分成以下幾個步驟: 1.整體規劃。 2.獲取資料。 3.發現、視覺化資料,增加

Scikit-Learn 與 TensorFlow 機器學習實用指南學習筆記2 — 機器學習的主要挑戰

紅色石頭的個人網站:redstonewill.com 簡而言之,因為機器學習的主要任務就是選擇合適的機器學習演算法在資料集上進行訓練,所以不好的演算法和不好的資料都可能嚴重影響訓練效果。下面我們先來看看不好的資料會帶來什麼影響。 1.4.1 訓練資料不足

Scikit-Learn 與 TensorFlow 機器學習實用指南學習筆記1 — 機器學習基礎知識簡介

紅色石頭的個人網站:redstonewill.com 本章介紹的是每一個數據科學家都應該知道並聽說的機器學習許多基本的概念和術語。這將是一個高層次的概括(本書唯一沒有很多程式碼的一章)。內容很簡單,但是你要保證在進行下一章之前對本章每個概念都理解得很透徹。因此,端

Caffe框架機器學習——安裝與問題錦集

Ubuntu16.04   python   Faster R-CNN安裝教程:一、Caffe安裝教程:Ubuntu16.04(CPU) https://blog.csdn.net/u010193446/article/details/53259294二、Faster R-C

tensor flow 學習 tf.contrib.layers.flatten()和tf.contrib.layers.fully_connection()

tf.contrib.layers.flatten(P)這個函式就是把P保留第一個維度,把第一個維度包含的每一子張量展開成一個行向量,返回張量是一個二維的, shape=(batch_size,….),

『Python』MachineLearning機器學習入門_極小的機器學習應用

highlight 保存 數值 out 有意思 port del ear 解方程 一個小知識: 有意思的是,scipy囊括了numpy的命名空間,也就是說所有np.func都可以通過sp.func等價調用。 簡介: 本部分對一個互聯網公司的流量進行擬合處理,學習最基本的機器

機器學習】 Matlab 2015a 自帶機器學習算法匯總

dtree 決策 mat 可能 集成 模型訓練 貝葉斯 cdi top MATLAB機器學習沒看到啥教程,只有一系列函數,只好記錄下: MATLAB每個機器學習方法都有很多種方式實現,並可進行高級配置(比如訓練決策樹時設置的各種參數) ,這裏由於篇幅的限制,不再詳細描述。我

【深度學習】一文讀懂機器學習常用損失函數(Loss Function)

back and 們的 wiki 導出 歐氏距離 classes 自變量 關於 最近太忙已經好久沒有寫博客了,今天整理分享一篇關於損失函數的文章吧,以前對損失函數的理解不夠深入,沒有真正理解每個損失函數的特點以及應用範圍,如果文中有任何錯誤,請各位朋友指教,謝謝~

大數據分析學習之使用R語言實戰機器學習視頻課程

https aid 通過 原理 har fsg follow mdf 學習 大數據分析學習之使用R語言實戰機器學習網盤地址:https://pan.baidu.com/s/1Yi9H6s8Eypg_jJJlQmdFSg 密碼:0jz3備用地址(騰訊微雲):https://s

【吳恩達機器學習隨筆】什麽是機器學習

都是 mea 預測 learn 會有 code 度量 its 價格 定義    Tom Mitchell對機器學習定義為“計算機從經驗E中學習,解決某一任務T,進行某一度量P,通過P測定在T上的表現因經驗E而提高”。定義個人覺得大體理解即可,如果扣文咬字去理解會十分痛苦,就

機器學習機器學習分類器模型評價指標 機器學習分類器模型評價指標

機器學習分類器模型評價指標 分類器評價指標主要有: 1,Accuracy 2,Precision  3,Recall  4,F1 score  5,ROC 曲線

機器學習】一文讀懂機器學習常用損失函式

損失函式(loss function)是用來估量模型的預測值f(x)與真實值Y的不一致程度,它是一個非負實值函式,通常使用L(Y, f(x))來表示,損失函式越小,模型的魯棒性就越好。損失函式是經驗風險函式的核心部分,也是結構風險函式重要組成部分。模型的結構風險函式包括了經驗風險項和正則項,通常可以

吳恩達機器學習 - 推薦系統 吳恩達機器學習 - 推薦系統

原 吳恩達機器學習 - 推薦系統 2018年06月25日 22:26:51 離殤灬孤狼 閱讀數:187

吳恩達機器學習 - 異常檢測 吳恩達機器學習 - 異常檢測

原 吳恩達機器學習 - 異常檢測 2018年06月25日 21:09:33 離殤灬孤狼 閱讀數:69

吳恩達機器學習 - 評估假設 吳恩達機器學習 - 評估假設

原 吳恩達機器學習 - 評估假設 2018年06月22日 20:47:29 離殤灬孤狼 閱讀數:105

HIT機器學習期末複習(1)——機器學習簡介及決策樹

劉楊的機器學習終於上完了惹,下週就要考試了,趕緊複習ing...... 趁機做個總結,就當是複習了惹...... 機器學習簡介 1、什麼是機器學習 簡單來說,就是一個三元組<P, T, E> P——performance效能(對應著效能的評估函式,也就是常說的loss或者likeli

吳恩達機器學習 - 邏輯迴歸 吳恩達機器學習 - 邏輯迴歸

原 吳恩達機器學習 - 邏輯迴歸 2018年06月19日 12:49:09 離殤灬孤狼 閱讀數:96 更多