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

标签 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)

最佳答案

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

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/

相关文章:

python - 使用 PyBabel 以编程方式提取消息

python - 如何在不更改对象的情况下缩放 tkinter Canvas 窗口

python - 虚拟环境和嵌入 Python

python - 根据数据集动态设置图层单位

python - Keras 模型在训练时返回较高的验证准确率,但在评估时准确率非常低

python - 在机器人框架中定义包含变量的关键字

javascript - Pyramid json View ajax调用

python - 如何创建自定义模型类

python - 检查目标 : expected dense_1 to have 3 dimensions, 时出错,但得到形状为 (118, 1) 的数组

python - Keras model.predict 不接受大小为一的输入(标量)