简体   繁体   English

在DNNClassifier TensorFlow中创建适当的input_fn

[英]Creating appropriate input_fn in DNNClassifier TensorFlow

I'm building a neural network with DNNClassifier and I've read the examples on the site, and done by others, about this estimator, but I'm still confused on the construction of the input_fn. 我正在使用DNNClassifier构建一个神经网络,并且已经在站点上阅读了该估计器的示例,并由其他人完成了该估计器,但是我对input_fn的构造仍然感到困惑。 I post my code below 我在下面发布我的代码

import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split

df = pd.read_csv('chunk.csv')
y = df.MoreClass
x_train, x_test, y_train, y_test = train_test_split(df, y, test_size=0.2)

CATEGORICAL_COLUMNS = [#list of categorical columns]
CONTINUOUS_COLUMNS = [#list of continuous columns]
COLUMNS = [#all the columns]
FEATURES = [#columns containing features]
LABEL = "label" #categorical column with 10 classes (D, BBB, BB and so on)

#embedding of categorical columns
col1 = tf.feature_column.categorical_column_with_hash_bucket(
  "df.col1", hash_bucket_size=1000)
col1_emb = tf.feature_column.embedding_column(col1, 30)
#list of others embedded columns

#transformation of numerical columns in indicator ones
col2 = tf.feature_column.numeric_column("df.col2")
col2_ind = tf.feature_column.indicator_column(col2)

#all the transformed columns
dense_cols = [col1, col2 #etcetc]

#DNNClassifier
classifier = tf.estimator.DNNClassifier(
 feature_columns=dense_cols,
 hidden_units=[10, 10],
 n_classes=10,
 dropout=0.1
)

def create_train_input_fn(): 
    return tf.estimator.inputs.pandas_input_fn(
        x=x_train,
        y=y_train, 
        batch_size=32,
        num_epochs=None, 
        shuffle=True)

def create_test_input_fn():
    return tf.estimator.inputs.pandas_input_fn(
        x=x_test,
        y=y_test, 
        num_epochs=1, 
        shuffle=False) 


train_input_fn = create_train_input_fn()
classifier.train(train_input_fn, steps=1000)

I've omitted part of the code because is mostly the same when I define the embedded and indicator columns, and also when I define COLUMNS, FEATURES and LABELS. 我省略了部分代码,因为在定义内嵌和指示符列以及定义列,特征和标签时,它们几乎是相同的。

After running the script I bump in the error: '_NumericColumn' object has no attribute '_get_sparse_tensors', and I don't know how to overcome it or where I did wrong. 运行脚本后,我遇到了错误:'_NumericColumn'对象没有属性'_get_sparse_tensors',并且我不知道如何克服它或在哪里做错了。

Is the problem in the creation of the input_fn? 创建input_fn是否有问题? Or is it before that? 还是在那之前? And if it before the input_fn, how do I write a proper input_fn? 如果它在input_fn之前,我该如何编写适当的input_fn?

Any help would be greatly appreciated, thanks in advance. 任何帮助将不胜感激,在此先感谢。

The indicator_column function requires a _CategoricalColumn , but you're calling it with the _NumericColumn returned by numeric_column . indicator_column函数需要一个_CategoricalColumn ,但是您要使用numeric_column返回的_NumericColumn来调用它。 I think you can obtain a _CategoricalColumn by calling bucketized_column . 我认为你可以得到一个_CategoricalColumn通过调用bucketized_column

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM