使用此dataset尝试通过线性回归对失业进行时间序列预测时,我在此处编写的代码
import tensorflow as tf
import json
trainCount = 25
units = 1
x_train = []
y_train = []
with open("USA_Unemployement.json","r") as jFile:
data = json.loads(jFile.read())
data = data[0]
for year in data:
if year != "2017":
x_train.append([int(year)])
y_train.append([data[year]])
x_train = tf.constant(x_train,dtype=tf.float32)
y_train = tf.constant(y_train,dtype=tf.float32)
#linear regression layer
linear_model = tf.layers.Dense(units=units)
#prediction
y_pred = linear_model(x_train)
#inaccuracy of prediction
loss = tf.losses.mean_squared_error(labels=y_train,predictions=y_pred)
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for iteration in range(trainCount):
_, loss_value = sess.run((train,loss))
print("Iteration: {} Predicted Value with Loss:{}".format(iteration,loss_value))
得到
的响应2018-07-22 22:00:14.925603: I
tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
Iteration: 0 Predicted Value with Loss:2998692.0
Iteration: 1 Predicted Value with Loss:1.932637787442381e+16
Iteration: 2 Predicted Value with Loss:1.2455744272344698e+26
Iteration: 3 Predicted Value with Loss:8.027652540628559e+35
Iteration: 4 Predicted Value with Loss:inf
Iteration: 5 Predicted Value with Loss:inf
Iteration: 6 Predicted Value with Loss:inf
Iteration: 7 Predicted Value with Loss:inf
Iteration: 8 Predicted Value with Loss:inf
Iteration: 9 Predicted Value with Loss:nan
Iteration: 10 Predicted Value with Loss:nan
Iteration: 11 Predicted Value with Loss:nan
Iteration: 12 Predicted Value with Loss:nan
Iteration: 13 Predicted Value with Loss:nan
Iteration: 14 Predicted Value with Loss:nan
Iteration: 15 Predicted Value with Loss:nan
Iteration: 16 Predicted Value with Loss:nan
Iteration: 17 Predicted Value with Loss:nan
Iteration: 18 Predicted Value with Loss:nan
Iteration: 19 Predicted Value with Loss:nan
Iteration: 20 Predicted Value with Loss:nan
Iteration: 21 Predicted Value with Loss:nan
Iteration: 22 Predicted Value with Loss:nan
Iteration: 23 Predicted Value with Loss:nan
Iteration: 24 Predicted Value with Loss:nan
从TensorFlow.org阅读this document后,我尝试制作自己的线性回归示例时,我做了一个简单的代码