-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
250 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# 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) | ||
else: | ||
return res |
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,70 @@ | ||
# 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. | ||
"""Test code for loss operators.""" | ||
import numpy as np | ||
import pytest | ||
import tvm | ||
from tvm import te | ||
from tvm import topi | ||
import tvm.topi.testing | ||
|
||
import tvm.testing | ||
|
||
|
||
def verify_nll_loss(prediction_shape, reduction="mean", ignore_index=-100, dtype="float32"): | ||
C = prediction_shape[1] | ||
target_shape = prediction_shape[:1] + prediction_shape[2:] | ||
predictions = te.placeholder(shape=prediction_shape, name="predictions", dtype=dtype) | ||
targets = te.placeholder(shape=target_shape, name="targets", dtype="int32") | ||
weights = te.placeholder(shape=(C,), name="weights", dtype=dtype) | ||
nll_loss_result = topi.nn.nll_loss( | ||
predictions, targets, weights, reduction, ignore_index | ||
) | ||
|
||
def check_device(target, dev): | ||
print("Running on target: %s" % target) | ||
with tvm.target.Target(target): | ||
s = tvm.topi.testing.get_injective_schedule(target)(nll_loss_result) | ||
fn = tvm.build(s, [predictions, targets, weights, nll_loss_result], target, name="nll_loss") | ||
predictions_npy = np.random.uniform(size=prediction_shape).astype(dtype) | ||
targets_npy = np.random.randint(0, C, target_shape).astype("int32") | ||
weights_npy = np.random.uniform(size=(C,)).astype(dtype) | ||
out_npy = tvm.topi.testing.nll_loss(predictions_npy, targets_npy, weights_npy, reduction, ignore_index) | ||
predictions_nd = tvm.nd.array(predictions_npy, dev) | ||
targets_nd = tvm.nd.array(targets_npy, dev) | ||
weights_nd = tvm.nd.array(weights_npy, dev) | ||
out_nd = tvm.nd.array(np.empty(out_npy.shape).astype(nll_loss_result.dtype), dev) | ||
fn(predictions_nd, targets_nd, weights_nd, out_nd) | ||
out_topi = out_nd.asnumpy() | ||
tvm.testing.assert_allclose(out_topi, out_npy) | ||
|
||
for target, dev in tvm.testing.enabled_targets(): | ||
check_device(target, dev) | ||
|
||
|
||
@tvm.testing.uses_gpu | ||
def test_nll_loss(): | ||
verify_nll_loss((10, 5,)) | ||
verify_nll_loss((10, 5, 2, 2)) | ||
verify_nll_loss((10, 5,), reduction="sum") | ||
verify_nll_loss((10, 5,), reduction="none") | ||
verify_nll_loss((10, 5,), ignore_index=3) | ||
verify_nll_loss((10, 5,), dtype="float64") | ||
|
||
|
||
if __name__ == "__main__": | ||
test_nll_loss() |