Lambda layer
Keras.layers.core.Lambda (function, Output_shape=none, Mask=none, Arguments=none)
This function is used to apply any theano/tensorflow expression to the output of the previous layer
If you just want to make a change to the data that flows through the layer, and the transformation itself has no parameters to learn, then it is most appropriate to use a lambda layer directly.
The Import method is
from Import Lambda
The lambda function accepts two parameters, the first one is the mapping function of the input tensor to the output tensor, and the second is the mapping function of the shape to the output shape of the input.
Parameters
Functions: A function to be implemented that accepts only one variable, that is, the output of the previous layer
Output_shape: The shape of the value that the function should return, can be a tuple, or a function that calculates the output shape from the input shape
Mask: Masking film
Arguments: optional, dictionary, used to record additional keyword arguments passed to a function
Example
#Add a X-x^2 layerModel.add (Lambda (LambdaX:X * * 2))#add a layer that returns the concatenation# of the positive part of the the input and#The opposite of the negative partdefantirectifier (x): x-= K.mean (x, Axis=1, keepdims=True) x= K.l2_normalize (x, Axis=1) Pos=k.relu (x) Neg= K.relu (-x)returnK.concatenate ([Pos, neg], Axis=1)defAntirectifier_output_shape (input_shape): Shape=list (input_shape)assertLen (shape) = = 2#Only valid for 2D tensorsShape[-1] *= 2returntuple (Shape) model.add (Lambda (antirectifier, Output_shape=antirectifier_output_shape))
Enter shape
Arbitrarily, when using this layer as the first layer, specify the input_shape
Output shape
The output shape specified by the output_shape parameter, which can be inferred automatically when using TensorFlow
================================================
Keras Lambda Custom layer implementation of data slicing, lambda parameter 1, the code is as follows:
ImportNumPy as NP fromKeras.modelsImportSequential fromKeras.layersImportDense, Activation,reshape fromKeras.layersImportMerge fromKeras.utils.visualize_utilImportplot fromKeras.layersImportInput, Lambda fromKeras.modelsImportModeldefSlice (x,index):returnX[:,:,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]]])Printmodel.predict (x_test) plot (model, To_file='Lambda.png', show_shapes=true)
2. Note that lambda can be passed as a parameter, as described in the following code:
def Slice (x,index): return X[:,:,index]
as above, index is the parameter, passing in the argument through the dictionary.
X1 = Lambda (slice,output_shape= (4,1), arguments={'index'= lambda (slice,output_ Shape= (4,1), arguments={'index': 1}) (a)
3, the above code is implemented, each column of the matrix is extracted, and then operate separately, and finally in a spell together. The visualization diagram is shown below.
Reference:
53414068
54936185
https://keras-cn.readthedocs.io/en/latest/layers/core_layer/
From for notes (Wiz)
Keras LAMBDA Layer