首页 > 解决方案 > 在 keras 中制作“非全连接”(单连接?)神经网络

问题描述

我不知道我要查找的名称,但我想在 keras 中创建一个层,其中每个输入都乘以它自己的独立权重和偏差。例如,如果有 10 个输入,则将有 10 个权重和 10 个偏差,每个输入将乘以它的权重并与它的偏差相加得到 10 个输出。

例如这里是一个简单的密集网络:

from keras.layers import Input, Dense
from keras.models import Model
N = 10
input = Input((N,))
output = Dense(N)(input)
model = Model(input, output)
model.summary()

可以看到,这个模型有 110 个参数,因为它是全连接的:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         (None, 10)                0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                110       
=================================================================
Total params: 110
Trainable params: 110
Non-trainable params: 0
_________________________________________________________________

我想output = Dense(N)(input)用类似的东西替换output = SinglyConnected()(input),这样模型现在有 20 个参数:10 个权重和 10 个偏差。

标签: kerasneural-networkdeep-learningkeras-layer

解决方案


创建自定义层:

class SingleConnected(Layer):

    #creator
    def __init__(self, **kwargs):
        super(SingleConnected, self).__init__(**kwargs)

   #creates weights
   def build(self, input_shape):

       weight_shape = (1,) * (len(input_shape) - 1)
       weight_shape = weight_shape + (input_shape[-1]) #(....., input)

       self.kernel = self.add_weight(name='kernel', 
                                  shape=weight_shape,
                                  initializer='uniform',
                                  trainable=True)

       self.bias = self.add_weight(name='bias', 
                                   shape=weight_shape,
                                   initializer='zeros',
                                   trainable=True)

       self.built=True

   #operation:
   def call(self, inputs):
       return (inputs * self.kernel) + self.bias

   #output shape
   def compute_output_shape(self, input_shape):
       return input_shape

   #for saving the model - only necessary if you have parameters in __init__
   def get_config(self):
       config = super(SingleConnected, self).get_config()
       return config

使用图层:

model.add(SingleConnected())

推荐阅读