【发布时间】: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