我收到此错误:



我正在使用tensorFlow lib。我想获得预测结果,但无法运行m.train(input_fn=get_input_fn ,steps=5000)代码。无论执行什么操作,我总是会遇到相同的错误。我在下面使用了这些输入功能,但没有任何变化。

def input_fn_train():
     x=tf.constant(df_train.astype(np.float64)),
     y=tf.constant(df_train[LABEL].astype(np.float64))
     return x, y


def get_input_fn(data_set, num_epochs=None, shuffle=False):
     return tf.estimator.inputs.pandas_input_fn(
      x=pd.DataFrame({k: data_set[k].values for k in data_set.columns}),
      y=pd.Series(data_set[LABEL].values), num_epochs=num_epochs,
                  shuffle=shuffle)

我不明白该怎么办。错误是关于什么的?我一直在谷歌搜索,但从未发现有用的东西。我该如何处理此错误。代码如下。谢谢!
import pandas as pd
import tensorflow as tf
import numpy as np
import tempfile

COLS= ["RowNumber","CustomerId","Surname","CreditScore","Geography",
"Gender","Age","Tenure","Balance","NumOfProducts","HasCrCard",
"IsActiveMember","EstimatedSalary","Exited"]


FEATURES = ["CreditScore","Age","Tenure","Balance","NumOfProducts",
       "HasCrCard","IsActiveMember", "EstimatedSalary"]

LABEL="Exited"

df_train = pd.read_csv("Churn_Modelling.csv", skipinitialspace=True,
header=0)
df_test = pd.read_csv("Churn_Modelling.csv", skipinitialspace=True,
header=0)
test_label = df_test[LABEL].astype(float)
df_test.drop("Surname", axis = 1, inplace=True)
df_test.drop("RowNumber", axis = 1, inplace=True)
df_test.drop("CustomerId", axis = 1, inplace=True)
df_train.drop("CustomerId", axis = 1, inplace=True)
df_train.drop("Surname", axis = 1, inplace=True)
df_train.drop("RowNumber", axis = 1, inplace=True)
df_train.drop("Geography", axis = 1, inplace=True)
df_train.drop("Gender", axis = 1, inplace=True)

def get_input_fn():
    return {'x': tf.constant(df_train[FEATURES].as_matrix(), tf.float32,
           df_train.shape),
           'y': tf.constant(df_train[LABEL].as_matrix(), tf.float32,
            df_train.shape)
           }

 df=df_train.select_dtypes(exclude=['object'])
 numeric_cols=df.columns

 m = tf.estimator.LinearClassifier(model_dir=model_dir, feature_columns=
[numeric_cols])

 m.train(input_fn=get_input_fn ,steps=5000)
 results = m.evaluate(input_fn= get_input_fn(df_test, num_epochs=1,
 shuffle=False),steps=None)

 y = m.predict(input_fn=get_input_fn(df_test, num_epochs=1, shuffle=False))
 pred = list(y)

 rowNumber=0
 for i in pred:
     print(str(rowNumber)+': '+str(pred[i]))
     rowNumber=rowNumber+1

最佳答案

您的第一个错误是如何创建tf.estimator.LinearClassifier。您要将数据帧索引df.columns传递给feature_columns,但应传递tensorflow feature columns的列表。列应定义它是数字类型还是分类类型,在后一种情况下应定义编码类型。

其次,输入函数可以大大简化,因为您正在读取pandas数据帧。只需使用tf.estimator.inputs.pandas_input_fn即可。

您的.csv很可能与众不同,我用一些值做了一个虚拟的。因此,这是一种读取输入并正确拟合模型的方法:

import pandas as pd
import tensorflow as tf

FEATURES = ["CreditScore", "Age", "Tenure", "Balance", "NumOfProducts",
            "HasCrCard", "IsActiveMember", "EstimatedSalary", "Exited"]

credit_score = tf.feature_column.numeric_column("CreditScore")
age = tf.feature_column.numeric_column("Age")
tenure = tf.feature_column.numeric_column("Tenure")
balance = tf.feature_column.numeric_column("Balance")
num_of_products = tf.feature_column.numeric_column("NumOfProducts")
has_card = tf.feature_column.categorical_column_with_vocabulary_list("HasCrCard", ["True", "False"])
is_active_member = tf.feature_column.categorical_column_with_vocabulary_list("IsActiveMember", ["True", "False"])
estimated_salary = tf.feature_column.numeric_column("EstimatedSalary")
feature_columns = [credit_score, age, tenure, balance, num_of_products, has_card, is_active_member, estimated_salary]

def input_fn(num_epochs=None, shuffle=True, batch_size=100):
  df = pd.read_csv('Churn_Modelling.csv',
                   names=FEATURES,
                   dtype={'HasCrCard': str, 'IsActiveMember': str},
                   skipinitialspace=True,
                   header=0)
  df = df.dropna(how='any', axis=0)   # remove NaN elements
  labels = df["Exited"]
  return tf.estimator.inputs.pandas_input_fn(x=df,
                                             y=labels,
                                             batch_size=batch_size,
                                             num_epochs=num_epochs,
                                             shuffle=shuffle,
                                             num_threads=5)

model = tf.estimator.LinearClassifier(model_dir=None,
                                      feature_columns=feature_columns)
model.train(input_fn=input_fn(), steps=100)

关于python - feature_columns的项目必须为_FeatureColumn,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47197989/

10-12 17:56