日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Keras实现简单BP神经网络

發布時間:2023/12/29 综合教程 29 生活家
生活随笔 收集整理的這篇文章主要介紹了 Keras实现简单BP神经网络 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

BP 神經網絡的簡單實現

from keras.models import Sequential #導入模型
from keras.layers.core import Dense #導入常用層
train_x,train_y #訓練集
test_x,text_y #測試集
model=Sequential() #初始化模型
model.add(Dense(3,input_shape=(32,),activation='sigmoid',init='uniform'))) #添加一個隱含層,注:只是第一個隱含層需指定input_dim
model.add(Dense(1,activation='sigmoid')) #添加輸出層
model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy'])  # 編譯,指定目標函數與優化方法
model.fit(train_x,train_y ) # 模型訓練
model.evaluate(test_x,text_y ) #模型測試

常用層

常用層對應于core模塊,core內部定義了一系列常用的網絡層,包括全連接、激活層等

Dense層

keras.layers.core.Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)

Dense就是常用的全連接層,所實現的運算是output = activation(dot(input, kernel)+bias)。其中activation是逐元素計算的激活函數,kernel是本層的權值矩陣,bias為偏置向量,只有當use_bias=True才會添加。

如果本層的輸入數據的維度大于2,則會先被壓為與kernel相匹配的大小。

#example

# as first layer in a sequential model:
| model = Sequential()
| model.add(Dense(32, input_shape=(16,)))
| # now the model will take as input arrays of shape (*, 16)
| # and output arrays of shape (*, 32)
|
| # after the first layer, you don't need to specify
| # the size of the input anymore:
| model.add(Dense(32))

Keras主要包括14個模塊,本文主要對Models、layers、Initializations、Activations、Objectives、Optimizers、Preprocessing、metrics共計8個模塊分別展開介紹。

1. Model
包:keras.models
這是Keras中最主要的一個模塊,用于對各個組件進行組裝
eg:

from keras.models import Sequential
model=Sequential() #初始化模型
model.add(...) #可使用add方法組裝組件

2. layers
包:keras.layers
該模塊主要用于生成神經網絡層,包含多種類型,如Core layers、Convolutional layers等
eg:

from keras.layers import Dense #Dense表示Bp層
model.add(Dense(input_dim=3,output_dim=5)) #加入隱含層

3. Initializations
包:keras.initializations
該模塊主要負責對模型參數(權重)進行初始化,初始化方法包括:uniform、lecun_uniform、normal、orthogonal、zero、glorot_normal、he_normal等
詳細說明:http://keras.io/initializations/
eg:

model.add(Dense(input_dim=3,output_dim=5,init='uniform')) #加入帶初始化(uniform)的隱含層

4. Activations
包:keras.activations、keras.layers.advanced_activations(新激活函數)
該模塊主要負責為神經層附加激活函數,如linear、sigmoid、hard_sigmoid、tanh、softplus、relu、 softplus以及LeakyReLU等比較新的激活函數
詳細說明:http://keras.io/activations/
eg:

model.add(Dense(input_dim=3,output_dim=5,activation='sigmoid')) 加入帶激活函數(sigmoid)的隱含層

Equal to:

model.add(Dense(input_dim=3,output_dim=5)) 
model.add(Activation('sigmoid'))

5. Objectives
包:keras.objectives
該模塊主要負責為神經網絡附加損失函數,即目標函數。如mean_squared_error,mean_absolute_error ,squared_hinge,hinge,binary_crossentropy,categorical_crossentropy等,其中binary_crossentropy,categorical_crossentropy是指logloss
注:目標函數的設定是在模型編譯階段
詳細說明:http://keras.io/objectives/
eg:

 model.compile(loss='binary_crossentropy', optimizer='sgd') #loss是指目標函數

6. Optimizers
包:keras.optimizers
該模塊主要負責設定神經網絡的優化方法,如sgd。
注:優化函數的設定是在模型編譯階段
詳細說明:http://keras.io/optimizers/
eg:

model.compile(loss='binary_crossentropy', optimizer='sgd') #optimizer是指優化方法

7. Preprocessing
包:keras.preprocessing.(imagesequence ext)
數據預處理模塊,不過本人目前尚未用過

8. metrics
包:keras.metrics
與sklearn中metrics包基本相同,主要包含一些如binary_accuracy、mae、mse等的評價方法
eg:

predict=model.predict_classes(test_x) #輸出預測結果
keras.metrics.binary_accuracy(test_y,predict) #計算預測精度

  

  

總結

以上是生活随笔為你收集整理的Keras实现简单BP神经网络的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。