张量流随机森林回归

tensorflow random forest regression

我想实现一个简单的随机森林回归来预测一个值。输入是一些具有多个特征的样本,标签是一个值。但是,我找不到关于随机森林回归问题的简单示例。于是,我看了tensorflow的文档,发现:

可以训练和评估随机森林的估计器。 示例:

  python
  params = tf.contrib.tensor_forest.python.tensor_forest.ForestHParams(
      num_classes=2, num_features=40, num_trees=10, max_nodes=1000)
  # Estimator using the default graph builder.
  estimator = TensorForestEstimator(params, model_dir=model_dir)
  # Or estimator using TrainingLossForest as the graph builder.
  estimator = TensorForestEstimator(
      params, graph_builder_class=tensor_forest.TrainingLossForest,
      model_dir=model_dir)
  # Input builders
  def input_fn_train: # returns x, y
    ...
  def input_fn_eval: # returns x, y
    ...
  estimator.fit(input_fn=input_fn_train)
  estimator.evaluate(input_fn=input_fn_eval)
  # Predict returns an iterable of dicts.
  results = list(estimator.predict(x=x))
  prob0 = results[0][eval_metrics.INFERENCE_PROB_NAME]
  prediction0 = results[0][eval_metrics.INFERENCE_PRED_NAME]

但是,当我按照示例进行时,我得到了在线错误,prob0 = results[0][eval_metrics.INFERENCE_PROB_NAME],错误显示:

Example conversion:
est = Estimator(...) -> est = SKCompat(Estimator(...))
Traceback (most recent call last):
  File "RF_2.py", line 312, in <module>
    main()
  File "RF_2.py", line 298, in main
    train_eval(x_train, y_train, x_validation, y_validation, x_test, y_test, num_tree)
  File "RF_2.py", line 221, in train_eval
    prob0 = results[0][eval_metrics.INFERENCE_PROB_NAME]
KeyError: 'probabilities'

我认为错误发生在 INFERENCE_PROB_NAME,我看到了 document。但是,我还是不知道用什么词来代替INFERENCE_PROB_NAME

我已经尝试 get_metric('accuracy') 替换 INFERENCE_PROB_NAME,它 return 错误:KeyError: <function _accuracy at 0x11a06eaa0>.

我也试过get_prediction_key('accuracy')替换INFERENCE_PROB_NAME,它return错误:KeyError: 'classes'.

如果你知道可能的答案,请告诉我。提前谢谢你。

我认为你是在无意中做一个分类问题,给出了一个错误的 num_classes=2 而没有改变 regression=False 的默认值。请参阅 参数 部分 here。作为快速测试,设置 num_classes=0regression=True,然后重新 运行 您的代码。

num_classes=0 在 tensorflow 1.3.0 中是错误的。

来自 Mehdi Rezaie 的 link,num_classes 是回归问题输出中的维数。

您必须为 num_classes 使用 num_classes=1 或更大的值。 或者你会得到像 ValueError: Invalid logits_dimension 0.

这样的错误