在 NN 中指定连接(在 keras 中)
Specify connections in NN (in keras)
我正在使用 keras 和 tensorflow 1.4。
我想明确指定哪些神经元连接在两层之间。因此,只要第一层中的神经元 i 连接到第二层中的神经元 j 并在其他地方为零,我就有一个矩阵 A,其中包含 1。
我的第一个尝试是创建一个带有内核的自定义层,其大小与 A 相同,其中包含不可训练的零,其中 A 中有零和可训练的权重,其中 A 中有一个。然后,所需的输出将是一个简单的点积。不幸的是,我没有设法弄清楚如何实现一个部分可训练和部分不可训练的内核。
有什么建议吗?
(构建一个包含大量手动连接的神经元的功能模型可能是一种解决方法,但不知何故 'ugly' 解决方案)
如果这个矩阵的形状正确,我能想到的最简单的方法是派生 Dense 层并简单地在代码中添加乘以原始权重的矩阵:
class CustomConnected(Dense):
def __init__(self,units,connections,**kwargs):
#this is matrix A
self.connections = connections
#initalize the original Dense with all the usual arguments
super(CustomConnected,self).__init__(units,**kwargs)
def call(self,inputs):
#change the kernel before calling the original call:
self.kernel = self.kernel * self.connections
#call the original calculations:
super(CustomConnected,self).call(inputs)
使用:
model.add(CustomConnected(units,matrixA))
model.add(CustomConnected(hidden_dim2, matrixB,activation='tanh')) #can use all the other named parameters...
请注意,所有 neurons/units 都在末尾添加了偏差。如果您不希望出现偏见,参数 use_bias=False
仍然有效。例如,您也可以使用矢量 B 做完全相同的事情,并用 self.biases = self.biases * vectorB
掩盖原始偏差
测试提示:使用不同的输入和输出维度,这样您就可以确定矩阵 A 具有正确的形状。
我刚刚意识到我的代码可能存在错误,因为我正在更改原始 Dense 层使用的 属性。如果出现奇怪的行为或消息,您可以尝试另一种调用方式:
def call(self, inputs):
output = K.dot(inputs, self.kernel * self.connections)
if self.use_bias:
output = K.bias_add(output, self.bias)
if self.activation is not None:
output = self.activation(output)
return output
其中 K
来自 import keras.backend as K
。
如果您想查看被矩阵掩盖的权重,您还可以进一步设置自定义 get_weights()
方法。 (这在上面的第一种方法中不是必需的)
我正在使用 keras 和 tensorflow 1.4。
我想明确指定哪些神经元连接在两层之间。因此,只要第一层中的神经元 i 连接到第二层中的神经元 j 并在其他地方为零,我就有一个矩阵 A,其中包含 1。
我的第一个尝试是创建一个带有内核的自定义层,其大小与 A 相同,其中包含不可训练的零,其中 A 中有零和可训练的权重,其中 A 中有一个。然后,所需的输出将是一个简单的点积。不幸的是,我没有设法弄清楚如何实现一个部分可训练和部分不可训练的内核。
有什么建议吗?
(构建一个包含大量手动连接的神经元的功能模型可能是一种解决方法,但不知何故 'ugly' 解决方案)
如果这个矩阵的形状正确,我能想到的最简单的方法是派生 Dense 层并简单地在代码中添加乘以原始权重的矩阵:
class CustomConnected(Dense):
def __init__(self,units,connections,**kwargs):
#this is matrix A
self.connections = connections
#initalize the original Dense with all the usual arguments
super(CustomConnected,self).__init__(units,**kwargs)
def call(self,inputs):
#change the kernel before calling the original call:
self.kernel = self.kernel * self.connections
#call the original calculations:
super(CustomConnected,self).call(inputs)
使用:
model.add(CustomConnected(units,matrixA))
model.add(CustomConnected(hidden_dim2, matrixB,activation='tanh')) #can use all the other named parameters...
请注意,所有 neurons/units 都在末尾添加了偏差。如果您不希望出现偏见,参数 use_bias=False
仍然有效。例如,您也可以使用矢量 B 做完全相同的事情,并用 self.biases = self.biases * vectorB
测试提示:使用不同的输入和输出维度,这样您就可以确定矩阵 A 具有正确的形状。
我刚刚意识到我的代码可能存在错误,因为我正在更改原始 Dense 层使用的 属性。如果出现奇怪的行为或消息,您可以尝试另一种调用方式:
def call(self, inputs):
output = K.dot(inputs, self.kernel * self.connections)
if self.use_bias:
output = K.bias_add(output, self.bias)
if self.activation is not None:
output = self.activation(output)
return output
其中 K
来自 import keras.backend as K
。
如果您想查看被矩阵掩盖的权重,您还可以进一步设置自定义 get_weights()
方法。 (这在上面的第一种方法中不是必需的)