Python Tensorflow线性模型不支持将字符串转换为float

use*_*124 11 python model tensorflow linearmodels

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

不支持将字符串转换为float

具体来说,错误在这一行:

results = m.evaluate(input_fn=lambda: input_fn(df_test), steps=1)
Run Code Online (Sandbox Code Playgroud)

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

 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)]]
Run Code Online (Sandbox Code Playgroud)


该模型是对本教程的改编:
https://www.tensorflow.org/versions/r0.11/tutorials/wide/index.html
https://github.com/tensorflow/tensorflow/blob/master/ tensorflow/examples/learn/wide_n_deep_tutorial.py

教程代码确实运行,所以我的tensorflow安装不是问题.

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

我该如何解决?
我可以提供有关我的代码的更多信息,如果它有助于诊断和解决问题.

And*_*ujo 6

问题是您可能已经指示了像真实 类型一样的功能,但在您的数据框中仍然是字符串,或者在 tf.constant 中设置时您没有转换为正确的类型。

确认您的列的类型。您可以仅检查类型(df 是您的数据框):

df.info()
Run Code Online (Sandbox Code Playgroud)

您可以看到所有列和类型,有些像这样:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 178932 entries, 0 to 178931
Data columns (total 64 columns):
d_prcp                      178932 non-null float64
d_stn                       178932 non-null int64
ws_lat                      178932 non-null float64
ws_lon                      178932 non-null float64
d_year                      178932 non-null int64
d_temp                      178932 non-null float64
...
Run Code Online (Sandbox Code Playgroud)

您可以使用以下函数将数据转换为tensorflow中的正确类型。(此代码来自google/training-data-analyst仓库:链接此处

def make_input_fn(df):
  def pandas_to_tf(pdcol):
    # convert the pandas column values to float
    t = tf.constant(pdcol.astype('float32').values)
    # take the column which is of shape (N) and make it (N, 1)
    return tf.expand_dims(t, -1)

  def input_fn():
    # create features, columns
    features = {k: pandas_to_tf(df[k]) for k in FEATURES}
    labels = tf.constant(df[TARGET].values)
    return features, labels
  return input_fn

def make_feature_cols():
  input_columns = [tf.contrib.layers.real_valued_column(k) for k in FEATURES]
  return input_columns
Run Code Online (Sandbox Code Playgroud)


Fgb*_*nch 5

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

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

skiprows=1
Run Code Online (Sandbox Code Playgroud)

在阅读csv时:

df_test = pd.read_csv(test_file, names=COLUMNS_TEST, skipinitialspace=True, skiprows=1, engine="python")
Run Code Online (Sandbox Code Playgroud)

我建议你检查一下:

df_test.dtypes
Run Code Online (Sandbox Code Playgroud)

你应该得到类似的东西

Feature1      int64
Feature2      int64
Feature3      int64
Feature4      object
Feature5      object
Feature6      float64
dtype: object
Run Code Online (Sandbox Code Playgroud)

如果你没有得到正确的dtype,那么model.fit将会失败


Mar*_*ald 2

您不能从字面上将字符串转换为数字,特别是“y”、“n”到 1.0/0.0。

如果您有数字字符串(例如“0”),您可以尝试tf.string_to_number(..)