gpt4 book ai didi

python - 如何在keras中使用自定义层的偏差

转载 作者:太空宇宙 更新时间:2023-11-03 14:01:39 24 4
gpt4 key购买 nike

我创建了一个自定义层,但不确定如何使用和训练该层的偏置权重。

from keras import backend as K
from keras.engine.topology import Layer
import numpy as np
import tensorflow as tf

class MyLayer(Layer):

def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
super(MyLayer, self).__init__(**kwargs)

def build(self, input_shape):
self.kernel = self.add_weight(name='kernel',
shape=(input_shape[1], self.output_dim),
initializer='he_normal',
trainable=True)
super(MyLayer, self).build(input_shape)

def call(self, x):
ep = K.constant(value= 10e-5, shape=(1,513))
v = K.dot(x, self.kernel)
out = K.switch((v - ep)>0, v, - (ep)/(v-1-ep))
return out


def compute_output_shape(self, input_shape):
return (input_shape[0], self.output_dim)

最佳答案

与您使用常规重量的方式完全相同......

def build(self, input_shape):
self.kernel = self.add_weight(name='kernel',
shape=(input_shape[1], self.output_dim),
initializer='he_normal',
trainable=True)
self.bias = self.add_weight(name='bias',
shape=(self.output_dim),
initializer='zeros',
trainable=True)
super(MyLayer, self).build(input_shape)

def call(self, x):
ep = K.constant(value= 10e-5, shape=(1,513))
v = K.dot(x, self.kernel) #maybe add bias here?
out = K.switch((v - ep)>0, v, - (ep)/(v-1-ep))
return out + self.bias

关于python - 如何在keras中使用自定义层的偏差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49204156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com