2017-06-15 54 views
0

我試圖分析數據集60列(FEATURES = 59列(整數和浮點數)的組合,LABEL =列名爲TARGET 0 /值爲1)Tensorflow DNNClassifier.fit錯誤:類型錯誤:「元組」對象不是可調用

我得到以下錯誤,而試圖擬合模型:

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)由於我的功能是int和float數據類型的混合,它們會在將它們轉換爲張量時引發問題嗎?

2)我的理解是,在classifier.fit的my_input_fn呼叫應閱讀功能和目標數據。我在這裏錯過了什麼嗎?第一

回答

0

第一件事,錯誤:TypeError: 'tuple' object is not callable它只是一個標準的Python錯誤,由於錯誤的訪問的元組。例如:

創建一個元組:my_tuple = ('elem1','elem2')

訪問它在方式:
在[0]:my_tuple[0]
缺貨[0]'elem1'

訪問它在一個方式:
[1]中:my_tuple(0)(注括號)
出[1]TypeError: 'tuple' object is not callable

因此,該錯誤只告訴你,你要訪問元組值括號代替平方braquets是要走的路。

質詢

1)是的,這會導致一個問題,如果不能很好的預處理。你的問題可能會使int特性浮動?也許你可以嘗試這種方法。

2)我沒有檢查函數是否正確,但是,它應該這樣做。事情是函數返回一個元組,你必須訪問以正確的方式(以上暴露)

注的價值是:如果你想要更多的幫助,發佈您的CSV文件(「Performance_train_jun5。CSV「)的地方將有很大的幫助

+0

我曾試圖將int特性強制浮動,它似乎工作。我將進一步研究預處理。感謝您的輸入! –

0

根據Tensorflow的文檔,參數‘input_fn’必須接受一個函數對象(即input_fn = my_input_fn),而不是一個函數調用的返回值。這是你的TypeError與'fit'的原因。查看以下鏈接,瞭解如何使用帶有input_fn參數參數的函數。 https://www.tensorflow.org/get_started/input_fn

相關問題