forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Relay, TOPI] Add negative log likelihood loss (nll_loss) op (apache#…
…8056) * add nll_loss * enrich the doc and rename parameters * update upon review * add tests * update based on reviews * update upon reviews * update upon reviews
- Loading branch information
Showing
15 changed files
with
482 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint: disable=invalid-name,unused-argument | ||
"""Loss functions definitions.""" | ||
from __future__ import absolute_import | ||
from . import cpp | ||
|
||
|
||
def nll_loss(predictions, targets, weights, reduction, ignore_index): | ||
"""Negative log likelihood loss on the input data. | ||
output{n, i_1, i_2, ..., i_k} = -p * w | ||
where t = target{n, i_1, i_2, ..., i_k} | ||
p = predictions{n, t, i_1, i_2, i_k} | ||
w = weights{n, i_1, i_2, ..., i_k} if t != ignore_index else 0 | ||
result = reduction(output) | ||
Parameters | ||
---------- | ||
predictions : tvm.te.Tensor | ||
(k+2)-D with shape (N, C, d_1, d_2, ..., d_k), | ||
where C is the number of target classes | ||
targets : tvm.te.Tensor | ||
(k+1)-D with shape (N, d_1, d_2, ..., d_k) | ||
The target value of the input. | ||
weights : tvm.te.Tensor | ||
1-D with shape (C,) | ||
The weight of each target value. | ||
reduction : string | ||
The reduction method to apply to output. | ||
Can be "mean", "sum" or "none". | ||
ignore_index : int | ||
The target value to ignore. | ||
Returns | ||
------- | ||
output : tvm.te.Tensor | ||
a scalar if the reduction type is "mean" or "sum", | ||
otherwise the same shape as `target`. | ||
""" | ||
return cpp.nn.nll_loss(predictions, targets, weights, reduction, ignore_index) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint: disable=invalid-name | ||
"""NLLLoss in python""" | ||
import numpy as np | ||
|
||
|
||
def nll_loss(predictions, targets, weights, reduction="mean", ignore_index=-100): | ||
"""nll_loss operator implemented in numpy. | ||
output{n, i_1, i_2, ..., i_k} = -p * w | ||
where t = target{n, i_1, i_2, ..., i_k} | ||
p = predictions{n, t, i_1, i_2, i_k} | ||
w = weights{n, i_1, i_2, ..., i_k} if t != ignore_index else 0 | ||
result = reduction(output) | ||
Parameters | ||
---------- | ||
predictions : numpy.ndarray | ||
(k+2)-D with shape (N, C, d_1, d_2, ..., d_k), | ||
where C is the number of target classes | ||
targets : numpy.ndarray | ||
(k+1)-D with shape (N, d_1, d_2, ..., d_k) | ||
The target value of the input. | ||
weights : numpy.ndarray | ||
1-D with shape (C,) | ||
The weight of each target value. | ||
reduction : string | ||
The reduction method to apply to output. | ||
Can be "mean", "sum" or "none". | ||
ignore_index : int | ||
The target value to ignore. | ||
Returns | ||
------- | ||
output : numpy.ndarray | ||
a scalar if the reduction type is "mean" or "sum", | ||
otherwise the same shape as `target`. | ||
""" | ||
res = np.zeros(targets.shape) | ||
weight_sum = 0.0 | ||
for index in np.ndindex(targets.shape): | ||
class_id = targets[index] | ||
if class_id != ignore_index: | ||
index_list = list(index) | ||
pred_index = tuple(index_list[:1] + [class_id] + index_list[1:]) | ||
res[index] = -predictions[pred_index] * weights[class_id] | ||
weight_sum += weights[class_id] | ||
if reduction == "mean": | ||
return np.sum(res) / weight_sum | ||
if reduction == "sum": | ||
return np.sum(res) | ||
return res |
Oops, something went wrong.