自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

TensorFlow廣度和深度學習的教程

人工智能 深度學習
在這篇文章中,我們將會介紹如何使用 TF.Learn API 同時訓練一個廣度線性模型和一個深度前饋神經網絡。這種方法結合了記憶和泛化的優(yōu)勢。它在一般的大規(guī)?;貧w和具有稀疏輸入特性的分類問題(例如,分類特征存在一個很大的可能值域)上很有效。

[[211918]]

在這篇文章中,我們將會介紹如何使用 TF.Learn API 同時訓練一個廣度線性模型和一個深度前饋神經網絡。這種方法結合了記憶和泛化的優(yōu)勢。它在一般的大規(guī)?;貧w和具有稀疏輸入特性的分類問題(例如,分類特征存在一個很大的可能值域)上很有效。如果你有興趣學習更多關于廣度和深度學習如何工作的問題,請參考 研究論文 Wide & Deep Spectrum of Models現在,我們來看一個簡單的例子。

上圖展示了廣度模型(具有稀疏特征和轉換性質的 logistic 回歸模型),深度模型(具有一個嵌入層和多個隱藏層的前饋神經網絡),廣度和深度模型(兩者的聯合訓練)的區(qū)別比較。在高層級里,只需要通過以下三個步驟就能使用 TF.Learn API 配置廣度,深度或廣度和深度模型。

  1. 選擇廣度部分的特征:選擇要使用的稀疏基本列和交叉列。

  2. 選擇深度部分的特征:選擇連續(xù)列,每個分類列的嵌入維度和隱藏層大小。

  3. 將它們一起放入廣度和深度模型(DNNLinearCombinedClassifier)。

安裝

如果想要嘗試本教程中的代碼:

1.安裝 TensorFlow ,請前往此處

2.下載 教程代碼。

3.安裝 pandas 數據分析庫。因為本教程中需要使用 pandas 數據。雖然 tf.learn 不要求 pandas,但是它支持 pandas。安裝 pandas:

a. 獲取 pip:

  1. # Ubuntu/Linux 64-bit 
  2. $ sudo apt-get install python-pip python-dev 
  3.  
  4. # Mac OS X 
  5. $ sudo easy_install pip 
  6. $ sudo easy_install --upgrade six 

b. 使用 pip 安裝 pandas

  1. $ sudo pip install pandas 

如果你在安裝過程中遇到問題,請前往 pandas 網站上的 說明 。

4.執(zhí)行以下命令來訓練教程中描述的線性模型:

  1. $ python wide_n_deep_tutorial.py --model_type=wide_n_deep 

請繼續(xù)閱讀,了解此代碼如何構建其線性模型。

定義基本特征列

首先,定義我們使用的基本分類和連續(xù)特征的列。這些列將被作為模型的廣度部分和深度部分的構件塊。

  1. import tensorflow as tf 
  2.  
  3. gender = tf.feature_column.categorical_column_with_vocabulary_list( 
  4.  "gender", ["Female""Male"]) 
  5. education = tf.feature_column.categorical_column_with_vocabulary_list( 
  6.  "education", [ 
  7.  "Bachelors""HS-grad""11th""Masters""9th"
  8.  "Some-college""Assoc-acdm""Assoc-voc""7th-8th"
  9.  "Doctorate""Prof-school""5th-6th""10th""1st-4th"
  10.  "Preschool""12th" 
  11.  ]) 
  12. marital_status = tf.feature_column.categorical_column_with_vocabulary_list( 
  13.  "marital_status", [ 
  14.  "Married-civ-spouse""Divorced""Married-spouse-absent"
  15.  "Never-married""Separated""Married-AF-spouse""Widowed" 
  16.  ]) 
  17. relationship = tf.feature_column.categorical_column_with_vocabulary_list( 
  18.  "relationship", [ 
  19.  "Husband""Not-in-family""Wife""Own-child""Unmarried"
  20.  "Other-relative" 
  21.  ]) 
  22. workclass = tf.feature_column.categorical_column_with_vocabulary_list( 
  23.  "workclass", [ 
  24.  "Self-emp-not-inc""Private""State-gov""Federal-gov"
  25.  "Local-gov""?""Self-emp-inc""Without-pay""Never-worked" 
  26.  ]) 
  27.  
  28. # 展示一個哈希的例子: 
  29. occupation = tf.feature_column.categorical_column_with_hash_bucket( 
  30.  "occupation", hash_bucket_size=1000) 
  31. native_country = tf.feature_column.categorical_column_with_hash_bucket( 
  32.  "native_country", hash_bucket_size=1000) 
  33.  
  34. # 連續(xù)基列 
  35. age = tf.feature_column.numeric_column("age"
  36. education_num = tf.feature_column.numeric_column("education_num"
  37. capital_gain = tf.feature_column.numeric_column("capital_gain"
  38. capital_loss = tf.feature_column.numeric_column("capital_loss"
  39. hours_per_week = tf.feature_column.numeric_column("hours_per_week"
  40.  
  41. # 轉換 
  42. age_buckets = tf.feature_column.bucketized_column( 
  43.  age, boundaries=[18, 25, 30, 35, 40, 45, 50, 55, 60, 65]) 

廣度模型:具有交叉特征列的線性模型

廣度模型是一個具有稀疏和交叉特征列的線性模型:

  1. base_columns = [ 
  2.  gender, native_country, education, occupation, workclass, relationship, 
  3.  age_buckets, 
  4.  
  5. crossed_columns = [ 
  6.  tf.feature_column.crossed_column( 
  7.  ["education""occupation"], hash_bucket_size=1000), 
  8.  tf.feature_column.crossed_column( 
  9.  [age_buckets, "education""occupation"], hash_bucket_size=1000), 
  10.  tf.feature_column.crossed_column( 
  11.  ["native_country""occupation"], hash_bucket_size=1000) 

具有交叉特征列的廣度模型可以有效地記憶特征之間的稀疏交互。也就是說,交叉特征列不能概括沒有在訓練數據中出現的特征組合。讓我們采用嵌入方式來添加一個深度模型來修復這個問題。

深度模型:嵌入式神經網絡

深度模型是一個前饋神經網絡,如前圖所示。每一個稀疏,高維度分類特征首先都會被轉換成一個低維度密集的實值矢量,通常被稱為嵌入式矢量。這些低維度密集的嵌入式矢量與連續(xù)特征相連,然后在正向傳遞中饋入神經網絡的隱藏層。嵌入值隨機初始化,并與其他模型參數一起訓練,以***化減少訓練損失。如果你有興趣了解更多關于嵌入的知識,請在查閱教程 Vector Representations of Words 或在 Wikipedia 上查閱 Word Embedding。

我們將使用 embedding_column 配置分類嵌入列,并將它們與連續(xù)列連接:

 

  1. deep_columns = [ 
  2.  tf.feature_column.indicator_column(workclass), 
  3.  tf.feature_column.indicator_column(education), 
  4.  tf.feature_column.indicator_column(gender), 
  5.  tf.feature_column.indicator_column(relationship), 
  6.  # 展示一個嵌入例子 
  7.  tf.feature_column.embedding_column(native_country, dimension=8), 
  8.  tf.feature_column.embedding_column(occupation, dimension=8), 
  9.  age, 
  10.  education_num, 
  11.  capital_gain, 
  12.  capital_loss, 
  13.  hours_per_week, 

嵌入的 dimension 越高,自由度就越高,模型將不得不學習這些特性的表示。為了簡單起見,我們設置所有特征列的維度為 8。從經驗上看,關于維度的設定***是從 \log_{2}(n) 或 k\sqrt[4]{n} 值開始,這里的 n 代表特征列中唯一特征的數量,k 是一個很小的常量(通常小于10)。

通過密集嵌入,深度模型可以更好的概括,并更好對之前沒有在訓練數據中遇見的特征進行預測。然而,當兩個特征列之間的底層交互矩陣是稀疏和高等級時,很難學習特征列的有效低維度表示。在這種情況下,大多數特征對之間的交互應該為零,除了少數幾個,但密集的嵌入將導致所有特征對的非零預測,從而可能過度泛化。另一方面,具有交叉特征的線性模型可以用更少的模型參數有效地記住這些“異常規(guī)則”。

現在,我們來看看如何聯合訓練廣度和深度模型,讓它們優(yōu)勢和劣勢互補。

將廣度和深度模型結合為一體

通過將其最終輸出的對數幾率作為預測結合起來,然后將預測提供給 logistic 損失函數,將廣度模型和深度模型相結合。所有的圖形定義和變量分配都已經被處理,所以你只需要創(chuàng)建一個 DNNLinearCombinedClassifier:

  1. import tempfile 
  2. model_dir = tempfile.mkdtemp() 
  3. m = tf.contrib.learn.DNNLinearCombinedClassifier( 
  4.  model_dir=model_dir, 
  5.  linear_feature_columns=wide_columns, 
  6.  dnn_feature_columns=deep_columns, 
  7.  dnn_hidden_units=[100, 50]) 

訓練和評估模型

在訓練模型之前,請先閱讀人口普查數據集,就像在 《TensorFlow Liner Model Tutorial》 中所做的一樣。 輸入數據處理的代碼再次為你提供方便:

  1. import pandas as pd 
  2. import urllib 
  3.  
  4. # 為數據集定義列名 
  5. CSV_COLUMNS = [ 
  6.  "age""workclass""fnlwgt""education""education_num"
  7.  "marital_status""occupation""relationship""race""gender"
  8.  "capital_gain""capital_loss""hours_per_week""native_country"
  9.  "income_bracket" 
  10.  
  11. def maybe_download(train_data, test_data): 
  12.  """Maybe downloads training data and returns train and test file names.""" 
  13.  if train_data: 
  14.  train_file_name = train_data 
  15.  else
  16.  train_file = tempfile.NamedTemporaryFile(delete=False
  17.  urllib.request.urlretrieve( 
  18.  "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data"
  19.  train_file.name) # pylint: disable=line-too-long 
  20.  train_file_name = train_file.name 
  21.  train_file.close() 
  22.  print("Training data is downloaded to %s" % train_file_name) 
  23.  
  24.  if test_data: 
  25.  test_file_name = test_data 
  26.  else
  27.  test_file = tempfile.NamedTemporaryFile(delete=False
  28.  urllib.request.urlretrieve( 
  29.  "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.test"
  30.  test_file.name) # pylint: disable=line-too-long 
  31.  test_file_name = test_file.name 
  32.  test_file.close() 
  33.  print("Test data is downloaded to %s"% test_file_name) 
  34.  
  35.  return train_file_name, test_file_name 
  36.  
  37. def input_fn(data_file, num_epochs, shuffle): 
  38.  """Input builder function.""" 
  39.  df_data = pd.read_csv( 
  40.  tf.gfile.Open(data_file), 
  41.  names=CSV_COLUMNS, 
  42.  skipinitialspace=True
  43.  engine="python"
  44.  skiprows=1) 
  45.  # 移除 NaN 元素 
  46.  df_data = df_data.dropna(how="any", axis=0) 
  47.  labels = df_data["income_bracket"].apply(lambda x: ">50K" in x).astype(int
  48.  return tf.estimator.inputs.pandas_input_fn( 
  49.  x=df_data, 
  50.  y=labels, 
  51.  batch_size=100, 
  52.  num_epochs=num_epochs, 
  53.  shuffle=shuffle, 
  54.  num_threads=5) 

閱讀數據之后,你可以訓練并評估模型:

 

  1. # 將 num_epochs 設置為 None,以獲得***的數據流 
  2. m.train( 
  3.  input_fn=input_fn(train_file_name, num_epochs=None, shuffle=True), 
  4.  steps=train_steps) 
  5. # 在所有數據被消耗之前,為了運行評估,設置 steps 為 None 
  6. results = m.evaluate( 
  7.  input_fn=input_fn(test_file_name, num_epochs=1, shuffle=False), 
  8.  steps=None) 
  9. print("model directory = %s" % model_dir) 
  10. for key in sorted(results): 
  11.  print("%s: %s" % (key, results[key])) 

輸出的***行應該類似 accuracy: 0.84429705。我們可以看到使用廣度和深度模型將廣度線性模型精度約 83.6% 提高到了約 84.4%。如果你想看端對端的工作示例,你可以下載我們的 示例代碼

 

請注意,本教程只是一個小型數據基的簡單示例,為了讓你快速熟悉 API。如果你有大量具有稀疏特征列和大量可能特征值的數據集,廣度和深度學習將會更加強大。此外,請隨時關注我們的研究論文,以了解更多關于在實際中廣度和深度學習在大型機器學習方面如何應用的思考。 

責任編輯:龐桂玉 來源: 36大數據
相關推薦

2015-08-24 15:06:13

大數據

2017-08-10 15:31:57

Apache Spar TensorFlow

2017-05-22 13:15:45

TensorFlow深度學習

2018-04-09 10:20:32

深度學習

2016-12-06 08:51:48

深度學習TensorFlow機器學習

2018-09-06 08:00:00

深度學習TensorFlowPython

2011-04-12 16:40:40

C++復雜度

2017-08-16 10:57:52

深度學習TensorFlowNLP

2016-12-23 09:09:54

TensorFlowKubernetes框架

2017-05-03 22:05:48

深度學習候選采樣深度學習庫

2017-05-12 16:25:44

深度學習圖像補全tensorflow

2017-06-06 10:14:55

KerasTensorFlow深度學習

2017-02-21 10:00:44

大數據深度學習框架對比

2023-05-14 22:35:24

TensorFlowKeras深度學習

2022-11-13 08:11:03

TensorFlow人工智能開源

2017-09-21 12:29:58

深度學習TensorFlow智能終端

2018-04-11 17:50:14

深度學習PyTorchTensorFlow

2017-03-01 19:58:00

深度學習TensorFlow

2018-04-11 09:30:41

深度學習

2018-04-16 11:30:32

深度學習
點贊
收藏

51CTO技術棧公眾號