ator训练Tensorflow随机森林时发生TypeError

ator训练Tensorflow随机森林时发生TypeError

本文介绍了使用TensorForestEstimator训练Tensorflow随机森林时发生TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用TensorForestEstimator训练Tensorflow随机森林时出现TypeError.

I get a TypeError when attempting to train an Tensorflow Random Forest using TensorForestEstimator.

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

我尝试使用Python 2.7和Python 3,并且尝试使用tf.cast()将所有内容放入float32,但这无济于事.我已经在执行时检查了数据类型,它是float32.问题似乎不在于我提供的数据(所有浮点数的csv),所以我不确定从这里去哪里.

I've tried using Python 2.7 and Python 3, and I've tried using tf.cast() to put everything in float32 but it doesn't help. I have checked the data type on execution and it's float32. The problem doesn't seem to be the data I provide (csv of all floats), so I'm not sure where to go from here.

对于我可以尝试的任何建议,将不胜感激.

Any suggestions of things I can try would be much appreciated.

代码:

# Build an estimator.
def build_estimator(model_dir):
  params = tensor_forest.ForestHParams(
      num_classes=2, num_features=760,
      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 into batches.
  return estimator.SKCompat(random_forest.TensorForestEstimator(params, graph_builder_class=graph_builder_class, model_dir=model_dir))


# Train and evaluate the model.
def train_and_eval():

  # load datasets
  training_set = pd.read_csv('/Users/carl/Dropbox/Docs/Python/randomforest_balanced_train.csv', dtype=np.float32, header=None)
  test_set = pd.read_csv('/Users/carl/Dropbox/Docs/Python/randomforest_balanced_test.csv', dtype=np.float32, header=None)

  print('###########')
  print(training_set.loc[:,1].dtype)  # this prints float32

  # load labels
  training_labels = pd.read_csv('/Users/carl/Dropbox/Docs/Python/randomforest_balanced_train_class.csv', dtype=np.int32, names=LABEL, header=None)
  test_labels = pd.read_csv('/Users/carl/Dropbox/Docs/Python/randomforest_balanced_test_class.csv', dtype=np.int32, names=LABEL, header=None)

  # define the path where the model will be stored - default is current directory
  model_dir = tempfile.mkdtemp() if not FLAGS.model_dir else FLAGS.model_dir
  print('model directory = %s' % model_dir)

  # build the random forest estimator
  est = build_estimator(model_dir)

  tf.cast(training_set, tf.float32) #error occurs with/without casts
  tf.cast(test_set, tf.float32)
  # train the forest to fit the training data
  est.fit(x=training_set, y=training_labels)  #this line throws the error

推荐答案

您使用tf.cast的方式不正确

You are using tf.cast in incorrect manner

tf.cast(training_set, tf.float32) #error occurs with/without casts

应该是

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

tf.cast是不是就地方法,与其他任何操作一样,它是张量流运算,需要分配和运行.

tf.cast is not in-place method, it is a tensor flow op, as any other operation, and needs to be assigned and run.

这篇关于使用TensorForestEstimator训练Tensorflow随机森林时发生TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:52