[英]How to implement a neural network with a not-fully-connected layer as the final layer?
I would like to implement a neural.network with an input layer, two dense hidden layer and a non-dense output layer.我想实现一个带有输入层、两个密集隐藏层和一个非密集 output 层的神经网络。 A toy example is shown in the figure below.
下图显示了一个玩具示例。 The first hidden layer has three neurons, the second two and the final four neurons but between the second and third there are only four connections.
第一个隐藏层有三个神经元,第二个和最后四个神经元,但第二个和第三个之间只有四个连接。
I would like to use Keras functional API. How can I implement it?我想使用 Keras functional API。我该如何实现呢? Should I set the missing weight manually to 0?
我应该手动将丢失的重量设置为 0 吗? I would start as follows:
我会开始如下:
input=keras.layers.Input(...)
hidden1=keras.layers.Dense(3, activation="..")(input)
hidden2=keras.layers.Dense(3, activation="..")(hidden1)
but then I do not know how to proceed.但后来我不知道如何进行。
The final layer is actually two separate Dense
layers, each with 2 neurons and connected to a different neuron of previous layer.最后一层实际上是两个独立的
Dense
层,每个层有 2 个神经元并连接到前一层的不同神经元。 Therefore, you can simply separate the neurons of second-to-last layer and pass it to two different layers:因此,您可以简单地将倒数第二层的神经元分开,并将其传递给两个不同的层:
input = keras.layers.Input(shape=(3,))
hidden1 = keras.layers.Dense(3)(input)
hidden2 = keras.layers.Dense(2)(hidden1)
hidden2_n1 = keras.layers.Lambda(lambda x: x[:,0:1])(hidden2) # take the first neuron
hidden2_n2 = keras.layers.Lambda(lambda x: x[:,1:])(hidden2) # take the second neuron
output1 = keras.layers.Dense(2)(hidden2_n1)
output2 = keras.layers.Dense(2)(hidden2_n2)
output = keras.layers.concatenate([output1, output2]) # optional: concatenate the layers to have a single output layer
model = keras.models.Model(input, output)
In tf.keras
or newer versions of keras
, instead of using Lambda
layers you could simply write:在
tf.keras
或更新版本的keras
中Lambda
您可以简单地编写:
output1 = keras.layers.Dense(2)(hidden2[:,0:1])
output2 = keras.layers.Dense(2)(hidden2[:,1:])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.