欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

keras 使用Lambda 快速新建層 添加多個(gè)參數(shù)操作

 更新時(shí)間:2020年06月10日 10:11:55   作者:青盞  
這篇文章主要介紹了keras 使用Lambda 快速新建層 添加多個(gè)參數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

keras許多簡(jiǎn)單操作,都需要新建一個(gè)層,使用Lambda可以很好完成需求。

# 額外參數(shù)
def normal_reshape(x, shape):
 return K.reshape(x,shape)
 
output = Lambda(normal_reshape, arguments={'shape':(-1, image_seq, 1000)})(output)
output = Lambda(lambda inp: K.mean(inp, axis=1), output_shape=(1000,))(output)

更多參考

補(bǔ)充知識(shí):keras 實(shí)現(xiàn)包括batch size所在維度的reshape,使用backend新建一層 針對(duì)多輸入使用不同batch size折衷解決辦法

新建層,可以在此層內(nèi)使用backend完成想要的功能,如包含batch size維度在內(nèi)的reshpe:

def backend_reshape(x): return backend.reshape(x, (-1, 5, 256))

使用lambda方法調(diào)用層:

vision_model.add(Lambda(backend_reshape, output_shape=(5, 256)))

注意指定輸出維度

在多輸入問(wèn)題中,有時(shí)兩個(gè)輸入具有不同的batch size,但在keras無(wú)法直接實(shí)現(xiàn)。我所遇到的問(wèn)題是,我有兩個(gè)輸入分別是圖像輸入和問(wèn)題輸入,對(duì)于圖像輸入每個(gè)樣本是一個(gè)圖像序列。這就要求我們?cè)诎褕D像序列輸入到CNN中時(shí)是一張一張圖像。

我的解決辦法是在輸入是把圖像序列作為一個(gè)樣本,等輸入進(jìn)去后,通過(guò)上述的reshape方法將圖像序列重新拆分成一張張圖像輸入到CNN,然后在后期處理時(shí)重新reshape成一個(gè)序列樣本。

代碼:

image_seq = 4
def preprocess_reshape(x):
 return K.reshape(x, (-1, 224, 224,3))
 
def backend_reshape(x):
 return K.reshape(x, (-1, image_seq, 256))
image_input = Input(shape=(image_seq, 224, 224, 3) , name='input_img')
image_re = Lambda(preprocess_reshape, output_shape=(224,224,3))(image_input)
im_pre = Lambda(preprocess_input, name='preprocessing')(image_re)

vision_model.add(Lambda(backend_reshape, output_shape=(image_seq, 256))) vision_model.add(LSTM(256, kernel_regularizer=l2, recurrent_regularizer=l2))

以上這篇keras 使用Lambda 快速新建層 添加多個(gè)參數(shù)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論