Estimator Api:使用Estimator API训练时出错

时间:2018-11-24 06:10:44

标签: tensorflow-estimator

我是ML的新手。我不确定如何进行数据重塑概念。之前,tf.estimator对我来说效果很好。但是,当我尝试使用新的大型数据集进行实验时,事情就开始失败了。

我正在处理以下代码。

import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
file='creditcard/creditcard.csv'
dataset=pd.read_csv(file)
dataset['Class']=dataset['Class'].astype('int')
print('Shape of dataset ',dataset.shape)
label=dataset['Class']
data=dataset.drop(['Class'],axis=1)
trainX,testX,trainY,testY=train_test_split(data,label,test_size=.3,random_state=101)
print('Shape of train data : (',trainX.shape,trainY.shape,')')
print('Shape of test data : (',testX.shape,testY.shape,')')
feature_value=[tf.feature_column.numeric_column('x')]
estimator=tf.estimator.LinearRegressor(feature_value)
train_input=tf.estimator.inputs.numpy_input_fn({'x':trainX.values},trainY.values,batch_size=512,shuffle=True)
test_input=tf.estimator.inputs.numpy_input_fn({'x':testX.values},testY.values,batch_size=512,shuffle=True)
estimator.train(input_fn=train_input,steps=1000)

当我训练模型时,我遇到了错误。

有人可以帮助以下问题吗?

  1. 如何识别正确的形状。
  2. 我需要对数据形状进行哪些校正。

错误:

C:\Users\vineet\AppData\Local\Programs\Python\Python36\python.exe C:/Users/vineet/Desktop/selfML/creditCard.py
Shape of dataset  (284807, 31)
WARNING:tensorflow:Using temporary folder as model directory: C:\Users\vineet\AppData\Local\Temp\tmpbyc90zqm
Shape of train data : ( (199364, 30) (199364,) )
Shape of test data : ( (85443, 30) (85443,) )
WARNING:tensorflow:From C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\inputs\queues\feeding_queue_runner.py:62: QueueRunner.__init__ (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
WARNING:tensorflow:From C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\inputs\queues\feeding_functions.py:500: add_queue_runner (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
2018-11-24 11:19:24.242005: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
WARNING:tensorflow:From C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py:804: start_queue_runners (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version.
Instructions for updating:
To construct input pipelines, use the `tf.data` module.
Traceback (most recent call last):
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1292, in _do_call
    return fn(*args)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1277, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1367, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 15360 values, but the requested shape has 512
	 [[{{node linear/linear_model_1/linear_model/x/Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](linear/linear_model_1/linear_model/x/ToFloat, linear/linear_model_1/linear_model/x/Reshape/shape)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/vineet/Desktop/selfML/creditCard.py", line 19, in <module>
    estimator.train(input_fn=train_input,steps=1000)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 356, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1181, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1215, in _train_model_default
    saving_listeners)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1409, in _train_with_estimator_spec
    _, loss = mon_sess.run([estimator_spec.train_op, estimator_spec.loss])
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py", line 671, in run
    run_metadata=run_metadata)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1148, in run
    run_metadata=run_metadata)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1239, in run
    raise six.reraise(*original_exc_info)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\six.py", line 693, in reraise
    raise value
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1224, in run
    return self._sess.run(*args, **kwargs)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1296, in run
    run_metadata=run_metadata)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\training\monitored_session.py", line 1076, in run
    return self._sess.run(*args, **kwargs)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 887, in run
    run_metadata_ptr)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1110, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1286, in _do_run
    run_metadata)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1308, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 15360 values, but the requested shape has 512
	 [[{{node linear/linear_model_1/linear_model/x/Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](linear/linear_model_1/linear_model/x/ToFloat, linear/linear_model_1/linear_model/x/Reshape/shape)]]

Caused by op 'linear/linear_model_1/linear_model/x/Reshape', defined at:
  File "C:/Users/vineet/Desktop/selfML/creditCard.py", line 19, in <module>
    estimator.train(input_fn=train_input,steps=1000)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 356, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1181, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1211, in _train_model_default
    features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\estimator.py", line 1169, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\canned\linear.py", line 492, in _model_fn
    sparse_combiner=sparse_combiner)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\canned\linear.py", line 163, in _linear_model_fn
    logits = logit_fn(features=features)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\estimator\canned\linear.py", line 101, in linear_logit_fn
    cols_to_vars=cols_to_vars)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\feature_column\feature_column.py", line 464, in linear_model
    retval = linear_model_layer(features)  # pylint: disable=not-callable
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 769, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\feature_column\feature_column.py", line 647, in call
    weighted_sum = layer(builder)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\layers\base.py", line 364, in __call__
    outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 769, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\feature_column\feature_column.py", line 539, in call
    weight_var=self._weight_var)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\feature_column\feature_column.py", line 2030, in _create_weighted_sum
    weight_var=weight_var)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\feature_column\feature_column.py", line 2046, in _create_dense_column_weighted_sum
    tensor = array_ops.reshape(tensor, shape=(batch_size, num_elements))
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 7546, in reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\util\deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 3272, in create_op
    op_def=op_def)
  File "C:\Users\vineet\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1768, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 15360 values, but the requested shape has 512
	 [[{{node linear/linear_model_1/linear_model/x/Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](linear/linear_model_1/linear_model/x/ToFloat, linear/linear_model_1/linear_model/x/Reshape/shape)]]


Process finished with exit code 1

0 个答案:

没有答案