diff --git a/python_module/megengine/module/activation.py b/python_module/megengine/module/activation.py index 8240c488b91d64df02935d9cc5b3e86d57b1c3c3..a6bba573d7be8840a1b08dda4dbb89cd139dbf20 100644 --- a/python_module/megengine/module/activation.py +++ b/python_module/megengine/module/activation.py @@ -223,5 +223,9 @@ class LeakyReLU(Module): """ + def __init__(self, negative_slope: float = 0.01): + super().__init__() + self.negative_slope = negative_slope + def forward(self, inputs): - return leaky_relu(inputs) + return leaky_relu(inputs, self.negative_slope) diff --git a/python_module/test/unit/module/test_activation.py b/python_module/test/unit/module/test_activation.py new file mode 100644 index 0000000000000000000000000000000000000000..afca1fde0cf5781d33b580d808d3b86f0fa3bcf7 --- /dev/null +++ b/python_module/test/unit/module/test_activation.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +# MegEngine is Licensed under the Apache License, Version 2.0 (the "License") +# +# Copyright (c) 2014-2020 Megvii Inc. All rights reserved. +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +import numpy as np + +import megengine as mge +from megengine.module import LeakyReLU +from megengine.test import assertTensorClose + + +def test_leaky_relu(): + data = np.array([-8, -12, 6, 10]).astype(np.float32) + negative_slope = 0.1 + + leaky_relu = LeakyReLU(negative_slope) + output = leaky_relu(mge.tensor(data)) + + np_output = np.maximum(0, data) + negative_slope * np.minimum(0, data) + assertTensorClose(output.numpy(), np_output, max_err=0)