From dd962a47008bb8a79fd6c8dc46d6f62064a6cf22 Mon Sep 17 00:00:00 2001 From: Megvii Engine Team Date: Thu, 23 Apr 2020 20:47:10 +0800 Subject: [PATCH] fix(mge/module): add negative slope attribute for LeakyReLU module GitOrigin-RevId: 67e1967b56e63d227c0fe080619d89195d0df35b --- python_module/megengine/module/activation.py | 6 ++++- .../test/unit/module/test_activation.py | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 python_module/test/unit/module/test_activation.py diff --git a/python_module/megengine/module/activation.py b/python_module/megengine/module/activation.py index 8240c488..a6bba573 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 00000000..afca1fde --- /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) -- GitLab