我正在研究TensorFlow上的项目,并且正在尝试使用线性回归器训练模型.要将数据添加到估算器上,我正在使用函数tf.estimator.inputs.pandas_input_fn(),但由于存在一些问题,因此无法启动训练.我收到此错误:
TypeError: Failed to convert object of type <class 'dict'> to Tensor. Contents: {'dispositionSoldamount': <tf.Tensor 'random_shuffle_queue_DequeueMany:4' shape=(128,) dtype=float64>}. ConsIDer casting elements to a supported type.
我试图将yData更改为pandas.core.serIEs.SerIEs,但这并没有改变结果.
有人有解决我问题的解决方案吗?
另外,我使用sklearn.linear_regression用相同的DataSet训练了另一个模型,并且该模型可以正常工作.
这是我的代码:
FEATURES = ["dispositionMileage","PurchasePrice","Age"] # Xfeature_cols = [tf.feature_column.numeric_column(k) for k in FEATURES]estimator = tf.estimator.linearRegressor(feature_columns=feature_cols,model_dir="train")def get_input_fn( num_epochs=None,n_batch = 128,shuffle=True): return tf.estimator.inputs.pandas_input_fn( x=Xdata,y=ydata,batch_size=n_batch,num_epochs=num_epochs,shuffle=shuffle) estimator.train(input_fn=get_input_fn(num_epochs=None,shuffle=True),steps=1000)
使用的数据:
Xdata type is pandas.core.frame.DataFrame: dispositionMileage PurchasePrice Age9741 3849 16472.0 09744 3849 16472.0 09745 3849 16472.0 09748 3849 16472.0 0 ...[18105 rows x 3 columns]ydata type is pandas.core.frame.DataFrame: dispositionSoldamount9741 1650.09744 1650.09745 1650.09748 1650.013465 7750.0 ...[18105 rows x 1 columns]
完整的回溯:
WARNING:tensorflow:From /home/USER/.local/lib/python3.6/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 /home/USER/.local/lib/python3.6/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.Traceback (most recent call last): file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py",line 527,in make_tensor_proto str_values = [compat.as_bytes(x) for x in proto_values] file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py",in <Listcomp> str_values = [compat.as_bytes(x) for x in proto_values] file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/util/compat.py",line 61,in as_bytes (bytes_or_text,))TypeError: Expected binary or unicode string,got {'dispositionSoldamount': <tf.Tensor 'random_shuffle_queue_DequeueMany:4' shape=(128,) dtype=float64>}During handling of the above exception,another exception occurred:Traceback (most recent call last): file "tuto.py",line 85,in <module> estimator.train(input_fn=get_input_fn(num_epochs=None,steps=1000) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py",line 354,in train loss = self._train_model(input_fn,hooks,saving_Listeners) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py",line 1207,in _train_model return self._train_model_default(input_fn,line 1237,in _train_model_default features,labels,model_fn_lib.ModeKeys.TRAIN,self.config) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/estimator/estimator.py",line 1195,in _call_model_fn model_fn_results = self._model_fn(features=features,**kwargs) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/estimator/canned/linear.py",line 537,in _model_fn sparse_combiner=sparse_combiner) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/estimator/canned/linear.py",line 215,in _linear_model_fn logits=logits) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/estimator/canned/head.py",line 239,in create_estimator_spec regularization_losses)) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/estimator/canned/head.py",line 1482,in _create_tpu_estimator_spec features=features,mode=mode,logits=logits,labels=labels) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/estimator/canned/head.py",line 1381,in create_loss expected_labels_dimension=self._logits_dimension) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/estimator/canned/head.py",line 305,in _check_dense_labels_match_logits_and_reshape labels = sparse_tensor.convert_to_tensor_or_sparse_tensor(labels) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/framework/sparse_tensor.py",line 279,in convert_to_tensor_or_sparse_tensor value,dtype=dtype,name=name) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py",line 1146,in internal_convert_to_tensor ret = conversion_func(value,name=name,as_ref=as_ref) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py",line 229,in _constant_tensor_conversion_function return constant(v,name=name) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py",line 208,in constant value,shape=shape,verify_shape=verify_shape)) file "/home/USER/.local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py",line 531,in make_tensor_proto "supported type." % (type(values),values))TypeError: Failed to convert object of type <class 'dict'> to Tensor. Contents: {'dispositionSoldamount': <tf.Tensor 'random_shuffle_queue_DequeueMany:4' shape=(128,) dtype=float64>}. ConsIDer casting elements to a supported type.
最佳答案您需要将ydata数据框转换为pandas.SerIEsydata = pd.SerIEs(ydata[column_name])
检查了随机数据,它正在工作.我实际上感到很惊讶,看起来在新的TF版本中tf.estimator.inputs.pandas_input_fn不接受数据框作为标签. 总结
以上是内存溢出为你收集整理的python-TensorFlow导入数据以进行线性回归 全部内容,希望文章能够帮你解决python-TensorFlow导入数据以进行线性回归 所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)