
你好,我对机器学习非常陌生,我想用Python在Keras中构建我的第一个自定义层。我想使用103个维度的数据集来完成分类任务。模型的最后一个完全连接层有103个神经元(由图像中的13个点表示)。前一层的五个维度组应连接到输出层的三个神经元,因此将有20种分类。输出层的神经元代表“真”(图像中的“T”),“淡漠”("?")和"False“(F)。其余三个不需要连接到输出层。
如何构建这一层?我怎样才能确定,有三个神经元的20组中的每一组的概率加起来都是1?例如,我可以将softmax激活函数应用于每个组吗?
编辑-这是我的解决方案:
# define input and hidden layers. append them to list by calling the new layer with the last layer in the list
self.layers: list = [keras.layers.Input(shape=self.neurons)]
[self.layers.append(keras.layers.Dense(self.neurons, activation=self.activation_hidden_layers)(self.layers[-1])) for _ in range(num_hidden_layers)]
self.layers.append(keras.layers.Dense(self.neurons - self.dims_to_leave_out, activation=activation_hidden_layers)(self.layers[-1]))
# define multi-output layer by slicing the neurons from the last hidden layer
self.outputs: list = []
index_start: int = 0
for i in range(int((self.neurons - self.dims_to_leave_out)/self.neurons_per_output_layer)):
index_end: int = index_start + self.neurons_per_output_layer
self.outputs.append(keras.layers.Dense(self.output_dims_per_output_layer, activation=self.activation_output_layers)(self.layers[-1][:, index_start:index_end]))
index_start = index_endhttps://datascience.stackexchange.com/questions/102527
复制相似问题