Python 为什么TensorFlow线性累加器预测的数字太大?
在一个简单的例子中,我尝试使用TensorFlow线性累加器,但结果不正确。有什么建议吗Python 为什么TensorFlow线性累加器预测的数字太大?,python,tensorflow,linear-regression,Python,Tensorflow,Linear Regression,在一个简单的例子中,我尝试使用TensorFlow线性累加器,但结果不正确。有什么建议吗 import tensorflow as tf import numpy as np x_data =np.array([ 44.57, 42.71, 119.25, 40.83, 46.87, 71.44, 113.5, 39.83, 39.48, 77.3, 53.32 , 21.68 , 113.55 , 40.1 , 77.39 , 46.01 , 35.42 , 93.81
import tensorflow as tf
import numpy as np
x_data =np.array([ 44.57, 42.71, 119.25, 40.83, 46.87, 71.44,
113.5, 39.83, 39.48,
77.3, 53.32 , 21.68 , 113.55 , 40.1 , 77.39 , 46.01 ,
35.42 , 93.81,
84.71, 51.7 , 73.57, 102.21 , 98.05 , 99.53 ,
98.65 , 50.1, 108.4,
62.06, 48.34 , 71.45 , 53.21 , 72.57 , 48.14 ,
71.32 , 41.01 , 96.71,
112.09, 54.87 , 63.17 , 44.95])
y_data= np.array([ 127.42 , 121.09 , 294.53, 96.73, 125.04, 195.08,
287.84, 106.97, 107.94,
204.45, 116.09 , 57.64 , 296.82 , 123.5 , 180.11 , 116.81 ,
96.73 ,233.71,
237.07, 130. , 182.61 , 260.22, 238.86 , 238.02 ,
248.05, 101.41, 269.69,
156.43 , 121.27 , 172.64 , 139.62 , 203.87 , 134.78 ,
176.24 , 106.22 , 252.93,
282.96 ,141.95 ,161. , 123.42])
features = [tf.contrib.layers.real_valued_column("x", dimension=1)]
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features,
model_dir='./linear_estimator')
input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x_data}, y_data,
num_epochs=1000)
estimator.fit(input_fn=input_fn, steps=2000)
np.asarray([i for i in estimator.predict(x={'x': x_data})])
结果是
阵列([1539.316650391476.5541992224059.264892581413.11694336,
1616.92626953, 2445.9987793 , 3865.2409668 , 1379.37365723,
1367.56335449, 2643.734375 , 1834.57043457, 766.93310547,
3866.92822266, 1388.48425293, 2646.77124023, 1587.90698242,
1230.56567383, 3200.83569336, 2893.77197266, 1779.90625 ,
2517.87182617, 3484.27929688, 3343.9074707 , 3393.84741211,
3364.15332031, 1725.91699219, 3693.15039062, 2129.48681641,
1666.52893066, 2446.3359375 , 1830.85864258, 2484.12866211,
1659.78015137, 2441.94946289, 1419.19055176, 3298.69140625,
3817.66284181886.87243652166.941650391552.13916016],dtype=float32)现在它可以工作了
我删除了导出目录并获得了新版本1.1.0
感谢您的所有回答。尝试在输入中使用“batch\u size=1”和“num\u epochs=10”。这对我来说非常适合给定的代码。我得到[114.96195221 110.31800079 301.4190979,…],它不是完美的,但至少具有正确的数量级。您使用哪个TensorFlow版本?考虑到ml4294的评论,请尝试删除您的model_dir并再次运行脚本。这将清理以前的培训课程。