Browse Source

fix(mge/module): add negative slope attribute for LeakyReLU module

GitOrigin-RevId: 67e1967b56
tags/v0.4.0
Megvii Engine Team Xinran Xu 5 years ago
parent
commit
dd962a4700
2 changed files with 29 additions and 1 deletions
  1. +5
    -1
      python_module/megengine/module/activation.py
  2. +24
    -0
      python_module/test/unit/module/test_activation.py

+ 5
- 1
python_module/megengine/module/activation.py View File

@@ -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): def forward(self, inputs):
return leaky_relu(inputs)
return leaky_relu(inputs, self.negative_slope)

+ 24
- 0
python_module/test/unit/module/test_activation.py View File

@@ -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)

Loading…
Cancel
Save