【问题标题】:What is causing this strange TypeError in my random forest code?是什么导致我的随机森林代码中出现这种奇怪的 TypeError?
【发布时间】:2017-12-13 02:58:25
【问题描述】:

我有一些非常简单的代码,可以根据来自 csv 的数据训练随机森林。代码,减去导入和常量可以在下面找到:

def build_estimator(model_dir):
  """Build an estimator."""
  params = tensor_forest.ForestHParams(
      num_classes=2, num_features=5,
      num_trees=FLAGS.num_trees, max_nodes=FLAGS.max_nodes)
  graph_builder_class = tensor_forest.RandomForestGraphs
  if FLAGS.use_training_loss:
    graph_builder_class = tensor_forest.TrainingLossForest
  # Use the SKCompat wrapper, which gives us a convenient way to split
  # in-memory data like MNIST into batches.
  return estimator.SKCompat(random_forest.TensorForestEstimator(
      params, graph_builder_class=graph_builder_class,
      model_dir=model_dir))


model_dir = tempfile.mkdtemp() if not FLAGS.model_dir else FLAGS.model_dir
est = build_estimator(model_dir)

COLUMNS = [ "a", "b", "c",
           "d", "e", "f"]
postData = pd.read_csv("PostData2Cut.csv", names=COLUMNS, skipinitialspace=True, dtype=np.float32)

est.fit(x=postData[["a", "b", "c",
           "d", "e"]], y=postData[["f"]],
          batch_size=FLAGS.batch_size)

当我到达 est.fit 行时,虽然它会崩溃,但会说以下内容:

TypeError: Input 'input_data' of 'CountExtremelyRandomStats' Op has type float64 that does not match expected type of float32.

显然,这发生在以下代码行中名为 op_def_library.py 的一些 tensorflow 文件中:

apply_op
    (prefix, dtypes.as_dtype(input_arg.type).name))

不确定是什么原因造成的。我似乎是说从 csv 读取的值应该是 float32 类型。对此感到非常沮丧。关于如何解决它的任何想法?

【问题讨论】:

  • 如果您在 pd.read_csv 调用中说 dtype=np.float64 会发生什么?
  • 它崩溃了,告诉我它无法将分类字符串值转换为浮点数:/
  • 你能用 tf.cast() 把所有东西都放在 float32 中吗?

标签: python csv machine-learning tensorflow random-forest


【解决方案1】:

我遇到了同样的错误,tf.cast() 解决了这个问题。

training_set = tf.cast(training_set, tf.float32)

看到这个答案: TypeError when training Tensorflow Random Forest using TensorForestEstimator

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 2011-09-03
    • 2011-09-30
    • 2020-01-18
    相关资源
    最近更新 更多