簡體   English   中英

使用 tensorflow 和提升樹的分布式學習示例

[英]Example for Distributed learning using tensorflow and boosted trees

我遍歷了許多 github,但是我找不到 Tensorflow 分布式學習與 Boosted Trees Classifier 估計器一起使用的示例。 所有教程都是針對神經網絡的。

我稍微調整了增強樹代碼以使用分布式策略,如下所示:

import numpy as np
import pandas as pd
from IPython.display import clear_output
from matplotlib import pyplot as plt
import tensorflow as tf
tf.random.set_seed(123)

dftrain = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/train.csv')
dfeval = pd.read_csv('https://storage.googleapis.com/tf-datasets/titanic/eval.csv')
y_train = dftrain.pop('survived')
y_eval = dfeval.pop('survived')

fc = tf.feature_column
CATEGORICAL_COLUMNS = ['sex', 'n_siblings_spouses', 'parch', 'class', 'deck',
                       'embark_town', 'alone']
NUMERIC_COLUMNS = ['age', 'fare']

def one_hot_cat_column(feature_name, vocab):
  return tf.feature_column.indicator_column(
      tf.feature_column.categorical_column_with_vocabulary_list(feature_name,
                                                 vocab))
feature_columns = []
for feature_name in CATEGORICAL_COLUMNS:
  # Need to one-hot encode categorical features.
  vocabulary = dftrain[feature_name].unique()
  feature_columns.append(one_hot_cat_column(feature_name, vocabulary))

for feature_name in NUMERIC_COLUMNS:
  feature_columns.append(tf.feature_column.numeric_column(feature_name,
                                           dtype=tf.float32))

NUM_EXAMPLES = len(y_train)

def make_input_fn(X, y, n_epochs=None, shuffle=True):
  def input_fn():
    dataset = tf.data.Dataset.from_tensor_slices((dict(X), y))
    if shuffle:
      dataset = dataset.shuffle(NUM_EXAMPLES)
    # For training, cycle thru dataset as many times as need (n_epochs=None).
    dataset = dataset.repeat(n_epochs)
    # In memory training doesn't use batching.
    dataset = dataset.batch(NUM_EXAMPLES)
    return dataset
  return input_fn

# Training and evaluation input functions.
train_input_fn = make_input_fn(dftrain, y_train)
eval_input_fn = make_input_fn(dfeval, y_eval, shuffle=False, n_epochs=1)

n_batches = 10
mirrored_strategy = tf.distribute.MirroredStrategy()
config = tf.estimator.RunConfig(
    train_distribute=mirrored_strategy, eval_distribute=mirrored_strategy)
est = tf.estimator.BoostedTreesClassifier(feature_columns,
                                          n_batches_per_layer=n_batches,config=config)

  # The model will stop training once the specified number of trees is built, not
  # based on the number of steps.
est.train(train_input_fn, max_steps=100)

# Eval.
result = est.evaluate(eval_input_fn)
clear_output()
print(pd.Series(result))


但是,每當我運行此代碼時,我都會收到錯誤消息:

在定義新圖或merge_call時調用 merge_call。 This can often happen if the function fn passed to strategy.experimental_run() is decorated with @tf.function (or contains a nested @tf.function ), and fn contains a synchronization point, such as aggregating gradients. 尚不支持此行為。 相反,請將整個調用strategy.experimental_run(fn)包裝在@tf.function中,並避免可能跨越同步邊界的嵌套tf.function

因此,如果我能找到一種方法來調試此錯誤或找到一個使用 Boosted Trees 進行分布式學習的示例,我將不勝感激。

在查看圖書館作者的論文時,我偶然發現了一個答案: https://arxiv.org/abs/1710.11555

在他們的論文中,他們包含了分布式訓練環境的圖像,其中特別包含了幾個參數服務器。 分布式環境設置

tensorflow 在您的問題中抱怨的特定merge_call事情是我認為必須在與工作人員不同的設備上完成的事情。 所以基本上如果你想在分布式環境中訓練森林,你的 runconfig 必須看起來像

strategy = tf.distribute.experimental.ParameterServerStrategy()
config = tf.estimator.RunConfig(
    train_distribute=strategy)

並且您必須弄清楚如何正確設置 TF_CONFIG 以便工作 tensorflow 進程知道主服務器和參數服務器。 如果你使用谷歌人工智能平台,他們會在你開始工作時設置這些東西。 我還沒有嘗試讓它在本地或非 AI 平台集群上工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM