如何在keras中使用偏向自定义图层

时间:2018-03-10 00:36:20

标签: python keras

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

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)

2 个答案:

答案 0 :(得分:1)

与使用常规砝码完全相同....

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

答案 1 :(得分:0)

丹尼尔的回答很好。但是,偏差的形状应写为shape=(self.output_dim,),并在右括号前加上一个逗号,否则您将面临错误TypeError: 'int' object is not iterable

参考(由keras团队实现的密集层):

def build(self, input_shape):
    assert len(input_shape) >= 2
    input_dim = input_shape[-1]

    self.kernel = self.add_weight(shape=(input_dim, self.units),
                                  initializer=self.kernel_initializer,
                                  name='kernel',
                                  regularizer=self.kernel_regularizer,
                                  constraint=self.kernel_constraint)
    if self.use_bias:
        self.bias = self.add_weight(shape=(self.units,),
                                    initializer=self.bias_initializer,
                                    name='bias',
                                    regularizer=self.bias_regularizer,
                                    constraint=self.bias_constraint)
    else:
        self.bias = None
    self.input_spec = InputSpec(min_ndim=2, axes={-1: input_dim})
    self.built = True