简体   繁体   English

张量流随机森林回归

[英]tensorflow random forest regression

I would like to implement a simple random forest regression to predict a value. 我想实现一个简单的随机森林回归来预测一个值。 The inputs are some samples with several features, and the label is a value. 输入是一些具有多个特征的样本,标签是一个值。 However, I cannot find a simple example about the random forest regression problem. 但是,我找不到关于随机森林回归问题的简单例子。 Thus, I saw the document of tensorflow and I found that: 因此,我看到了tensorflow文件,我发现:

An estimator that can train and evaluate a random forest. 可以训练和评估随机森林的估算器。 Example: 例:

  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]

However, when I follow the example, I got the error on the line, prob0 = results[0][eval_metrics.INFERENCE_PROB_NAME] , the error shows that: 但是,当我按照示例时,我在行上得到错误, 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'

I think the error occurs on INFERENCE_PROB_NAME , and I saw the document . 我认为错误发生在INFERENCE_PROB_NAME ,我看到了该文档 However, I still do not know what the word is to replace INFERENCE_PROB_NAME . 但是,我仍然不知道替换INFERENCE_PROB_NAME这个词是什么。

I have tried get_metric('accuracy') to replace INFERENCE_PROB_NAME , it return the error: KeyError: <function _accuracy at 0x11a06eaa0> . 我尝试了get_metric('accuracy')来替换INFERENCE_PROB_NAME ,它返回错误: KeyError: <function _accuracy at 0x11a06eaa0>

I also tried get_prediction_key('accuracy') to replace INFERENCE_PROB_NAME , it return the error: KeyError: 'classes' . 我还尝试了get_prediction_key('accuracy')来替换INFERENCE_PROB_NAME ,它返回错误: KeyError: 'classes'

If you know the possible answer, please tell me. 如果您知道可能的答案,请告诉我。 Thank you in advance. 先感谢您。

I think you are unintentionally doing a classification problem by giving a wrong num_classes=2 and not changing the default value of regression=False . 我认为你无意中通过给出错误的num_classes=2并且不改变regression=False的默认值来做分类问题。 See the Parameters section here . 请参阅此处的“ 参数”部分 Just as a quick test, set num_classes=0 and regression=True , and re-run your code. 就像快速测试一样,设置num_classes=0regression=True ,然后重新运行代码。

num_classes=0 is wrong in tensorflow 1.3.0. 在tensorflow 1.3.0中, num_classes=0是错误的。

From the link of Mehdi Rezaie, num_classes is the number of dimensions in the output of a regression problem. 在Mehdi Rezaie的链接中, num_classes是回归问题输出中的维数。

You have to use num_classes=1 or bigger value for num_classes. 您必须为num_classes=1使用num_classes=1或更大的值。 Or You'll get the error like ValueError: Invalid logits_dimension 0. 或者你会得到像ValueError: Invalid logits_dimension 0.这样的错误ValueError: Invalid logits_dimension 0.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM