Tensorflow DNNclassifier:错误训练(numpy.ndarray没有属性索引) [英] Tensorflow DNNclassifier: error wile training (numpy.ndarray has no attribute index)

查看:112
本文介绍了Tensorflow DNNclassifier:错误训练(numpy.ndarray没有属性索引)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在张量流中训练DNNClassifier

I am trying to train a DNNClassifier in tensorflow

这是我的代码

train_input_fn = tf.estimator.inputs.pandas_input_fn(
    x=X_train,
    y=y_train,
    batch_size=1000,
    shuffle = True
)


nn_classifier = tf.estimator.DNNClassifier(hidden_units=[1300,1300,1300], feature_columns=X_train, n_classes=200)
nn_classifier.train(input_fn = train_input_fn,  steps=2000)

这是y_train的外观

[450 450 450 ... 327 327 327]

类型: numpy.ndarray

这是X_train的外观

[[ 9.79285  11.659035  1.279528 ...  1.258979  1.063923 -2.45522 ]
 [ 8.711333 13.92955   1.117603 ...  3.588921  1.231256 -3.180302]
 [ 5.159803 14.059619  1.740708 ...  0.28172  -0.506701 -1.326669]
 ...
 [ 2.418473  0.542642 -3.658447 ...  4.631474  4.544892 -4.595605]
 [ 6.51176   4.321688 -1.483697 ...  3.13299   5.476103 -2.833903]
 [ 6.894113  5.986267 -1.178247 ...  2.305603  7.217919 -2.152574]]

类型: numpy.ndarray

错误:

in pandas_input_fn(x, y, batch_size, num_epochs, shuffle, queue_capacity, num_threads, target_column)
     85           'Cannot use name %s for target column: DataFrame already has a '
     86           'column with that name: %s' % (target_column, x.columns))
---> 87     if not np.array_equal(x.index, y.index):
     88       raise ValueError('Index for x and y are mismatched.\nIndex for x: %s\n'
     89                        'Index for y: %s\n' % (x.index, y.index))

更新1:使用numpy_input_fn

train_input_fn= tf.estimator.inputs.numpy_input_fn(
    x=X_train,
    y=y_train,
    batch_size=1000,
    shuffle = True
)

错误:

INFO:tensorflow:Calling model_fn.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-3b7c6b879e38> in <module>()
     10 start_time = time.time()
     11 nn_classifier = tf.estimator.DNNClassifier(hidden_units=[1300,1300,1300], feature_columns=X_train, n_classes=200)
---> 12 nn_classifier.train(input_fn = train_input_fn,  steps=2000)
     13 total_time = start_time - time.time()

c:\users\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\estimator\estimator.py in train(self, input_fn, hooks, steps, max_steps, saving_listeners)
    353 
    354     saving_listeners = _check_listeners_type(saving_listeners)
--> 355     loss = self._train_model(input_fn, hooks, saving_listeners)
    356     logging.info('Loss for final step: %s.', loss)
    357     return self

c:\users\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\estimator\estimator.py in _train_model(self, input_fn, hooks, saving_listeners)
    822       worker_hooks.extend(input_hooks)
    823       estimator_spec = self._call_model_fn(
--> 824           features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
    825 
    826       if self._warm_start_settings:

c:\users\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\estimator\estimator.py in _call_model_fn(self, features, labels, mode, config)
    803 
    804     logging.info('Calling model_fn.')
--> 805     model_fn_results = self._model_fn(features=features, **kwargs)
    806     logging.info('Done calling model_fn.')
    807 

c:\users\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\estimator\canned\dnn.py in _model_fn(features, labels, mode, config)
    347           head=head,
    348           hidden_units=hidden_units,
--> 349           feature_columns=tuple(feature_columns or []),
    350           optimizer=optimizer,
    351           activation_fn=activation_fn,

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

任何提示我在做什么错吗?

Any clue what I am doing wrong?

推荐答案

我今天遇到了这个错误,并认为如果我证明了解决方案那就太好了. 问题是由tf.estimator.inputs.numpy_input_fn引起的.根据 TensorFlow文档X必须为pandas.DataFrame实例和y必须是pandas.Seriespandas.DataFrame实例. type()函数可以帮助确定X_trainy_train值的数据类型.将X_trainy_train更改为适当的数据类型可以解决此问题.

I came across this error today and thought it would be great if I proved a solution. The problem is brought about by tf.estimator.inputs.numpy_input_fn. according to the TensorFlow docs, X must be a pandas.DataFrame instance and y must be a pandas.Series or a pandas.DataFrame instance. The type() function can help determine the data types of your X_train and y_train values. Changing X_train and y_train to the appropriate data type solves the problem.

这篇关于Tensorflow DNNclassifier:错误训练(numpy.ndarray没有属性索引)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆