编写keras层,keras权重共享层

1. Lambda:将均值为0的输入数据的正与负部分级联。Lambda用来编写不需要训练的层。

 

import keras.backend as K
from keras.layers.core import Lambda
from keras.models import Sequential
import numpy as np

np.random.seed(19931221)

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)

input_data=np.random.randint(0,10,(3,2))
x1 = input_data-np.mean(input_data, axis=1, keepdims=True)
model = Sequential()
model.add(Lambda(antirectifier, output_shape=antirectifier_output_shape,input_shape=(2,)))
res=model.predict(input_data)

当Lambda作为第一层时,要指定input_shape

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值