python - 在 Tensorflow 和 Keras 的两个通道上生成 softmax
问题描述
我的网络倒数第二层的形状(U, C)
是C
通道数。我想分别在每个通道上应用 softmax 函数。
例如,如果U=2
和C=3
,并且层产生[ [1 2 3], [10 20 30] ]
,我希望输出softmax(1, 2, 3)
对通道 0 和softmax(10, 20, 30)
通道 1 执行。
有没有办法用 Keras 做到这一点?我使用 TensorFlow 作为后端。
更新
还请解释如何确保损失是两个交叉熵的总和,以及我如何验证这一点?(也就是说,我不希望优化器只针对其中一个 softmax 的损失进行训练,而是针对每个的交叉熵损失的总和进行训练)。该模型使用 Keras 内置categorical_crossentropy
的损失函数。
解决方案
对多个输出使用功能 api。https://keras.io/getting-started/functional-api-guide/
input = Input(...)
...
t = some_tensor
t0 = t0[:,:,0]
t1 = t0[:,:,1]
soft0 = Softmax(output_shape)(t0)
soft1 = Softmax(output_shape)(t1)
outputs = [soft0,soft1]
model = Model(inputs=input, outputs=outputs)
model.compile(...)
model.fit(x_train, [y_train0, ytrain1], epoch = 10, batch_size=32)
推荐阅读
- c# - 在 C# 中,有没有办法实现抽象类,以便使用提供的第一个参数自动调用基本构造函数?
- javascript - 如何在不手动输入 javascript 中的每个图像文件的情况下从文件夹中循环图像?
- python - 碰撞仅在精灵在 x 轴上移动时按预期工作
- python - 如何使用带有 input() 函数的多处理并避免“EOFError: EOF when reading a line”?
- go - 什么是正确的
- karate - 空手道 UI:如何单击具有相同类名的特定复选框
- javascript - 使用 ajax 从后端检索 HTML,然后使用 jquery 切片不起作用
- mysql - 案例陈述和加入
- reactjs - 无法使用 nginx POST 到 /api 位置
- python - 如果它不存在,则向 multiIndex 添加一个值 pandas