[关闭]
@zqbinggong 2018-05-29T05:44:41.000000Z 字数 4951 阅读 992

tf.Learn

《tensorflow实战》


分布式Estimator

自定义模型介绍

  1. 包含各种各样的机器学习和深度学习的类
  2. 接受自定义模型,目前接受以下几种不同的函数签名:
    • (features, targert)--> (predictions, loss, train_op)
    • (features, targert, mode)--> (predictions, loss, train_op)
    • (features, targert, mode, params)--> (predictions, loss, train_op)
      • 其中mode可以被用来定义函数的使用阶段,如training、evaluating以及prediction,这些常用的模式可以在ModeKeys里找到
      • params是可以由自定义模型来调节的参数,使用fit函数时可以给更多的参数
  1. import tensorflow as tf
  2. from tensorflow.contrib import layers
  3. from tensorflow.contrib import learn
  4. from sklearn import datasets, cross_validation
  5. def my_model(features, target):
  6. target = tf.one_hot(target, 3, 1, 0)
  7. # 堆叠神经网络,,每一层分别有10,20,10个隐藏节点
  8. features = layers.stack(features, layers.fully_connected, [10,20,10])
  9. prediction, loss = learn.models.logistic_regression_zero_init(features, target)
  10. train_op = layers.optimize_loss(
  11. loss, tf.contrib.framework.get_global_step(), optimizer='Adagrad', learning_rate=0.1)
  12. return {'class': tf.argmax(prediction, 1), 'prop': prediction}, loss, train_op
  13. iris = datasets.load_iris()
  14. x_train, x_test, y_train, y_test = cross_validation.train_test_split(
  15. iris.data, iris.target, test_size=0.2, random_state=35)
  16. classifier = learn.Estimator(model_fn=my_model)
  17. classifier.fit(x_train, y_train, steps=700)
  18. predictions = classifier.predict(x_test)

建立自己的机器学习Estimator

learn.Estimate类(继承了BaseEstimator)

  1. 参数:
    • model_fn
    • model_dir
    • config : configuration object
    • params : will be passed into 'model_fn'
    • feature_engineering_fn
  2. 基本函数:
    • _get_train_op(features, labels): 被用来在每个训练迭代时对模型的参数进行优化,如果想要实现自己的Estimator,可以修改复写这个函数来实现自己的逻辑
    • _get_eval_ops(features, labels, metrics): 让BaseEstimator的子类来使用自定义的metrics评估每个模型训练的迭代,在contrib.metrics可以找到许多直接使用的metrics
    • _get_predict_ops(features), 用来实现自定义的预测,在这个函数里可以对预测值进行进一步的处理,比如把概率转化成预测结果
  3. TensorForestEstimator

调节RunConfig运行时参数

  1. RunConfig时Learn的一个类,用来帮助用户调节运行时参数,例如:
    • num_cores : 选择使用核的数量
    • num_ps_replicas : 服务器的数量
  2. 2.

Experiment 和 LearnRunner

  1. 前者是一个简单易用的建立模型实验的类,包含了建模所需要的所有信息,例如Estimator,训练数据,评估指标等
  2. 后者是用来方便做实验的一个模块

深度学习Estimator

深度神经网络

以DNNClassifier为例:

  1. # 此处在fit和evaluate中还有很多其他参数,可以实现更多的自定义逻辑
  2. # 先在_input_fn里建立数据,使用layers模块建立两个特征列--年龄和性别
  3. def _input_fn(num_epochs=None):
  4. features = {'age': tf.train.limit_epochs(tf.constant([[.8],[.2],[.1]]), num_epochs=num_epochs),
  5. # 返回tensor num_epochs次, 并raise on 'OutOfRange' error
  6. 'language': tf.SparseTensor(values=['en', 'fr', 'zh'],
  7. indices=[[0, 0], [0, 1], [2, 0]], dense_shape=[3, 2])
  8. # dense.shape = dense_shape, dense[tuple(indices[i])] = values[i]
  9. }
  10. return features, tf.constant([[1], [0], [0]], dtype=tf.int32) # 特征和label
  11. language_column = tf.contrib.layers.sparse_column_with_hash_bucket(
  12. 'language', hash_bucket_size=20)
  13. feature_columns = [
  14. tf.contrib.layers.embedding_column(language_column, dimension=1),
  15. tf.contrib.layers.real_valued_column('age')
  16. ]
  17. # 将特征列、每层的隐藏神经元数、标识类别数等传入DNNClassifier里建立模型
  18. classifier = tf.contrib.learn.DNNClassifier(
  19. n_classes=2,
  20. feature_columns=feature_columns,# 注意这里feature_columns相当于placeholder,它需要fit()接收的参数input_fn的返回值作为feed
  21. # weight_column_name= , 考虑数据带有权重,这时需要将权重也加入到feature_columns
  22. hidden_units=[3, 3],
  23. config=tf.contrib.learn.RunConfig(tf_random_seed=1)
  24. )
  25. classifier.fit(input_fn=_input_fn, steps=100)
  26. scores = classifier.evaluate(input_fn=_input_fn, steps=1)

广度深度模型

  1. 深度神经网络和logistic regression的结合,谷歌研究发现,将不同的特征通过两种不同的方式结合在一起,更能体现应用的意义以及更有效的推荐结果,这类似于Ensemble
  2. 与DNNClassifier和LinearClassifier相比有更多的参数可以受用,并且可以将不同特征列选择使用到DNNClassifier或者LinearClassifier中,比如讲上述的年龄交给DNN,语言交给Linear

机器学习Estimator


DataFrame


监督器Monitors

  1. 提供各种logging及监督控制训练的过程,以便让用户清楚的知道模型是否在进行有效的训练
  2. tf有5个等级的log, 按严重性从小到达为:debug, info, warn, error, fatal, 通过tf.logging.set_verbosity(tf.logging.INFO)将等级改为INFO
  3. 高阶Monitor类:
    • 用CaptureVariable将一个指定的变量的值存储到一个Collection里
    • 用PrintTensor打印Tensor的值
    • 用SummarySaver存储Summary所需的protocol buffer
    • 用ValidationMonitor在训练时打印多个评估Metrics,以及监督模型的训练以便停止训练防止模型的过拟合
  4. 事实上, 可以通过tensotboard可视化生成的log和checkpoint文件
  5. 以ValidationMonitor为例:
  1. # load the data
  2. iris_trian = tf.contrib.learn.datasets.base.load_csv(filename='', target_dtype=np.int)
  3. iris_test = tf.contrib.learn.datasets.base.load_csv(filename='', target_dtype=np.int)
  4. # define a metrics dict to evaluate the model
  5. validation_metrics = {'accuracy':tf.contrib.metrics.streaming_accuracy,
  6. 'precision': tf.contrib.metrics.streaming_precision,
  7. 'recall': tf.contrib.metrics.streaming_recall}
  8. # use the metrics-dict to construct the validation_monitor
  9. validation_monitor = tf.contrib.learn.monitors.ValidationMonitor(
  10. iris_test.data,
  11. iris_test.target, # offer the data and target to estimate the model
  12. every_n_steps=50, # run this monitor every 50 steps
  13. metrics=validation_metrics,
  14. early_stopping_metric='loss', # early stopping depending on the 'loss' metric
  15. early_stopping_metric_minimize=True, # if True, we should minimize the early_stopping_metric
  16. early_stopping_rounds=200
  17. )
  18. # next, we construct a DNNClassifier,
  19. # which has 3 layers and the number of hidden units of each layer are 10,15,10
  20. # note that there we can assign multiple monitors to monitor different functions
  21. classifier = tf.contrib.learn.DNNClassifier(
  22. feature_columns=feature_columns,
  23. hidden_units=[10, 15, 10],
  24. n_classes=3,
  25. model_dir='',
  26. config=tf.contrib.learn.RunConfig(save_checkpoints_secs=2)
  27. )
  28. classifier.fit(x=iris_trian.data, y=iris_train.target, steps=10000, monitors=[validation_monitor])
  29. accuracy_score = classifier.evaluate(x=iris_test.data, y=iris_test.target)['accuracy']
  30. # corresponding to validation_metrics dict.
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注