【问题标题】:UnimplementedError: Cast string to float is not supported while using TensorflowUnimplementedError:使用 Tensorflow 时不支持将字符串转换为浮点数
【发布时间】:2021-01-02 21:30:20
【问题描述】:
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
df = pd.read_csv('pokemon_data.csv')
df['Total'] = df['HP'] + df['Attack'] + df['Defense'] + df['Sp. Atk'] + df['Sp. Def'] + df['Speed']
df = df.loc[df['Total'] > 450]
df = df.loc[~df['Name'].str.contains('Mega')]
df = df.loc[~df['Name'].str.contains('Primal')]
df = df.drop(columns = ['Name'])
df = df.drop(columns = ['Generation'])
df = df.drop(columns = ['Legendary'])
df = df.drop(columns = ['Type 2'])
df = df.drop(columns = ['#'])
df_eval_sub = df.loc[df['Total'] < 500]
df_eval_over = df.loc[df['Total'] > 500]                      
y_train = df.pop('Type 1')
y_eval_sub = df_eval_sub.pop('Type 1')
y_eval_over = df_eval_over.pop('Type 1')                     
                      
feature_columns = []

NUMERIC_COLUMNS = ['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed', 'Total']
for feature_name in NUMERIC_COLUMNS:
    feature_columns.append(tf.feature_column.numeric_column(feature_name, dtype=tf.float32))
    
def make_input_fn(data_df, label_df, num_epochs = 10, shuffle = True, batch_size=32):
    def input_function():
        ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))
        if shuffle:
            ds = ds.shuffle(1000)
        ds = ds.batch(batch_size).repeat(num_epochs)
        return ds
    return input_function

train_input_fn = make_input_fn(df, y_train)
eval_input_fn = make_input_fn(df_eval_sub, y_eval_sub, num_epochs = 1, shuffle = False)

linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
linear_est.train(train_input_fn)
result = linear_est.evaluate(eval_input_fn)

clear_output()
print(result['accuracy'])

在原始文件中,除了“类型 1”列之外,所有列中都有数字。每当我尝试将 Type 1 更改为数字时,都会出现更多错误。每当调用 train_input_fn 时都会触发该错误。 错误:

UnimplementedError: Cast string to float is not supported
     [[{{node head/losses/Cast}}]]

During handling of the above exception, another exception occurred:

UnimplementedError                        Traceback (most recent call last)
<ipython-input-166-e9dbb248f085> in <module>
     12 
     13 linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
---> 14 linear_est.train(train_input_fn)
     15 result = linear_est.evaluate(eval_input_fn)
     16 

【问题讨论】:

  • 您的数据框的一个列是否可能是字符串而不是浮点数?
  • 剩下的所有列都是 int64,除了 Type 1 列是一个对象。

标签: python pandas tensorflow


【解决方案1】:

我可以通过在具有字符串值的数据框列上使用 tf.feature_column.numeric_column 来重现您的错误。

import tensorflow as tf
import pandas as pd
import numpy as np

df = pd.DataFrame({
    'float_values': np.random.rand(15),
    'string_values': np.random.randint(0, 10, (15,))
})

df['string_values'] = df['string_values'].astype(str)
float_values     float64
string_values     object
ds = tf.data.Dataset.from_tensors(dict(df))

float_column = tf.feature_column.numeric_column('float_values')
string_column = tf.feature_column.numeric_column('string_values')

# This works, the 'float_values' column is numeric
float_layer = tf.keras.layers.DenseFeatures(float_column)
float_layer(next(iter(ds)))

# This doesn't work, the 'string_values' column is string
string_layer = tf.keras.layers.DenseFeatures(string_column)
string_layer(next(iter(ds)))

tensorflow.python.framework.errors_impl.UnimplementedError:不支持将字符串转换为浮点数 [Op:Cast]

确保您的所有数据框都是dtype float/int。

for col in NUMERIC_COLUMNS:
    df[col] = pd.to_numeric(df[col])

请注意,转换为数字可能更好,我承认我不是 Pandas 专家。

【讨论】:

    猜你喜欢
    • 2020-08-11
    • 2021-08-27
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2011-11-25
    • 2017-11-05
    • 2022-06-28
    相关资源
    最近更新 更多