欢迎关注公众号:python科技园,一起学习python。
官网介绍:Lambda
keras.layers.Lambda(function, output_shape=None, mask=None, arguments=None)
将任意表达式封装为 Layer
对象。
例如:
# 添加一个 x -> x^2 层
model.add(Lambda(lambda x: x ** 2))
# 添加一个网络层,返回输入的正数部分
# 与负数部分的反面的连接
def 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))
参数
- function: 需要封装的函数。 将输入张量作为第一个参数。
- output_shape: 预期的函数输出尺寸。 只在使用 Theano 时有意义。 可以是元组或者函数。 如果是元组,它只指定第一个维度; 样本维度假设与输入相同:
output_shape = (input_shape[0], ) + output_shape
或者,输入是None
且样本维度也是None
:output_shape = (None, ) + output_shape
如果是函数,它指定整个尺寸为输入尺寸的一个函数:output_shape = f(input_shape)
- arguments: 可选的需要传递给函数的关键字参数。
输入尺寸
任意。当使用此层作为模型中的第一层时, 使用参数 input_shape
(整数元组,不包括样本数的轴)。
输出尺寸
由 output_shape
参数指定 (或者在使用 TensorFlow 时,自动推理得到)。
from keras.layers import Lambda
from keras.models import Input, Model
import numpy as np
import keras.backend as K
a = Input(shape=(2, ))
b = Input(shape=(2, ))
def minus(inputs):
x, y = inputs
return K.mean(x-y, axis=1)
minus_layer = Lambda(minus, name='minus')([a, b])
model = Model(inputs=[a, b], outputs=[minus_layer])
print(model.summary())
# To test
v0 = np.array([5, 2])
v1 = np.array([8, 4])
v2 = np.array([3, 2])
print(model.predict([v0.reshape(1, 2), v1.reshape(1, 2)]))
print(model.predict([v0.reshape(1, 2), v2.reshape(1, 2)]))
print(model.predict([np.array([v0, v0]), np.array([v1, v2])]))
示例2:keras Lambda自定义层实现数据的切片,Lambda传参数
(1)代码如下:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation,Reshape
from keras.layers import merge
from keras.utils.visualize_util import plot
from keras.layers import Input, Lambda
from keras.models import Model
def 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)上述代码实现的是,将矩阵的每一列提取出来,然后单独进行操作,最后在拼在一起。可视化的图如下所示。
参考:
1. lambda官网