Tensorflow DNNRegressor 预测分数输出

我正在尝试在一些简单的数据上运行DNNRegressor,以便测试其准确性,模型应该能够接受任何银行交易并尝试预测其价格,但我在得到一些奇怪的结果,我认为这是由于代码中某些地方出了问题。

我的代码如下:

from __future__ import print_functionfrom __future__ import divisionfrom __future__ import absolute_importimport itertoolsimport pandas as pdimport numpy as npimport tensorflow as tfprint('Running version of tensorflow')print(tf.__version__)tf.logging.set_verbosity(tf.logging.DEBUG)names = [    'trans',    'price',]predict_names = [    'trans']dtypes = {    'trans': str,    'price': np.float32,}df = pd.read_csv('simple.csv', names=names, dtype=dtypes, na_values='?')# Split the data into a training set and an eval set.training_data = df[:50]eval_data = df[50:]print("Training with this :\n")print(training_data)# Separate input features from labelstraining_label = training_data.pop('price')eval_label = eval_data.pop('price')# Feature Columnstraining_input_fn = tf.estimator.inputs.pandas_input_fn(x=training_data, y=training_label, batch_size=1, shuffle=True, num_epochs=None)eval_input_fn = tf.estimator.inputs.pandas_input_fn(x=eval_data, y=eval_label, batch_size=1, shuffle=False, num_epochs=None)#Embed the column since its a stringtransformed_trans = tf.feature_column.categorical_column_with_hash_bucket('trans', 50)print("Transformed words **********************")print(transformed_trans)dnn_features = [tf.feature_column.indicator_column(transformed_trans)]# regressor = tf.contrib.learn.LinearRegressor(feature_columns=[trans])dnnregressor = tf.contrib.learn.DNNRegressor(feature_columns=dnn_features, hidden_units=[50, 30, 10])#train the modeldnnregressor.fit(input_fn=training_input_fn, steps=1)# Evaluate the trianingdnnregressor.evaluate(input_fn=eval_input_fn, steps=1)# Predictionspredictdf = pd.read_csv('simple_predict.csv', names=names, dtype=dtypes, na_values='?')predict_input_fn = tf.estimator.inputs.pandas_input_fn(x=predictdf,shuffle=False, num_epochs=1)print("Predicting scores **********************")y = dnnregressor.predict_scores(input_fn=predict_input_fn)for x in y:    print(str(x)+"\n")

我的数据看起来像这样

simple.csv:Uber,4Food,12Coffee,4Cafe,10Coffee,4Cafe,10Uber,4Food,12Coffee,4Cafe,10Coffee,4Cafe,10Uber,4Food,12Coffee,4Cafe,10Coffee,4simple_predict.csv:UberFood

我认为对于这样一个相当可预测的数据集,我会得到0损失,并且预测结果会非常准确。但事实并非如此,我总是得到Uber和Food的预测结果完全相同,我甚至无法理解我得到的结果。

我的代码是否遗漏了什么?还是我误解了DNNRegressor应该如何工作?


回答:

上述代码的问题在于哈希率太低。只要将哈希率从50提高到300,一切就会正常工作。

transformed_trans = tf.feature_column.categorical_column_with_hash_bucket('trans', 300)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注