在keras中制作一个"non-fully connected"(单连接?)神经网络
Make a "non-fully connected" (singly connected?) neural network in 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 = SinglyConnected()(input)
之类的东西替换 output = Dense(N)(input)
,这样模型现在有 20 个参数:10 个权重和 10 个偏差。
创建自定义图层:
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())
我不知道我要找的东西的名称,但我想在 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 = SinglyConnected()(input)
之类的东西替换 output = Dense(N)(input)
,这样模型现在有 20 个参数:10 个权重和 10 个偏差。
创建自定义图层:
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())