Keras中仅偏向图层 [英] Bias only Layer in Keras

查看:54
本文介绍了Keras中仅偏向图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Keras中构建一层将输入x映射为x + b形式的输出,其中b是相同尺寸的可训练权重?(这里的激活功能也就是身份).

How could one build a layer in Keras which maps an input x to an output of the form x+b where b is a trainable weight of the same dimension? (Also the activation function here would be the identity).

推荐答案

您始终可以通过扩展 tf.keras.layers.Layer 类来构建自定义图层,这就是我要做的

You can always build a custom layer by extending tf.keras.layers.Layer class, here is how I'd do it

import tensorflow as tf
print('TensorFlow:', tf.__version__)

class BiasLayer(tf.keras.layers.Layer):
    def __init__(self, *args, **kwargs):
        super(BiasLayer, self).__init__(*args, **kwargs)

    def build(self, input_shape):
        self.bias = self.add_weight('bias',
                                    shape=input_shape[1:],
                                    initializer='zeros',
                                    trainable=True)
    def call(self, x):
        return x + self.bias

input_layer = tf.keras.Input(shape=[5])
x  = BiasLayer()(input_layer)
model = tf.keras.Model(inputs=[input_layer], outputs=[x])

model.summary()

TensorFlow: 2.1.0
Model: "model_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_7 (InputLayer)         [(None, 5)]               0         
_________________________________________________________________
bias_layer_3 (BiasLayer)     (None, 5)                 5         
=================================================================
Total params: 5
Trainable params: 5
Non-trainable params: 0
_________________________________________________________________

这篇关于Keras中仅偏向图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆