提交 634951f0 编写于 作者: littletomatodonkey's avatar littletomatodonkey

fix loss

上级 b8a7d186
...@@ -12,7 +12,8 @@ ...@@ -12,7 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import paddle.fluid as fluid import paddle
import paddle.nn.functional as F
__all__ = ['CELoss', 'MixCELoss', 'GoogLeNetLoss', 'JSDivLoss'] __all__ = ['CELoss', 'MixCELoss', 'GoogLeNetLoss', 'JSDivLoss']
...@@ -34,35 +35,37 @@ class Loss(object): ...@@ -34,35 +35,37 @@ class Loss(object):
def _labelsmoothing(self, target): def _labelsmoothing(self, target):
if target.shape[-1] != self._class_dim: if target.shape[-1] != self._class_dim:
one_hot_target = fluid.one_hot(input=target, depth=self._class_dim) one_hot_target = F.one_hot(target, self._class_dim)
else: else:
one_hot_target = target one_hot_target = target
soft_target = fluid.layers.label_smooth( soft_target = F.label_smooth(
label=one_hot_target, epsilon=self._epsilon, dtype="float32") one_hot_target, epsilon=self._epsilon, dtype="float32")
soft_target = fluid.layers.reshape( soft_target = paddle.reshape(soft_target, shape=[-1, self._class_dim])
soft_target, shape=[-1, self._class_dim])
return soft_target return soft_target
def _crossentropy(self, input, target): def _crossentropy(self, input, target):
if self._label_smoothing: if self._label_smoothing:
target = self._labelsmoothing(target) target = self._labelsmoothing(target)
softmax_out = fluid.layers.softmax(input, use_cudnn=False) input = -F.log_softmax(input, axis=-1)
cost = fluid.layers.cross_entropy( log_probs = -F.log_softmax(input, axis=-1)
input=softmax_out, label=target, soft_label=self._label_smoothing) cost = paddle.reduce_sum(target * log_probs, dim=-1)
avg_cost = fluid.layers.mean(cost) else:
# softmax_out = F.softmax(input)
cost = F.cross_entropy(input=input, label=target)
avg_cost = paddle.mean(cost)
return avg_cost return avg_cost
def _kldiv(self, input, target): def _kldiv(self, input, target):
cost = target * fluid.layers.log(target / input) * self._class_dim cost = target * F.log(target / input) * self._class_dim
cost = fluid.layers.sum(cost) cost = paddle.sum(cost)
return cost return cost
def _jsdiv(self, input, target): def _jsdiv(self, input, target):
input = fluid.layers.softmax(input, use_cudnn=False) input = F.softmax(input)
target = fluid.layers.softmax(target, use_cudnn=False) target = F.softmax(target)
cost = self._kldiv(input, target) + self._kldiv(target, input) cost = self._kldiv(input, target) + self._kldiv(target, input)
cost = cost / 2 cost = cost / 2
avg_cost = fluid.layers.mean(cost) avg_cost = paddle.mean(cost)
return avg_cost return avg_cost
def __call__(self, input, target): def __call__(self, input, target):
...@@ -94,7 +97,7 @@ class MixCELoss(Loss): ...@@ -94,7 +97,7 @@ class MixCELoss(Loss):
cost0 = self._crossentropy(input, target0) cost0 = self._crossentropy(input, target0)
cost1 = self._crossentropy(input, target1) cost1 = self._crossentropy(input, target1)
cost = lam * cost0 + (1.0 - lam) * cost1 cost = lam * cost0 + (1.0 - lam) * cost1
avg_cost = fluid.layers.mean(cost) avg_cost = paddle.mean(cost)
return avg_cost return avg_cost
...@@ -111,7 +114,7 @@ class GoogLeNetLoss(Loss): ...@@ -111,7 +114,7 @@ class GoogLeNetLoss(Loss):
cost1 = self._crossentropy(input1, target) cost1 = self._crossentropy(input1, target)
cost2 = self._crossentropy(input2, target) cost2 = self._crossentropy(input2, target)
cost = cost0 + 0.3 * cost1 + 0.3 * cost2 cost = cost0 + 0.3 * cost1 + 0.3 * cost2
avg_cost = fluid.layers.mean(cost) avg_cost = paddle.mean(cost)
return avg_cost return avg_cost
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册