keras Lambda 層

來源:互聯網
上載者:User

標籤:plot   def   file   opp   font   分享   idt   ict   nts   

Lambda層
keras.layers.core.Lambda(function, output_shape=None, mask=None, arguments=None)

本函數用以對上一層的輸出施以任何Theano/TensorFlow運算式

如果你只是想對流經該層的資料做個變換,而這個變換本身沒有什麼需要學習的參數,那麼直接用Lambda Layer是最合適的了。

匯入的方法是 

from keras.layers.core import Lambda

 Lambda函數接受兩個參數,第一個是輸入張量對輸出張量的映射函數,第二個是輸入的shape對輸出的shape的映射函數。

參數
  • function:要實現的函數,該函數僅接受一個變數,即上一層的輸出

  • output_shape:函數應該返回的值的shape,可以是一個tuple,也可以是一個根據輸入shape計算輸出shape的函數

  • mask: 掩膜

  • arguments:可選,字典,用來記錄向函數中傳遞的其他關鍵字參數

例子
# add a x -> x^2 layermodel.add(Lambda(lambda x: x ** 2))# add a layer that returns the concatenation# of the positive part of the input and# the opposite of the negative partdef antirectifier(x):    x -= K.mean(x, axis=1, keepdims=True)    x = K.l2_normalize(x, axis=1)    pos = K.relu(x)    neg = K.relu(-x)    return K.concatenate([pos, neg], axis=1)def antirectifier_output_shape(input_shape):    shape = list(input_shape)    assert len(shape) == 2  # only valid for 2D tensors    shape[-1] *= 2    return tuple(shape)model.add(Lambda(antirectifier,         output_shape=antirectifier_output_shape))
輸入shape

任意,當使用該層作為第一層時,要指定input_shape

輸出shape

由output_shape參數指定的輸出shape,當使用tensorflow時可自動推斷

================================================

keras Lambda自訂層實現資料的切片,Lambda傳參數1、代碼如下:
import numpy as npfrom keras.models import Sequentialfrom keras.layers import Dense, Activation,Reshapefrom keras.layers import mergefrom keras.utils.visualize_util import plotfrom keras.layers import Input, Lambdafrom keras.models import Modeldef slice(x,index):  return x[:,:,index]a = Input(shape=(4,2))x1 = Lambda(slice,output_shape=(4,1),arguments={‘index‘:0})(a)x2 = Lambda(slice,output_shape=(4,1),arguments={‘index‘:1})(a)x1 = Reshape((4,1,1))(x1)x2 = Reshape((4,1,1))(x2)output = merge([x1,x2],mode=‘concat‘)
model = Model(a, output)x_test = np.array([[[1,2],[2,3],[3,4],[4,5]]])print model.predict(x_test)plot(model, to_file=‘lambda.png‘,show_shapes=True)
2、注意Lambda 是可以進行參數傳遞的,傳遞的方式如下代碼所述:
def slice(x,index):    return x[:,:,index]

 如上,index是參數,通過字典將參數傳遞進去.

x1 = Lambda(slice,output_shape=(4,1),arguments={‘index‘:0})(a)x2 = Lambda(slice,output_shape=(4,1),arguments={‘index‘:1})(a)
3、上述代碼實現的是,將矩陣的每一列提取出來,然後單獨進行操作,最後在拼在一起。可視化的圖如下所示。

 

  

 

參考:

53414068

54936185

https://keras-cn.readthedocs.io/en/latest/layers/core_layer/

來自為知筆記(Wiz)



keras Lambda 層

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.