Tensorflow DNNClassifier.fit错误:TypeError:' tuple'对象不可调用

时间:2017-06-15 06:14:08

标签: tensorflow classification

我正在尝试分析一个包含60列的数据集(FEATURES = 59列(整数和浮点数的混合),LABEL =名为TARGET且值为0/1的列)

我在尝试适合模型时遇到以下错误:

TypeError: 'tuple' object is not callable

以下是使用的代码:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import urllib

import itertools

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

tf.logging.set_verbosity(tf.logging.INFO)

COLUMNS = ["DEMAdmNo","target","PEV_30","PEV_365","lhlos","LTSD","LNOSD","comorbidity_index","AdmD_3","AdmD_7","AdmD_8","AdmD_9","AdmD_11","AdmD_13","AdmD_14","AdmD_15","AdmD_16","AdmD_18","AdmD_20","AdmD_21","AdmD_22","AdmD_23","AdmD_26","AdmD_27","AdmD_28","AdmD_30","AdmD_31","AdmD_32","AdmD_33","AdmD_36","DisP_1","DisP_2","DisP_4","DisP_5","DisP_6","DisP_7","DisP_11","DisP_12","DisP_13","DisP_14","DisP_16","Disc_8","Disc_10","Disc_11","Disc_12","Disc_14","Disc_15","Disc_17","Disc_21","Disc_22","Disc_23","Disc_24","Disc_25","Disc_26","Disc_27","Disc_28","Disc_29"]

FEATURES = ["DEMAdmNo","PEV_30","PEV_365","lhlos","LTSD","LNOSD","comorbidity_index","AdmD_3","AdmD_7","AdmD_8","AdmD_9","AdmD_11","AdmD_13","AdmD_14","AdmD_15","AdmD_16","AdmD_18","AdmD_20","AdmD_21","AdmD_22","AdmD_23","AdmD_26","AdmD_27","AdmD_28","AdmD_30","AdmD_31","AdmD_32","AdmD_33","AdmD_36","DisP_1","DisP_2","DisP_4","DisP_5","DisP_6","DisP_7","DisP_11","DisP_12","DisP_13","DisP_14","DisP_16","Disc_8","Disc_10","Disc_11","Disc_12","Disc_14","Disc_15","Disc_17","Disc_21","Disc_22","Disc_23","Disc_24","Disc_25","Disc_26","Disc_27","Disc_28","Disc_29"]

LABEL = "target"

# Load datasets
training_set = pd.read_csv("Performance_train_jun5.csv", skipinitialspace=True,skiprows=1, names=COLUMNS)
test_set = pd.read_csv("Performance_test_jun5.csv", skipinitialspace=True,skiprows=1, names=COLUMNS)

def my_input_fn(data_set):
  feature_cols = {k: tf.constant(data_set[k].values)
                  for k in FEATURES}
  labels = tf.constant(data_set[LABEL].values)
  return feature_cols, labels

classifier = 
    tf.contrib.learn.DNNClassifier(feature_columns=my_input_fn(training_set),
                                              hidden_units=[10, 20, 10],
                                              n_classes=2,
                                              model_dir="/tmp/h_model")

      INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_evaluation_master': '', '_task_type': None, '_num_ps_replicas': 0, '_keep_checkpoint_every_n_hours': 10000, '_master': '', '_num_worker_replicas': 0, '_save_checkpoints_steps': None, '_model_dir': None, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x000000000DA51908>, '_keep_checkpoint_max': 5, '_save_checkpoints_secs': 600, '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, '_tf_random_seed': None, '_task_id': 0, '_environment': 'local', '_save_summary_steps': 100, '_is_chief': True}


classifier.fit(input_fn=my_input_fn(training_set), steps=2000)

TypeError: 'tuple' object is not callable

关于上述代码,我有以下问题:

1)因为我的FEATURES是int和float数据类型的混合。它们会在将它们转换为张量时引起问题吗?

2)我的理解是classifier.fit中的my_input_fn调用应该同时读取特征和目标数据。我在这里错过了什么吗?

2 个答案:

答案 0 :(得分:0)

首先,错误:TypeError: 'tuple' object is not callable由于对元组的错误访问,它只是一个标准的python错误。例如:

创建一个元组:my_tuple = ('elem1','elem2')

正确的方式访问它:
在[0]中: my_tuple[0]
Out [0] 'elem1'

错误方式访问它:
在[1]中: my_tuple(0)(注意括号)
Out [1] TypeError: 'tuple' object is not callable

因此,该错误只会告诉您,您正在尝试使用括号 访问 元组值,而不是<强烈的>方形的braquets,这是要走的路。

问题

1)是的,如果预处理不好,会导致问题。您的问题可能会将int功能转换为浮动?也许你可以尝试这种方法。

2)我没有检查功能是否正确,但是,它应该这样做。事情是函数返回一个元组,你必须以正确的方式访问这些值(在上面公开)

注意:如果您需要更多帮助,请在某处发布您的csv文件(&#39; Performance_train_jun5.csv&#39;)会有很多帮助

答案 1 :(得分:0)

根据Tensorflow的文档,参数&#39; input_fn&#39;必须接收函数对象(即input_fn = my_input_fn),而不是函数调用的返回值。这就是你的TypeError与&#39; fit&#39;的原因。查看以下链接,了解如何使用带有input_fn参数参数的函数。 https://www.tensorflow.org/get_started/input_fn