gpt4 book ai didi

python - 线性模型不支持将字符串转换为 float

转载 作者:太空狗 更新时间:2023-10-29 18:29:27 25 4
gpt4 key购买 nike

我的线性模型中不断出现此错误:

Cast string to float is not supported

具体错误在这一行:

results = m.evaluate(input_fn=lambda: input_fn(df_test), steps=1)

如果有帮助,这里是堆栈跟踪:

 File "tensorflowtest.py", line 164, in <module>
m.fit(input_fn=lambda: input_fn(df_train), steps=int(100))
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/linear.py", line 475, in fit
max_steps=max_steps)
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 333, in fit
max_steps=max_steps)
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 662, in _train_model
train_op, loss_op = self._get_train_ops(features, targets)
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 963, in _get_train_ops
_, loss, train_op = self._call_model_fn(features, targets, ModeKeys.TRAIN)
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 944, in _call_model_fn
return self._model_fn(features, targets, mode=mode, params=self.params)
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/linear.py", line 220, in _linear_classifier_model_fn
loss = loss_fn(logits, targets)
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/linear.py", line 141, in _log_loss_with_two_classes
logits, math_ops.to_float(target))
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 661, in to_float
return cast(x, dtypes.float32, name=name)
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 616, in cast
return gen_math_ops.cast(x, base_type, name=name)
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 419, in cast
result = _op_def_lib.apply_op("Cast", x=x, DstT=DstT, name=name)
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
op_def=op_def)
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/home/computer/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
self._traceback = _extract_stack()

UnimplementedError (see above for traceback): Cast string to float is not supported
[[Node: ToFloat = Cast[DstT=DT_FLOAT, SrcT=DT_STRING, _device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_1)]]

该模型改编自 here 中的教程和 here .教程代码确实可以运行,所以我的 TensorFlow 安装没有问题。

输入 CSV 是许多二进制分类列形式的数据(yes/no)。最初,我将每列中的数据表示为 0 和 1,但是当我将其更改为 yn 时,我得到了同样的错误。

我该如何解决这个问题?

最佳答案

我遇到了完全相同的问题,您需要确保为模型提供的输入数据格式正确。 (不仅是特征,还有标签列)

我的问题是我没有跳过数据文件中的第一行,所以我试图将标题转换为浮点格式。就像添加一样简单

skiprows=1

读取 csv 时:

df_test = pd.read_csv(test_file, names=COLUMNS_TEST, skipinitialspace=True, skiprows=1, engine="python")

我建议您检查一下:

df_test.dtypes

你应该得到类似的东西

Feature1      int64
Feature2 int64
Feature3 int64
Feature4 object
Feature5 object
Feature6 float64
dtype: object

如果您没有获得正确的数据类型,那么 model.fit 将会失败

关于python - 线性模型不支持将字符串转换为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40186722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com