线性模型不支持将字符串强制转换为浮点型 [英] Cast string to float is not supported in Linear Model

查看:845
本文介绍了线性模型不支持将字符串强制转换为浮点型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线性模型中不断收到此错误:

I keep getting this error in my linear model:

不支持要浮空的转换字符串

Cast string to float is not supported

具体来说,该错误在此行上:

Specifically, the error is on this line:

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

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

If it helps, here's the stack trace:

 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)]]

该模型是此处此处.教程代码可以运行,因此我的TensorFlow安装不是问题.

The model is an adaptation of the tutorial from here and here. The tutorial code does run, so it's not a problem with my TensorFlow installation.

输入CSV是许多二进制类别列(yes/no)形式的数据.最初,我将每一列中的数据表示为0和1,但是当我将其更改为y s和n s时会遇到相同的错误.

The input CSV is data in the form of many binary categorical columns (yes/no). Initially, I represented the data in each column as 0's and 1's, but I get the same error when I change it to ys and ns.

我该如何解决?

推荐答案

我遇到了完全相同的问题,您需要确保输入模型的输入数据格式正确. (不仅是功能,还包括标签列)

I had the exact same problem, you need to make sure that the input data you are feeding the model is in the right format. ( not just the features but also the label column)

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

My problem was that i was not skipping the first row in the data file, so i was trying to convert the titles to float format.Something as simple as adding

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

如果您没有获得正确的dtype,则model.fit将会失败

If you are not getting the correct dtype then the model.fit is going to fail

这篇关于线性模型不支持将字符串强制转换为浮点型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆