线性回归模型
这里注意 tensorflow2.0默认代的不是完整版 keras
pip install keras -U --pre
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
print(tf.__version__)
from keras.models import Sequential
from keras.layers import Dense
X=np.linspace(-1,1,200)
np.random.shuffle(X) # 将数据集随机化
Y=0.5*X+2+np.random.normal(0,0.05,(200,))# 假设我们真实模型为:Y=0.5X+2
plt.scatter(X,Y)
plt.show()
X_train,Y_train=X[:160],Y[:160]
X_test,Y_test=X[160:],Y[160:]
model=Sequential()# Keras有两种类型的模型,序贯模型(Sequential)和函数式模型
# 比较常用的是Sequential,它是单输入单输出的
model.add(Dense(input_dim=1, units=1))# 通过add()方法一层层添加模型
# Dense是全连接层,第一层需要定义输入,
# 第二层无需指定输入,一般第二层把第一层的输出作为输入
# 定义完模型就需要训练了,不过训练之前我们需要指定一些训练参数
# 通过compile()方法选择损失函数和优化器
# 这里我们用均方误差作为损失函数,随机梯度下降作为优化方法
model.compile(loss='mse',optimizer='sgd')
print("training...")
for step in range(301):
cost=model.train_on_batch(X_train,Y_train)
print('traincost',cost)
training... traincost 4.216369 traincost 4.0576506 traincost 3.905112 traincost 3.7585092 traincost 3.6176078 traincost 3.482185 traincost 3.352024 traincost 3.226918 traincost 3.1066682 traincost 2.9910836 traincost 2.8799806 traincost 2.773183 traincost 2.6705215 traincost 2.5718336 traincost 2.4769628 traincost 2.3857594 traincost 2.2980797 traincost 2.2137847 traincost 2.1327415 traincost 2.054823 traincost 1.9799061 traincost 1.9078728 traincost 1.8386104 traincost 1.7720104 traincost 1.7079678 traincost 1.6463829 traincost 1.5871592 traincost 1.5302042 traincost 1.4754289 traincost 1.4227486 traincost 1.3720804 traincost 1.3233463 traincost 1.27647 traincost 1.2313795 traincost 1.1880047 traincost 1.1462784 traincost 1.1061363 traincost 1.0675167 traincost 1.0303605 traincost 0.99460983 traincost 0.9602106 traincost 0.92710984 traincost 0.8952567 traincost 0.86460304 traincost 0.8351019 traincost 0.8067085 traincost 0.77937984 traincost 0.75307435 traincost 0.7277524 traincost 0.70337594 traincost 0.6799079 traincost 0.65731347 traincost 0.63555855 traincost 0.6146107 traincost 0.59443855 traincost 0.5750123 traincost 0.5563029 traincost 0.53828263 traincost 0.520925 traincost 0.5042044 traincost 0.4880963 traincost 0.472577 traincost 0.45762387 traincost 0.44321498 traincost 0.42932963 traincost 0.41594753 traincost 0.40304962 traincost 0.39061695 traincost 0.378632 traincost 0.3670775 traincost 0.35593694 traincost 0.34519458 traincost 0.33483508 traincost 0.3248439 traincost 0.31520706 traincost 0.305911 traincost 0.29694295 traincost 0.2882902 traincost 0.27994087 traincost 0.2718835 traincost 0.26410693 traincost 0.25660056 traincost 0.24935432 traincost 0.2423583 traincost 0.23560306 traincost 0.22907951 traincost 0.22277899 traincost 0.21669313 traincost 0.21081392 traincost 0.20513353 traincost 0.1996446 traincost 0.19434002 traincost 0.18921284 traincost 0.18425646 traincost 0.17946461 traincost 0.17483108 traincost 0.17035016 traincost 0.16601607 traincost 0.16182347 traincost 0.1577671 traincost 0.15384206 traincost 0.15004347 traincost 0.14636669 traincost 0.14280725 traincost 0.1393609 traincost 0.13602349 traincost 0.13279115 traincost 0.12966004 traincost 0.12662646 traincost 0.12368695 traincost 0.12083809 traincost 0.11807661 traincost 0.1153994 traincost 0.11280348 traincost 0.1102859 traincost 0.107843935 traincost 0.105474874 traincost 0.10317614 traincost 0.100945294 traincost 0.098779894 traincost 0.09667765 traincost 0.0946364 traincost 0.09265399 traincost 0.09072839 traincost 0.08885764 traincost 0.08703984 traincost 0.08527314 traincost 0.08355583 traincost 0.08188622 traincost 0.080262646 traincost 0.0786836 traincost 0.077147566 traincost 0.075653054 traincost 0.07419874 traincost 0.07278321 traincost 0.07140521 traincost 0.07006353 traincost 0.068756916 traincost 0.06748423 traincost 0.06624437 traincost 0.065036274 traincost 0.06385889 traincost 0.062711254 traincost 0.06159237 traincost 0.060501337 traincost 0.059437264 traincost 0.05839932 traincost 0.05738666 traincost 0.05639849 traincost 0.055434037 traincost 0.054492556 traincost 0.05357336 traincost 0.052675746 traincost 0.05179907 traincost 0.05094268 traincost 0.050105996 traincost 0.04928838 traincost 0.04848926 traincost 0.047708116 traincost 0.046944384 traincost 0.046197575 traincost 0.045467213 traincost 0.044752765 traincost 0.044053786 traincost 0.04336986 traincost 0.042700525 traincost 0.04204539 traincost 0.041404035 traincost 0.040776096 traincost 0.040161185 traincost 0.03955894 traincost 0.038969003 traincost 0.038391046 traincost 0.037824757 traincost 0.037269793 traincost 0.03672588 traincost 0.036192704 traincost 0.03566999 traincost 0.035157472 traincost 0.034654856 traincost 0.034161918 traincost 0.033678383 traincost 0.033204023 traincost 0.0327386 traincost 0.032281894 traincost 0.03183369 traincost 0.03139376 traincost 0.030961925 traincost 0.030537969 traincost 0.030121703 traincost 0.029712945 traincost 0.029311508 traincost 0.028917229 traincost 0.02852993 traincost 0.028149456 traincost 0.02777564 traincost 0.027408326 traincost 0.027047377 traincost 0.026692647 traincost 0.02634399 traincost 0.026001269 traincost 0.025664348 traincost 0.025333118 traincost 0.025007427 traincost 0.024687175 traincost 0.024372231 traincost 0.024062494 traincost 0.02375785 traincost 0.023458183 traincost 0.023163397 traincost 0.022873381 traincost 0.022588048 traincost 0.022307292 traincost 0.022031024 traincost 0.021759152 traincost 0.021491585 traincost 0.021228248 traincost 0.020969043 traincost 0.020713892 traincost 0.020462718 traincost 0.020215448 traincost 0.019972008 traincost 0.019732315 traincost 0.019496303 traincost 0.019263903 traincost 0.019035045 traincost 0.018809652 traincost 0.018587684 traincost 0.018369064 traincost 0.018153723 traincost 0.017941615 traincost 0.017732676 traincost 0.017526846 traincost 0.017324071 traincost 0.017124299 traincost 0.016927475 traincost 0.016733546 traincost 0.016542466 traincost 0.016354186 traincost 0.016168648 traincost 0.01598582 traincost 0.015805641 traincost 0.015628079 traincost 0.015453073 traincost 0.015280595 traincost 0.0151106 traincost 0.014943043 traincost 0.014777884 traincost 0.014615083 traincost 0.014454606 traincost 0.014296411 traincost 0.014140455 traincost 0.013986712 traincost 0.013835144 traincost 0.013685715 traincost 0.01353839 traincost 0.013393136 traincost 0.013249922 traincost 0.013108711 traincost 0.012969477 traincost 0.01283219 traincost 0.012696815 traincost 0.012563321 traincost 0.012431686 traincost 0.012301879 traincost 0.012173861 traincost 0.012047628 traincost 0.0119231315 traincost 0.011800354 traincost 0.011679266 traincost 0.011559845 traincost 0.011442062 traincost 0.0113259 traincost 0.011211328 traincost 0.011098323 traincost 0.010986872 traincost 0.010876941 traincost 0.0107685095 traincost 0.010661555 traincost 0.010556059 traincost 0.010451991 traincost 0.010349341 traincost 0.0102480855 traincost 0.010148199 traincost 0.010049667 traincost 0.009952474 traincost 0.009856592 traincost 0.009762008 traincost 0.009668704 traincost 0.009576656 traincost 0.009485848 traincost 0.009396264 traincost 0.009307886 traincost 0.009220699 traincost 0.00913468 traincost 0.009049815 traincost 0.008966093 traincost 0.008883496 traincost 0.008802002 traincost 0.0087216 traincost 0.008642277
cost=model.evaluate(X_test,Y_test,batch_size=40)
print('test cost',cost)
W,b = model.layers[0].get_weights()
# 查看训练出的网络参数
# 由于我们网络只有一层,且每次训练的输入只有一个,输出只有一个
# 因此第一层训练出Y=WX+B这个模型,其中W,b为训练出的参数
print("weights",W,'\n biases',b)
40/40 [==============================] - 0s 722us/step test cost 0.007749117910861969 weights [[0.6209358]] biases [1.9840264]
Y_pre=model.predict(X_test)
plt.scatter(X_test,Y_test)
plt.plot(X_test,Y_pre)
plt.show()