keras 线性回归 预测

#!/usr/bin/env python
# -*- coding:UTF-8 -*-
import numpy as np
np.random.seed(1337)  # for reproducibility
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt 
# 可视化模块

X = np.linspace(-1, 1, 200)
np.random.shuffle(X)    # randomize the data
Y = 0.9 * X + 2 + np.random.normal(0, 0.05, (200, ))
# plot data
#plt.scatter(X, Y)
#plt.show()
 
X_train, Y_train = X[:160], Y[:160]     # train 前 160 data points
X_test, Y_test = X[160:], Y[160:]       # test 后 40 data points
#plt.plot(X_train, Y_train,'.')
#plt.show()

model = Sequential()
l1 = Dense(output_dim = 1,input_dim = 1)
model.add(l1)

model.compile(loss = 'mse',optimizer = 'sgd')
model.fit(X_train,Y_train,epochs= 3000)

print(l1.get_weights())
Xe = np.linspace(1, 2, 20)
Ye = model.predict(Xe)

plt.plot(X_train, Y_train,'.',Xe,Ye,'.')
plt.show()


 

 

运行结果

Epoch 3000/3000
160/160 [==============================] - 0s 30us/step - loss: 0.0026
[array([[0.8913625]], dtype=float32), array([2.0040555], dtype=float32)]
训练得到的权重和偏置和理论值0.9  2 很接近了。

下图中橙色点是预测部分,蓝色点是训练数据。

keras 线性回归 预测_第1张图片

代码托管在github

https://github.com/sofiathefirst/AIcode

你可能感兴趣的:(深度学习,python,tensorflow)