不支持将字符串强制转换为float

时间:2019-01-18 22:44:58

标签: python-3.x tensorflow

我正在研究tensorflow课程,并试图将我所学的知识应用于自己的数据。我收到“不支持浮空的字符串”。使用pandas数据框INFO(),我确认使用categorical_column_with_hash_bucket转换为功能列的所有对象,所有INT64或FLOAT64我都使用了numeric_column。为什么会弹出此错误?

这是我的代码和错误:

import tensorflow as tf
import pandas as pd
from sklearn.model_selection import train_test_split
tree_data_file = r'\\David\f\first_test_feature_cols_v2.csv'
tree_data = pd.read_csv(tree_data_file)

### Create feature columns for continuous data
b3_sum = tf.feature_column.numeric_column('b3_sum')
im3b3_s = tf.feature_column.numeric_column('im3b3_s')
imred = tf.feature_column.numeric_column('imred')

# Create feature columns for categorical data
sp1 = tf.feature_column.categorical_column_with_hash_bucket('sp1',hash_bucket_size=10)
sp2 = tf.feature_column.categorical_column_with_hash_bucket('sp2',hash_bucket_size=10)

feat_cols = [b3_sum,im3b3_s,im_red,sp1,sp2]

#TRAIN TEST SPLIT
x_data = tree_data.drop('sp_call', axis=1)
labels = tree_data['sp_call']
X_train, X_test, Y_train, Y_test = train_test_split(x_data, labels, test_size = 0.3)

input_func = tf.estimator.inputs.pandas_input_fn(x = X_train, y=Y_train, batch_size=10, num_epochs=1000, shuffle=True)
model = tf.estimator.LinearClassifier(feature_columns=feat_cols, n_classes=2)
model.train(input_fn=input_func, steps=1000)


Caused by op 'linear/head/ToFloat', defined at:
  File "<string>", line 1, in <module>
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\idlelib\run.py", line 144, in main
    ret = method(*args, **kwargs)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\idlelib\run.py", line 474, in runcode
    exec(code, self.locals)
  File "C:\Users\david\Documents\Neural_Network\test_neural_network_v1.py", line 59, in <module>
    model.train(input_fn=input_func, steps=1000)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 354, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1207, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1237, in _train_model_default
    features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1195, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\canned\linear.py", line 384, in _model_fn
    sparse_combiner=sparse_combiner)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\canned\linear.py", line 215, in _linear_model_fn
    logits=logits)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\canned\head.py", line 239, in create_estimator_spec
    regularization_losses))
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\canned\head.py", line 1209, in _create_tpu_estimator_spec
    features=features, mode=mode, logits=logits, labels=labels))
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\canned\head.py", line 1115, in create_loss
    labels = math_ops.to_float(labels)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\math_ops.py", line 732, in to_float
    return cast(x, dtypes.float32, name=name)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\math_ops.py", line 677, in cast
    x = gen_math_ops.cast(x, base_type, name=name)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 1754, in cast
    "Cast", x=x, DstT=DstT, Truncate=Truncate, name=name)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3274, in create_op
    op_def=op_def)
  File "C:\Users\david\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1770, in __init__
    self._traceback = tf_stack.extract_stack()

UnimplementedError (see above for traceback): Cast string to float is not supported
     [[node linear/head/ToFloat (defined at C:\Users\david\Documents\Neural_Network\test_neural_network_v1.py:59)  = Cast[DstT=DT_FLOAT, SrcT=DT_STRING, Truncate=false, _class=["loc:@linea...t/Switch_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](linear/head/labels/_781)]]

0 个答案:

没有答案