首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Keras:用于多个多类分类的自定义输出层

Keras:用于多个多类分类的自定义输出层
EN

Data Science用户
提问于 2021-09-27 09:42:56
回答 1查看 176关注 0票数 2

你好,我对机器学习非常陌生,我想用Python在Keras中构建我的第一个自定义层。我想使用103个维度的数据集来完成分类任务。模型的最后一个完全连接层有103个神经元(由图像中的13个点表示)。前一层的五个维度组应连接到输出层的三个神经元,因此将有20种分类。输出层的神经元代表“真”(图像中的“T”),“淡漠”("?")和"False“(F)。其余三个不需要连接到输出层。

如何构建这一层?我怎样才能确定,有三个神经元的20组中的每一组的概率加起来都是1?例如,我可以将softmax激活函数应用于每个组吗?

编辑-这是我的解决方案:

代码语言:javascript
复制
# 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_end
EN

回答 1

Data Science用户

回答已采纳

发布于 2021-09-27 10:46:00

功能API允许您设计更复杂的模型,包括多输出模型。查看文档,了解如何将特定的神经元连接到您选择的其他神经元。您应该能够从头开始制作自定义层。一旦构建了不同的输出层,就可以像往常一样使用softmax激活来设置每个层中的概率。

票数 2
EN
页面原文内容由Data Science提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://datascience.stackexchange.com/questions/102527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档