【问题标题】:What does keras do after definition of loss function?定义损失函数后,keras 做了什么?
【发布时间】:2018-01-13 09:41:21
【问题描述】:
【问题讨论】:
标签:
python
deep-learning
keras
theano
【解决方案1】:
在 Keras 中,在计算损失之后,将样本权重和损失权重应用于损失。请参阅脚本training.py 中的这些行。
-
样品重量:
score_array = K.mean(score_array, axis=list(range(weight_ndim, ndim)))
score_array *= weights
score_array /= K.mean(K.cast(K.not_equal(weights, 0), K.floatx()))
return K.mean(score_array)
-
减肥:
if total_loss is None:
total_loss = loss_weight * output_loss
将这些线映射回计算图很简单。例如,以下块计算K.mean(K.cast(K.not_equal(weights, 0), K.floatx())):
- 第一个节点计算
K.not_equal(weights, 0)
- 第二个节点是
K.cast(..., K.floatx())
- 所有其他节点大约是
K.mean(...)
- 左分支计算批量大小(调用
shape,并从中获取维度0)
- 右分支计算张量和
- 将右分支的输出除以左分支的输出
- 这个块的最终输出是标量
K.mean(K.cast(K.not_equal(weights, 0), K.floatx()))