AIM*_*BLB 3 deep-learning keras
如何在 Keras 中构建一个层,将输入 x 映射到 x+b 形式的输出,其中 b 是相同维度的可训练权重?(这里的激活函数也是恒等式)。
您始终可以通过扩展类来构建自定义层tf.keras.layers.Layer,这就是我的做法
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()
Run Code Online (Sandbox Code Playgroud)
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
_________________________________________________________________
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3647 次 |
| 最近记录: |