Tensorflow - AttributeError:' dict'对象没有属性' index'

时间:2018-01-26 03:37:54

标签: python tensorflow

我是Tensorflow的新手,请帮助修复此错误。我使用Tensorflow教程代码使用get_input_fn将pandas数据帧输入转换为张量,但我遇到了这个错误。没什么好帮助会很感激。

---------------------------------------------------------------------------
 AttributeError                            Traceback (most recent call last)
<ipython-input-4-8ac4f79a2981> in <module>()
114 
115 if __name__ == '__main__':
--> 116     main()

<ipython-input-4-8ac4f79a2981> in main()
105     Xtrain = pd.concat([Xtrain, ytrain], axis = 1)
106     Xtest = pd.concat([Xtest, ytest], axis = 1)
--> 107     Tensors(Xtrain, Xtest)

<ipython-input-4-8ac4f79a2981> in Tensors(Xtrain, Xtest)
 30     print(feature_cols)
 31     nnreg = tf.contrib.learn.DNNRegressor(feature_columns = 
feature_cols, hidden_units = [10, 10])
 ---> 32     nnreg.fit(input_fn = get_input_fn(Xtrain), steps = 5000)
 33     ev = nnreg.evaluate(input_fn = get_input_fn(Xtest, num_epochs = 1, shuffle = False))
 34     loss_score = ev['loss']

<ipython-input-4-8ac4f79a2981> in get_input_fn(data, num_epochs, shuffle)
 22                     y = tf.constant(data[label].values),
 23                     num_epochs = num_epochs,
---> 24                     shuffle = shuffle)
 25 
 26 def Tensors(Xtrain, Xtest):

C:\Users\user\Anaconda3\lib\site-
 packages\tensorflow\python\estimator\inputs\pandas_io.py in pandas_input_fn(x, y, batch_size, num_epochs, shuffle, queue_capacity, num_threads, target_column)
 83           'Cannot use name %s for target column: DataFrame already has a '
 84           'column with that name: %s' % (target_column, x.columns))
 ---> 85     if not np.array_equal(x.index, y.index):
 86       raise ValueError('Index for x and y are mismatched.\nIndex for x: %s\n'
 87                        'Index for y: %s\n' % (x.index, y.index))

AttributeError: 'dict' object has no attribute 'index'

这是我对pandas dataframe的get_input_fn()。

def get_input_fn(data, num_epochs = None, shuffle = True):
        features = ['name', 'ship', 'GC', 'SC1_Enc', 'SC2_Enc', 'rating', 'condition', 'pol']
        label = ['target']
        return tf.estimator.inputs.pandas_input_fn(
                x = {k: tf.constant(data[k].values) for k in features},
                y = tf.constant(data[label].values),
                num_epochs = num_epochs,
                shuffle = shuffle)

0 个答案:

没有答案