25

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

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.

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.

How do I fix this?

double-beep
  • 5,031
  • 17
  • 33
  • 41
user3764124
  • 381
  • 1
  • 3
  • 8

12 Answers12

20

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

When reading the csv:

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

I would recommend you to check:

df_test.dtypes

You should get something like

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

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

Fgblanch
  • 5,195
  • 8
  • 37
  • 51
  • 1
    Yes, it is definitely a data type issue, but it is NOT because you included the row of column names. Those are most often required. This error was most likely due to interpreting a categorical column as a numerical one. E.g. some features are objects. – Chrimle Jan 18 '22 at 22:45
6

The problem is that you probably had indicated the feature like a real type but in your data frame is still string or when set in tf.constant you didn't cast to correct type.

Confirm the types of your columns. You can check just type (df is your dataframe):

df.info()

And you can see all columns and types, some like that:

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

You can use this bellow function in order to convert your data in correct type in tensorflow. (this code is from a repo google/training-data-analyst: link here)

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
Andre Araujo
  • 2,348
  • 2
  • 27
  • 41
  • Please leave a comment before a down vote. The line t = tf.constant(pdcol.astype('float32').values) can solve the main problem of the question. – Andre Araujo Apr 30 '18 at 19:56
2

You can't literally cast a string to a number, particularly "y","n" to 1.0/0.0.

If you have numeric strings (e.g. "0") you could try tf.string_to_number(..)

Mark McDonald
  • 7,571
  • 6
  • 46
  • 53
2

I faced the same issue when I tried replicating the steps on a different data-set for practice.

SIMPLE TO FIX, just use the following code to change the data type of your TARGET COLUMN to int,

df["target_column_name"] = df["target_column_name"].astype(str).astype(int)

Also, you need to do it at the starting, when the target column is inside your data-file initially.

1

I am using W10, Python3 and Tensorflow 1.9

The source of the error in my code was in the feature definition. I had a boolean feature with a default_value of -1 like this:

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)

The issue did not arise when the default_value was changed to 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 is the integer ID value to return for out-of-vocabulary feature values. For example, in a list/file of value like ['True', 'False'] to make default_value == True, it would be default_value=0; the list index.

Tony Shouse
  • 106
  • 5
0

Your classes are probably in string forms and they need to be numeric (1 and 0 only for this specific tutorial)

MahdeTo
  • 11,034
  • 2
  • 27
  • 28
  • Can you be a bit more specific? You mean the clases in the input or the classes in the model? – Fgblanch Dec 05 '16 at 19:33
  • 1
    By classes here I mean the labels, this example only works for a binary classification problem, the labels has to be specifically numeric and 0 or 1 only – MahdeTo Jan 19 '17 at 07:01
0

Normally this error is because m.evaluate is somehow empty.

Since you load your data from csv file, it is very likely that your data was stored as string instead of float or int inside the array. I suggest you check it manually to make sure.

Qin Heyang
  • 1,456
  • 1
  • 16
  • 18
0

Somewhere in your code you're using tf.cast() to convert a string to number, but you cannot do this. Replace it with tf.strings.to_number():

tf.strings.to_number(x, out_type=tf.float32)
tsveti_iko
  • 6,834
  • 3
  • 47
  • 39
0

The problem is that there's a header on the file you've imported to work on. The header is of the type string and the rest of the rows are int64 or float64. While the types are variant in a column they are typed as object . You can check and make sure the this is the problem with this code:

df_test.dtypes

To solve this you can simply delete the header row from your CSV before importing it to pyhton. Just remember that if you drop that row after importing it will not work and the data types won't change!

0

The other way is using

df = df.astype({'COL1': 'float64', 'COL2': 'float64'})

for a dataframe

Ayenew Yihune
  • 1,059
  • 13
  • 19
0

Some times the reason is that your feeding data is not in the string format and that is example of WRONGE data

,0
' or x = 1 , 1, 0,1
SELECT * FROM USERS WHERE(1 = 1, 1, 0 = 0,0),1

Try to clean your dataset first and honestly this worked with me in my graduation project dataset

  • You should read the question carefully. The error says `Cast string to float is not supported` means the data is actually a `string` but it don't know how to cast that string data into a `float`. Please refer [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). – nobleknight Jun 10 '21 at 15:01
  • @nobleknight I know bro, I give him solution for the same error happened to me with same situation and what I did to solve this error only, the data may be string but the problem with extracting this data. – Abdelrahman Saleh Jun 12 '21 at 12:24
0

The immediate answer is, that you need to change your df_test's format to the same format as your df_train at the very beginning.

For example,

df_train.replace({'yes':1,'no':0}, inplace = True)
    
df_test.replace({'yes':1,'no':0}, inplace = True)  
coatless
  • 20,011
  • 13
  • 69
  • 84