我创建了一个自定义层,但不确定如何使用和训练该层的偏置权重。
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/