diff --git a/imperative/python/megengine/functional/__init__.py b/imperative/python/megengine/functional/__init__.py index 4d70c8a6855f398672d853e1346c90be0882aeb5..4dc2675aaf3c2130c2c5b4e27434ed99cd9884e2 100644 --- a/imperative/python/megengine/functional/__init__.py +++ b/imperative/python/megengine/functional/__init__.py @@ -8,7 +8,6 @@ # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # pylint: disable=redefined-builtin from .elemwise import * -from .loss import * from .math import * from .nn import * from .quantized import conv_bias_activation diff --git a/imperative/python/megengine/functional/loss.py b/imperative/python/megengine/functional/loss.py index e5e1b6a5defe29f2b219f5d3fa589d317bf48eb2..8ed5958a483f804a2984b505fd2edd4dcb538c5a 100644 --- a/imperative/python/megengine/functional/loss.py +++ b/imperative/python/megengine/functional/loss.py @@ -55,7 +55,7 @@ def l1_loss(pred: Tensor, label: Tensor) -> Tensor: ipt = mge.tensor(np.array([3, 3, 3, 3]).astype(np.float32)) tgt = mge.tensor(np.array([2, 8, 6, 1]).astype(np.float32)) - loss = F.l1_loss(ipt, tgt) + loss = F.nn.l1_loss(ipt, tgt) print(loss.numpy()) Outputs: @@ -106,7 +106,7 @@ def square_loss(pred: Tensor, label: Tensor) -> Tensor: ipt = mge.tensor(np.array([3, 3, 3, 3]).astype(np.float32)) tgt = mge.tensor(np.array([2, 8, 6, 1]).astype(np.float32)) - loss = F.square_loss(ipt, tgt) + loss = F.nn.square_loss(ipt, tgt) print(loss.numpy()) Outputs: @@ -159,7 +159,7 @@ def cross_entropy( label_shape = (1, ) pred = tensor(np.array([0, 0], dtype=np.float32).reshape(data_shape)) label = tensor(np.ones(label_shape, dtype=np.int32)) - loss = F.cross_entropy(pred, label) + loss = F.nn.cross_entropy(pred, label) print(loss.numpy()) Outputs: @@ -226,7 +226,7 @@ def binary_cross_entropy( pred = tensor(np.array([0, 0], dtype=np.float32).reshape(1, 2)) label = tensor(np.ones((1, 2), dtype=np.float32)) - loss = F.binary_cross_entropy(pred, label) + loss = F.nn.binary_cross_entropy(pred, label) print(loss.numpy()) Outputs: @@ -264,7 +264,7 @@ def hinge_loss(pred: Tensor, label: Tensor, norm: str = "L1") -> Tensor: pred = tensor([[0.5, -0.5, 0.1], [-0.6, 0.7, 0.8]], dtype="float32") label = tensor([[1, -1, -1], [-1, 1, 1]], dtype="float32") - loss = F.hinge_loss(pred, label) + loss = F.nn.hinge_loss(pred, label) print(loss.numpy()) Outputs: diff --git a/imperative/python/megengine/functional/nn.py b/imperative/python/megengine/functional/nn.py index 1e62901d9189f53b7c52e1a3e4a675bda06dabfa..d146ce678afa794336243235368a8c8542769141 100644 --- a/imperative/python/megengine/functional/nn.py +++ b/imperative/python/megengine/functional/nn.py @@ -1522,5 +1522,4 @@ def nms(boxes: Tensor, scores: Tensor, iou_thresh: float) -> Tensor: -from .loss import * # isort:skip -from .quantized import conv_bias_activation # isort:skip +from .loss import * diff --git a/imperative/python/test/integration/test_converge.py b/imperative/python/test/integration/test_converge.py index 0815ca63499c0fadb674fa4437aed953f5e924b9..d16570c7fe4401883d49363eac825a255a86762b 100644 --- a/imperative/python/test/integration/test_converge.py +++ b/imperative/python/test/integration/test_converge.py @@ -80,7 +80,7 @@ def test_training_converge(): def train(data, label): with gm: pred = net(data) - loss = F.cross_entropy(pred, label) + loss = F.nn.cross_entropy(pred, label) gm.backward(loss) return loss diff --git a/imperative/python/test/integration/test_correctness.py b/imperative/python/test/integration/test_correctness.py index 9dd31cc00cab1ad4d14058eb3e34ab2f19cf9552..d33bde89edbd5f53e8943e94d27895ac96d0013a 100644 --- a/imperative/python/test/integration/test_correctness.py +++ b/imperative/python/test/integration/test_correctness.py @@ -92,7 +92,7 @@ class MnistNet(Module): def train(data, label, net, opt, gm): with gm: pred = net(data) - loss = F.cross_entropy(pred, label) + loss = F.nn.cross_entropy(pred, label) gm.backward(loss) return loss diff --git a/imperative/python/test/integration/test_dp_correctness.py b/imperative/python/test/integration/test_dp_correctness.py index c4774272404fd97ea914fb2ea894c6f74f0f4ac9..3491cf5f34e1a5059d2690ae482f2885765aaef6 100644 --- a/imperative/python/test/integration/test_dp_correctness.py +++ b/imperative/python/test/integration/test_dp_correctness.py @@ -98,7 +98,7 @@ def train(data, label, net, opt, gm): opt.clear_grad() with gm: pred = net(data) - loss = F.cross_entropy(pred, label) + loss = F.nn.cross_entropy(pred, label) gm.backward(loss) opt.step() return loss diff --git a/imperative/python/test/integration/test_trace_dump.py b/imperative/python/test/integration/test_trace_dump.py index 742803b457dbcc26aeb49d43506fdc15b313d9f5..37cb4fd46d2ce3bafd8040d7534f3890e8196932 100644 --- a/imperative/python/test/integration/test_trace_dump.py +++ b/imperative/python/test/integration/test_trace_dump.py @@ -72,7 +72,7 @@ def test_xornet_trace_dump(): with gm: net.train() pred = net(data) - loss = F.cross_entropy(pred, label) + loss = F.nn.cross_entropy(pred, label) gm.backward(loss) return pred, loss @@ -80,7 +80,7 @@ def test_xornet_trace_dump(): def val_fun(data, label): net.eval() pred = net(data) - loss = F.cross_entropy(pred, label) + loss = F.nn.cross_entropy(pred, label) return pred, loss @trace(symbolic=True, capture_as_const=True) diff --git a/imperative/python/test/unit/functional/test_functional.py b/imperative/python/test/unit/functional/test_functional.py index 09ccdc3bf908c3cfbbe857a18ddc5384b61a0c96..25f96a2628daa85973892c38638dfb7497ac05b2 100644 --- a/imperative/python/test/unit/functional/test_functional.py +++ b/imperative/python/test/unit/functional/test_functional.py @@ -317,14 +317,16 @@ def test_binary_cross_entropy(): {"input": [data1, label1], "output": expect1,}, {"input": [data2, label2], "output": expect2,}, ] - opr_test(cases, F.binary_cross_entropy, compare_fn=compare_fn) + opr_test(cases, F.nn.binary_cross_entropy, compare_fn=compare_fn) cases = [ {"input": [sigmoid(data1), label1], "output": expect1,}, {"input": [sigmoid(data2), label2], "output": expect2,}, ] opr_test( - cases, partial(F.binary_cross_entropy, with_logits=False), compare_fn=compare_fn + cases, + partial(F.nn.binary_cross_entropy, with_logits=False), + compare_fn=compare_fn, ) @@ -338,7 +340,7 @@ def test_hinge_loss(): expect = np.clip(0, np.inf, 1 - data * label).sum(axis=1).mean() cases.append({"input": [data, label], "output": expect}) - opr_test(cases, F.hinge_loss) + opr_test(cases, F.nn.hinge_loss) # cases with L2 norm cases = [] @@ -349,7 +351,7 @@ def test_hinge_loss(): cases.append({"input": [data, label], "output": expect}) def hinge_loss_with_l2_norm(pred, label): - return F.hinge_loss(pred, label, "L2") + return F.nn.hinge_loss(pred, label, "L2") opr_test(cases, hinge_loss_with_l2_norm) diff --git a/imperative/python/test/unit/functional/test_loss.py b/imperative/python/test/unit/functional/test_loss.py index 8464a3a21bec35abccf9cf978e903f2ab08a1524..8bfd1cd5e24d83d27fc2c4604100af954f157668 100644 --- a/imperative/python/test/unit/functional/test_loss.py +++ b/imperative/python/test/unit/functional/test_loss.py @@ -15,14 +15,14 @@ from megengine import tensor def test_cross_entropy_with_logits(): data = tensor([1, 100]).astype(np.float32).reshape((1, 2)) label = tensor([1]).astype(np.int32) - loss = F.cross_entropy(data, label) + loss = F.nn.cross_entropy(data, label) np.testing.assert_allclose(loss.numpy(), 0.0) label = tensor([0]).astype(np.int32) - loss = F.cross_entropy(data, label) + loss = F.nn.cross_entropy(data, label) np.testing.assert_allclose(loss.numpy(), 100 - 1) label = np.array([1]) - loss = F.cross_entropy(data, label) + loss = F.nn.cross_entropy(data, label) np.testing.assert_allclose(loss.numpy(), 0.0) @@ -41,5 +41,5 @@ def test_cross_entropy(): x[i, y[i]] += np.random.rand() * 2 x = softmax(x) l_ref = ref(x, y) - l = F.cross_entropy(tensor(x, "float32"), tensor(y, "int32"), with_logits=False) + l = F.nn.cross_entropy(tensor(x, "float32"), tensor(y, "int32"), with_logits=False) np.testing.assert_allclose(l.numpy(), l_ref)