Casting a string to a floating point is not supported in a linear model

I keep getting this error in my linear model:

Casting a string to a floating point is not supported

In particular, the error is on this line:

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

If this helps, here is 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)]]

The model is an adaptation of the textbook here and here . The tutorial code works, so this is not a problem with my TensorFlow installation.

An input CSV is data in the form of a set of binary categorical columns ( yes/ no). Initially, I represented the data in each column as 0 and 1, but I get the same error when I change them to yand ns.

How to fix it?

+10
6

, , , , . ( , )

, , float. - ,

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

+5

, "y", "n" 1.0/0.0.

(, "0" ), tf.string_to_number(..)

+2

, , , , ​​ tf.constant, .

. (df - dataframe):

df.info()

, :

<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
...

, tensorflow. ( google/training-data-analyzer: )

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
+2

W10, Python3 Tensorflow 1.9

. default_value -1, :

tf.feature_column.categorical_column_with_vocabulary_list( 
    key='partial_funding_indicator', vocabulary_list=['True', 'False'],
    dtype=tf.string, **default_value=-1**, num_oov_buckets=None)

, default_value 0:

tf.feature_column.categorical_column_with_vocabulary_list(
    key='partial_funding_indicator', vocabulary_list=['True', 'False'],
    dtype=tf.string, **default_value=0**, num_oov_buckets=None)

default_value - , . , / , ['True', 'False'] default_value == True, default_value=0; .

+1

Your classes are probably in string forms, and they should be numeric (1 and 0 only for this particular tutorial)

0
source

Usually this error occurs because it m.evaluateis somehow empty.

Since you are loading data from a csv file, it is very likely that your data was saved as a string instead of a float or int inside an array. I suggest you check it manually to make sure.

0
source

Source: https://habr.com/ru/post/1658509/


All Articles