-
Notifications
You must be signed in to change notification settings - Fork 62
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
5 changed files
with
91 additions
and
2 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,24 @@ | ||
import math | ||
|
||
import torch | ||
from torch import nn | ||
|
||
|
||
class Hardsigmoid(nn.Module): | ||
def __new__(cls, alpha=0.2, beta=0.5): | ||
""" | ||
If alpha and beta same as default values for torch's Hardsigmoid, | ||
return torch's Hardsigmoid. Else, return custom Hardsigmoid. | ||
""" | ||
if math.isclose(alpha, 1 / 6, abs_tol=1e-2) and beta == 0.5: | ||
return nn.Hardsigmoid() | ||
else: | ||
return super().__new__(cls) | ||
|
||
def __init__(self, alpha=0.2, beta=0.5): | ||
super().__init__() | ||
self.alpha = alpha | ||
self.beta = beta | ||
|
||
def forward(self, input): | ||
return torch.clip(input * self.alpha + self.beta, 0, 1) |
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,57 @@ | ||
from unittest.mock import MagicMock | ||
|
||
import numpy as np | ||
import onnx | ||
import torch | ||
import pytest | ||
|
||
from onnx2pytorch.convert.operations import convert_operations | ||
from onnx2pytorch.operations import Hardsigmoid | ||
|
||
|
||
@pytest.fixture | ||
def x(): | ||
return np.random.randn(3, 4, 5).astype(np.float32) | ||
|
||
|
||
def test_hardsigmoid(x): | ||
alpha = 1 / 6 | ||
beta = 1 / 2 | ||
op = Hardsigmoid(alpha=alpha, beta=beta) | ||
# For pytorch's default values it should use torch's Hardsigmoid | ||
assert isinstance(op, torch.nn.Hardsigmoid) | ||
x = np.random.randn(3, 4, 5).astype(np.float32) | ||
y = np.clip(x * alpha + beta, 0, 1) | ||
out = op(torch.from_numpy(x)) | ||
np.testing.assert_allclose(out, torch.from_numpy(y), rtol=1e-6, atol=1e-6) | ||
|
||
|
||
def test_hardsigmoid_with_custom_alpha_and_beta(x): | ||
alpha = 0.2 | ||
beta = 0.5 | ||
op = Hardsigmoid(alpha=alpha, beta=beta) | ||
assert not isinstance(op, torch.nn.Hardsigmoid) | ||
y = np.clip(x * alpha + beta, 0, 1) | ||
out = op(torch.from_numpy(x)) | ||
np.testing.assert_allclose(out, torch.from_numpy(y), rtol=1e-6, atol=1e-6) | ||
|
||
|
||
def test_hardsigmoid_conversion(): | ||
alpha = np.float32(0.2) | ||
beta = np.float32(0.5) | ||
node = onnx.helper.make_node( | ||
"HardSigmoid", | ||
inputs=["x"], | ||
outputs=["y"], | ||
alpha=alpha, | ||
beta=beta, | ||
) | ||
|
||
graph = MagicMock() | ||
graph.initializers = [] | ||
graph.node = [node] | ||
converted_ops = list(convert_operations(graph, 10)) | ||
op_id, op_name, op = converted_ops[0] | ||
assert isinstance(op, Hardsigmoid) | ||
assert op.alpha == alpha | ||
assert op.beta == beta |