keras之模擬線性回歸(一)

0 前言

要求:

  • 安裝keras庫
  • 如有疑問,評論區(qū)留言

1 搭建模型

#這個(gè)demo,做一個(gè)簡單的線性回歸預(yù)測
# 步驟:
# 目標(biāo):是使得網(wǎng)絡(luò)模型擬和一條 y=2*x+1 的函數(shù)圖像,
# 1、首先在 y=2*x+1 附近生成一些點(diǎn),其中的x作為輸入數(shù)據(jù),y作為輸出數(shù)據(jù)
# 2、訓(xùn)練一個(gè)神經(jīng)網(wǎng)絡(luò)模型,包括100個(gè)神經(jīng)元的全連接層和最終1個(gè)神經(jīng)元的輸出層。下面會提供網(wǎng)絡(luò)模型圖片
# 3、訓(xùn)練大概1萬次左右,即更新一萬次參數(shù)。最后隨機(jī)給出幾個(gè)x,預(yù)測是否符合 y=2*x+1 的標(biāo)準(zhǔn)
# 4、給改模型做一個(gè)keras的可視化分析
在這里插入圖片描述

1.1、生成數(shù)據(jù)

import numpy as np
import keras
from keras import Sequential
import matplotlib.pyplot as plt

number = 100
x = np.linspace(-100,100,number)
y = 2*x+1+np.random.rand(number)*(number*0.3)
# print(y)
plt.scatter(x,y)
plt.xlabel("x number")
plt.ylabel("y real number")
plt.title("x & y scatter graph")
# plt.plot(x,y)
plt.show()
# print (x)

運(yùn)行結(jié)果:生成在 y=2*x+1 上下震蕩的數(shù)據(jù)。


在這里插入圖片描述

1.2、訓(xùn)練模型

模型結(jié)構(gòu)搭建:

from keras.layers import Dense
from keras.layers import Dropout 
from keras import optimizers

model = Sequential()
model.add(Dense(100,kernel_initializer='uniform',activation="relu"))
model.add(Dropout(0.25))
model.add(Dense(1))

sgd = optimizers.SGD(lr=0.000001, decay=1e-6, momentum=0.9, nesterov=True)
# 學(xué)習(xí)率要設(shè)置小一點(diǎn),否則直接nan
model.compile(loss='mse',optimizer=sgd)
# loss根據(jù)目標(biāo)不同設(shè)置不同的損失函數(shù),常用有交叉嫡,均方差
model.fit(x,y,batch_size=100,epochs=500,verbose=1)
# 一共更新參數(shù)500次

# fit 中的 verbose:
# verbose:日志顯示
# verbose = 0 為不在標(biāo)準(zhǔn)輸出流輸出日志信息
# verbose = 1 為輸出進(jìn)度條記錄
# verbose = 2 為每個(gè)epoch輸出一行記錄

運(yùn)行結(jié)果(以下圖截取部分):


在這里插入圖片描述

1.3、預(yù)測性能

x_test = x[50:100]
y_test = y[50:100]
y_pred = model.predict(x_test)

plt.scatter(x_test,y_test)
plt.plot(x_test,y_pred)
plt.show()

運(yùn)行結(jié)果:散點(diǎn)表示真實(shí)數(shù)據(jù),直線是我們預(yù)測數(shù)據(jù)

在這里插入圖片描述

1.4、模型結(jié)構(gòu)可視化

模型可視化很簡單,直接調(diào)用keras庫即可。

# keras網(wǎng)絡(luò)結(jié)構(gòu)可視化
from keras.utils import plot_model
plot_model(model,show_shapes='True')

運(yùn)行結(jié)果:


在這里插入圖片描述
print(model.summary())

運(yùn)行結(jié)果:


在這里插入圖片描述

2 源代碼

本人在google colab環(huán)境下完成編碼。以下代碼也能在jupyter notebook上運(yùn)行。個(gè)人推薦google colab,至于為什么,詳見
Google colab使用之手把手教學(xué)

#這個(gè)demo,做一個(gè)簡單的線性回歸預(yù)測
# 步驟:
# 目標(biāo):是使得網(wǎng)絡(luò)模型擬和一條 y=2*x+1 的函數(shù)圖像,
# 1、首先在 y=2*x+1 附近生成一些點(diǎn),其中的x作為輸入數(shù)據(jù),y作為輸出數(shù)據(jù)
# 2、訓(xùn)練一個(gè)神經(jīng)網(wǎng)絡(luò)模型,包括100個(gè)神經(jīng)元的全連接層和最終1個(gè)神經(jīng)元的輸出層。下面會提供網(wǎng)絡(luò)模型圖片
# 3、訓(xùn)練大概1萬次左右,即更新一萬次參數(shù)。最后隨機(jī)給出幾個(gè)x,預(yù)測是否符合 y=2*x+1 的標(biāo)準(zhǔn)
# 4、給改模型做一個(gè)keras的可視化分析

# 1、生成數(shù)據(jù)
import numpy as np
import keras
from keras import Sequential
import matplotlib.pyplot as plt

number = 100
x = np.linspace(-100,100,number)
y = 2*x+1+np.random.rand(number)*(number*0.3)
plt.scatter(x,y)
plt.xlabel("x number")
plt.ylabel("y real number")
plt.title("x & y scatter graph")
plt.show()


# 2、訓(xùn)練模型
from keras.layers import Dense
from keras.layers import Dropout 
from keras import optimizers

model = Sequential()
model.add(Dense(100,kernel_initializer='uniform',activation="relu"))
model.add(Dropout(0.25))
model.add(Dense(1))

sgd = optimizers.SGD(lr=0.000001, decay=1e-6, momentum=0.9, nesterov=True)
# 學(xué)習(xí)率要設(shè)置小一點(diǎn),否則直接nan
model.compile(loss='mse',optimizer=sgd)
# loss根據(jù)目標(biāo)不同設(shè)置不同的損失函數(shù),常用有交叉嫡,均方差
model.fit(x,y,batch_size=100,epochs=500,verbose=1)
# 一共更新參數(shù)500次

# fit 中的 verbose:
# verbose:日志顯示
# verbose = 0 為不在標(biāo)準(zhǔn)輸出流輸出日志信息
# verbose = 1 為輸出進(jìn)度條記錄
# verbose = 2 為每個(gè)epoch輸出一行記錄

# 3、預(yù)測性能
x_test = x[50:100]
y_test = y[50:100]
y_pred = model.predict(x_test)

plt.scatter(x_test,y_test)
plt.plot(x_test,y_pred)
plt.show()

# 4、模型可視化
from keras.utils import plot_model
plot_model(model,show_shapes='True')
print(model.summary())
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容