Keras函數(shù)式(functional)API的使用方式
多層感知器(Multilayer Perceptron)
定義了用于二分類的多層感知器模型。
模型輸入32維特征,經(jīng)過三個(gè)全連接層,每層使用relu線性激活函數(shù),并且在輸出層中使用sigmoid激活函數(shù),最后用于二分類。
##------ Multilayer Perceptron ------## from keras.models import Model from keras.layers import Input, Dense from keras import backend as K K.clear_session() # MLP model x = Input(shape=(32,)) hidden1 = Dense(10, activation='relu')(x) hidden2 = Dense(20, activation='relu')(hidden1) hidden3 = Dense(10, activation='relu')(hidden2) output = Dense(1, activation='sigmoid')(hidden3) model = Model(inputs=x, outputs=output) # summarize layers model.summary()
模型的結(jié)構(gòu)和參數(shù)如下:
卷積神經(jīng)網(wǎng)絡(luò)(Convolutional Neural Network)
定義用于圖像分類的卷積神經(jīng)網(wǎng)絡(luò)。
該模型接收3通道的64×64圖像作為輸入,然后經(jīng)過兩個(gè)卷積和池化層的序列作為特征提取器,接著過一個(gè)全連接層,最后輸出層過softmax激活函數(shù)進(jìn)行10個(gè)類別的分類。
##------ Convolutional Neural Network ------## from keras.models import Model from keras.layers import Input from keras.layers import Dense, Flatten from keras.layers import Conv2D, MaxPooling2D from keras import backend as K K.clear_session() # CNN model x = Input(shape=(64,64,3)) conv1 = Conv2D(16, (5,5), activation='relu')(x) pool1 = MaxPooling2D((2,2))(conv1) conv2 = Conv2D(32, (3,3), activation='relu')(pool1) pool2 = MaxPooling2D((2,2))(conv2) conv3 = Conv2D(32, (3,3), activation='relu')(pool2) pool3 = MaxPooling2D((2,2))(conv3) flat = Flatten()(pool3) hidden1 = Dense(512, activation='relu')(flat) output = Dense(10, activation='softmax')(hidden1) model = Model(inputs=x, outputs=output) # summarize layers model.summary()
模型的結(jié)構(gòu)和參數(shù)如下:
循環(huán)神經(jīng)網(wǎng)絡(luò)(Recurrent Neural Network)
定義一個(gè)用于文本序列分類的LSTM網(wǎng)絡(luò)。
該模型需要100個(gè)時(shí)間步長作為輸入,然后經(jīng)過一個(gè)Embedding層,每個(gè)時(shí)間步變成128維特征表示,然后經(jīng)過一個(gè)LSTM層,LSTM輸出過一個(gè)全連接層,最后輸出用sigmoid激活函數(shù)用于進(jìn)行二分類預(yù)測。
##------ Recurrent Neural Network ------## from keras.models import Model from keras.layers import Input from keras.layers import Dense, LSTM, Embedding from keras import backend as K K.clear_session() VOCAB_SIZE = 10000 EMBED_DIM = 128 x = Input(shape=(100,), dtype='int32') embedding = Embedding(VOCAB_SIZE, EMBED_DIM, mask_zero=True)(x) hidden1 = LSTM(64)(embedding) hidden2 = Dense(32, activation='relu')(hidden1) output = Dense(1, activation='sigmoid')(hidden2) model = Model(inputs=x, outputs=output) # summarize layers model.summary()
模型的結(jié)構(gòu)和參數(shù)如下:
Bidirectional recurrent neural network
定義一個(gè)雙向循環(huán)神經(jīng)網(wǎng)絡(luò),可以用來完成序列標(biāo)注等任務(wù),相比上面的LSTM網(wǎng)絡(luò),多了一個(gè)反向的LSTM,其它設(shè)置一樣。
##------ Bidirectional recurrent neural network ------## from keras.models import Model from keras.layers import Input, Embedding from keras.layers import Dense, LSTM, Bidirectional from keras import backend as K K.clear_session() VOCAB_SIZE = 10000 EMBED_DIM = 128 HIDDEN_SIZE = 64 # input layer x = Input(shape=(100,), dtype='int32') # embedding layer embedding = Embedding(VOCAB_SIZE, EMBED_DIM, mask_zero=True)(x) # BiLSTM layer hidden = Bidirectional(LSTM(HIDDEN_SIZE, return_sequences=True))(embedding) # prediction layer output = Dense(10, activation='softmax')(hidden) model = Model(inputs=x, outputs=output) model.summary()
模型的結(jié)構(gòu)和參數(shù)如下:
共享輸入層模型(Shared Input Layer Model)
定義了具有不同大小內(nèi)核的多個(gè)卷積層來解釋圖像輸入。
該模型采用尺寸為64×64像素的3通道圖像。
有兩個(gè)共享此輸入的CNN特征提取子模型; 第一個(gè)內(nèi)核大小為5x5,第二個(gè)內(nèi)核大小為3x3。
把提取的特征展平為向量然后拼接成一個(gè)長向量,然后過一個(gè)全連接層,最后輸出層完成10分類。
##------ Shared Input Layer Model ------## from keras.models import Model from keras.layers import Input from keras.layers import Dense, Flatten from keras.layers import Conv2D, MaxPooling2D, Concatenate from keras import backend as K K.clear_session() # input layer x = Input(shape=(64,64,3)) # first feature extractor conv1 = Conv2D(32, (3,3), activation='relu')(x) pool1 = MaxPooling2D((2,2))(conv1) flat1 = Flatten()(pool1) # second feature extractor conv2 = Conv2D(16, (5,5), activation='relu')(x) pool2 = MaxPooling2D((2,2))(conv2) flat2 = Flatten()(pool2) # merge feature merge = Concatenate()([flat1, flat2]) # interpretation layer hidden1 = Dense(128, activation='relu')(merge) # prediction layer output = Dense(10, activation='softmax')(merge) model = Model(inputs=x, outputs=output) model.summary()
模型的結(jié)構(gòu)和參數(shù)如下:
Shared Feature Extraction Layer
定義一個(gè)共享特征抽取層的模型,這里共享的是LSTM層的輸出,具體共享參見代碼
##------ Shared Feature Extraction Layer ------## from keras.models import Model from keras.layers import Input, Embedding from keras.layers import Dense, LSTM, Concatenate from keras import backend as K K.clear_session() # input layer x = Input(shape=(100,32)) # feature extraction extract1 = LSTM(64)(x) # first interpretation model interp1 = Dense(32, activation='relu')(extract1) # second interpretation model interp11 = Dense(64, activation='relu')(extract1) interp12 = Dense(32, activation='relu')(interp11) # merge interpretation merge = Concatenate()([interp1, interp12]) # output layer output = Dense(10, activation='softmax')(merge) model = Model(inputs=x, outputs=output) model.summary()
模型的結(jié)構(gòu)和參數(shù)如下:
多輸入模型(Multiple Input Model)
定義有兩個(gè)輸入的模型,這里測試的是輸入兩張圖片,一個(gè)輸入是單通道的64x64,另一個(gè)是3通道的32x32,兩個(gè)經(jīng)過卷積層、池化層后,展平拼接,最后進(jìn)行二分類。
##------ Multiple Input Model ------## from keras.models import Model from keras.layers import Input from keras.layers import Dense, Flatten from keras.layers import Conv2D, MaxPooling2D, Concatenate from keras import backend as K K.clear_session() # first input model input1 = Input(shape=(64,64,1)) conv11 = Conv2D(32, (5,5), activation='relu')(input1) pool11 = MaxPooling2D(pool_size=(2,2))(conv11) conv12 = Conv2D(16, (3,3), activation='relu')(pool11) pool12 = MaxPooling2D(pool_size=(2,2))(conv12) flat1 = Flatten()(pool12) # second input model input2 = Input(shape=(32,32,3)) conv21 = Conv2D(32, (5,5), activation='relu')(input2) pool21 = MaxPooling2D(pool_size=(2,2))(conv21) conv22 = Conv2D(16, (3,3), activation='relu')(pool21) pool22 = MaxPooling2D(pool_size=(2,2))(conv22) flat2 = Flatten()(pool22) # merge input models merge = Concatenate()([flat1, flat2]) # interpretation model hidden1 = Dense(20, activation='relu')(merge) output = Dense(1, activation='sigmoid')(hidden1) model = Model(inputs=[input1, input2], outputs=output) model.summary()
模型的結(jié)構(gòu)和參數(shù)如下:
多輸出模型(Multiple Output Model)
定義有多個(gè)輸出的模型,以文本序列輸入LSTM網(wǎng)絡(luò)為例,一個(gè)輸出是對文本的分類,另外一個(gè)輸出是對文本進(jìn)行序列標(biāo)注。
##------ Multiple Output Model ------ ## from keras.models import Model from keras.layers import Input from keras.layers import Dense, Flatten, TimeDistributed, LSTM from keras.layers import Conv2D, MaxPooling2D, Concatenate from keras import backend as K K.clear_session() x = Input(shape=(100,1)) extract = LSTM(10, return_sequences=True)(x) class11 = LSTM(10)(extract) class12 = Dense(10, activation='relu')(class11) output1 = Dense(1, activation='sigmoid')(class12) output2 = TimeDistributed(Dense(1, activation='linear'))(extract) model = Model(inputs=x, outputs=[output1, output2]) model.summary()
模型的結(jié)構(gòu)和參數(shù)如下:
參考
[1] https://machinelearningmastery.com/keras-functional-api-deep-learning/
[2] https://keras.io/getting-started/functional-api-guide/
[3] https://tensorflow.google.cn/alpha/guide/keras/functional
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
對python mayavi三維繪圖的實(shí)現(xiàn)詳解
今天小編就為大家分享一篇對python mayavi三維繪圖的實(shí)現(xiàn)詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python報(bào)錯:TypeError:?‘xxx‘?object?is?not?subscriptable解決
這篇文章主要給大家介紹了關(guān)于Python報(bào)錯:TypeError:?‘xxx‘?object?is?not?subscriptable的解決辦法,TypeError是Python中的一種錯誤,表示操作或函數(shù)應(yīng)用于不合適類型的對象時(shí)發(fā)生,文中將解決辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08Pytorch上下采樣函數(shù)--interpolate用法
這篇文章主要介紹了Pytorch上下采樣函數(shù)--interpolate用法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法
這篇文章主要介紹了python根據(jù)開頭和結(jié)尾字符串獲取中間字符串的方法,涉及Python操作字符串截取的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03TENSORFLOW變量作用域(VARIABLE SCOPE)
這篇文章主要介紹了TENSORFLOW變量作用域(VARIABLE SCOPE),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Python爬蟲獲取數(shù)據(jù)保存到數(shù)據(jù)庫中的超詳細(xì)教程(一看就會)
使用爬蟲爬數(shù)據(jù),總要涉及到數(shù)據(jù)持久化,也就是數(shù)據(jù)存儲的問題,下面這篇文章主要給大家介紹了關(guān)于Python爬蟲獲取數(shù)據(jù)保存到數(shù)據(jù)庫中的超詳細(xì)教程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06python 中 .py文件 轉(zhuǎn) .pyd文件的操作
這篇文章主要介紹了python 中 .py文件 轉(zhuǎn) .pyd文件的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03