为什么TensorFlow估算器无法进行这个简单的线性回归预测

我目前正在学习TensorFlow,但无法理解为什么TensorFlow在以下简单的回归问题上不能进行正确的预测。

X是从1000到8000之间的随机数,Y是X + 250

所以如果X是2000,那么Y就是2250。这对我来说似乎是一个线性回归问题。然而,当我尝试进行预测时,结果与我预期的相差甚远,例如X为1000时,预测结果是1048,而不是1250。

此外,损失和平均损失都非常大:

{'average_loss': 10269.81, 'loss': 82158.48, 'global_step': 1000}

这是完整的代码:

import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport tensorflow as tffrom sklearn.model_selection import train_test_splitx_data = np.random.randint(1000, 8000, 1000000)y_true = x_data + 250feat_cols = [tf.feature_column.numeric_column('x', shape=[1])]estimator = tf.estimator.LinearRegressor(feature_columns=feat_cols)x_train, x_eval, y_train, y_eval = train_test_split(x_data, y_true, test_size=0.3, random_state=101)input_func = tf.estimator.inputs.numpy_input_fn({'x': x_train}, y_train, batch_size=8, num_epochs=None, shuffle=True)train_input_func = tf.estimator.inputs.numpy_input_fn({'x': x_train}, y_train, batch_size=8, num_epochs=1000, shuffle=False)eval_input_func = tf.estimator.inputs.numpy_input_fn({'x': x_eval}, y_eval, batch_size=8, num_epochs=1000, shuffle=False)estimator.train(input_fn=input_func, steps=1000)train_metrics = estimator.evaluate(input_fn=train_input_func, steps=1000)eval_metrics = estimator.evaluate(input_fn=eval_input_func, steps=1000)print(train_metrics)print(eval_metrics)brand_new_data = np.array([1000, 2000, 7000])input_fn_predict = tf.estimator.inputs.numpy_input_fn({'x': brand_new_data}, shuffle=False)prediction_result = estimator.predict(input_fn=input_fn_predict)print(list(prediction_result))

是我做错了什么,还是我误解了LinearRegression的含义?


回答:

我认为当你调整一些超参数时,它确实会起作用。我还将优化器改成了AdamOptimizer

主要是批量大小设为1,epochs设为None

train_input_func = tf.estimator.inputs.numpy_input_fn({‘x’: x_train}, y_train, batch_size=1, num_epochs=None, shuffle=True)

代码如下:

import tensorflow as tfimport numpy as npfrom sklearn.model_selection import train_test_splitx_data = np.random.randint(1000, 8000, 10000)y_true = x_data + 250feat_cols = tf.feature_column.numeric_column('x')optimizer = tf.train.AdamOptimizer(1e-3)estimator = tf.estimator.LinearRegressor(feature_columns=[feat_cols],optimizer=optimizer)x_train, x_eval, y_train, y_eval = train_test_split(x_data, y_true, test_size=0.3, random_state=101)train_input_func = tf.estimator.inputs.numpy_input_fn({'x': x_train}, y_train, batch_size=1, num_epochs=None,                                                      shuffle=True)eval_input_func = tf.estimator.inputs.numpy_input_fn({'x': x_eval}, y_eval, batch_size=1, num_epochs=None,                                                     shuffle=True)estimator.train(input_fn=train_input_func, steps=1005555)train_metrics = estimator.evaluate(input_fn=train_input_func, steps=10000)eval_metrics = estimator.evaluate(input_fn=eval_input_func, steps=10000)print(train_metrics)print(eval_metrics)brand_new_data = np.array([1000, 2000, 7000])input_fn_predict = tf.estimator.inputs.numpy_input_fn({'x': brand_new_data}, num_epochs=1,shuffle=False)prediction_result = estimator.predict(input_fn=input_fn_predict)for prediction in prediction_result:    print(prediction['predictions'])

指标如下:

{‘average_loss’: 3.9024353e-06, ‘loss’: 3.9024353e-06, ‘global_step’: 1005555}

{‘average_loss’: 3.9721594e-06, ‘loss’: 3.9721594e-06, ‘global_step’: 1005555}

[1250.003]

[2250.002]

[7249.997]

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

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