您好,登錄后才能下訂單哦!
這篇文章主要介紹了Keras函數(shù)式API怎么使用的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Keras函數(shù)式API怎么使用文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
定義了用于二分類的多層感知器模型。
模型輸入32維特征,經(jīng)過(guò)三個(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ò)。
該模型接收3通道的64×64圖像作為輸入,然后經(jīng)過(guò)兩個(gè)卷積和池化層的序列作為特征提取器,接著過(guò)一個(gè)全連接層,最后輸出層過(guò)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ù)如下:
定義一個(gè)用于文本序列分類的LSTM網(wǎng)絡(luò)。
該模型需要100個(gè)時(shí)間步長(zhǎng)作為輸入,然后經(jīng)過(guò)一個(gè)Embedding層,每個(gè)時(shí)間步變成128維特征表示,然后經(jīng)過(guò)一個(gè)LSTM層,LSTM輸出過(guò)一個(gè)全連接層,最后輸出用sigmoid激活函數(shù)用于進(jìn)行二分類預(yù)測(cè)。
##------ 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ù)如下:
定義一個(gè)雙向循環(huán)神經(jīng)網(wǎng)絡(luò),可以用來(lái)完成序列標(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ù)如下:
定義了具有不同大小內(nèi)核的多個(gè)卷積層來(lái)解釋圖像輸入。
該模型采用尺寸為64×64像素的3通道圖像。
有兩個(gè)共享此輸入的CNN特征提取子模型; 第一個(gè)內(nèi)核大小為5x5,第二個(gè)內(nèi)核大小為3x3。
把提取的特征展平為向量然后拼接成一個(gè)長(zhǎng)向量,然后過(guò)一個(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ù)如下:
定義一個(gè)共享特征抽取層的模型,這里共享的是LSTM層的輸出,具體共享參見(jiàn)代碼
##------ 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ù)如下:
定義有兩個(gè)輸入的模型,這里測(cè)試的是輸入兩張圖片,一個(gè)輸入是單通道的64x64,另一個(gè)是3通道的32x32,兩個(gè)經(jīng)過(guò)卷積層、池化層后,展平拼接,最后進(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ù)如下:
定義有多個(gè)輸出的模型,以文本序列輸入LSTM網(wǎng)絡(luò)為例,一個(gè)輸出是對(duì)文本的分類,另外一個(gè)輸出是對(duì)文本進(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ù)如下:
關(guān)于“Keras函數(shù)式API怎么使用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Keras函數(shù)式API怎么使用”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。