UnimplementedError:使用Tensorflow时不支持将字符串强制转换为浮点


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

从原始文件中,除了"Type 1"列之外,所有列中都有数字。每当我试图将类型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 

我可以通过在具有字符串值的数据帧列上使用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]

确保所有数据帧都是dtypefloat/int。

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

请注意,可能有一个更好的方法来转换为数字,我无可否认不是熊猫专家。

相关内容

  • 没有找到相关文章

最新更新