Skip to content

Commit e93a161

Browse files
authored
add ReLU to MLP predictor and add tests for it (#1031)
This PR adds ReLU to Multilayer perceptron predictor. The ReLU implementation is the same as for Neural Network predictor. Tests for MLP regressor and classifier with ReLU activation function are passing.
1 parent bf33711 commit e93a161

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed
13.3 KB
Binary file not shown.
9.23 KB
Binary file not shown.

pymoose/pymoose/predictors/multilayer_perceptron_predictor.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
class Activation(Enum):
1212
IDENTITY = 1
1313
SIGMOID = 2
14+
RELU = 3
1415

1516

1617
class MLPPredictor(aes_predictor.AesPredictor, metaclass=abc.ABCMeta):
@@ -72,6 +73,8 @@ def from_onnx(cls, model_proto):
7273
)
7374
if activation_str == "Sigmoid":
7475
activation = Activation.SIGMOID
76+
elif activation_str == "Relu":
77+
activation = Activation.RELU
7578
else:
7679
activation = Activation.IDENTITY
7780

@@ -95,13 +98,14 @@ def apply_layer(self, input, num_hidden_layers, i, fixedpoint_dtype):
9598
def activation_fn(self, z):
9699
if self.activation == Activation.SIGMOID:
97100
activation_output = edsl.sigmoid(z)
98-
# There is a bug in edsl.shape
99-
# Relu code:
100-
# y_1_shape = edsl.slice(edsl.shape(x), begin=0, end=1)
101-
# ones = edsl.ones(y_1_shape, dtype=edsl.float64)
102-
# ones = edsl.cast(ones, dtype=fixedpoint_dtype)
103-
# zeros = edsl.sub(ones, ones)
104-
# activation_output = edsl.maximum([zeros, y_1])
101+
elif self.activation == Activation.RELU:
102+
z_shape = edsl.shape(z)
103+
with self.bob:
104+
ones_1 = edsl.ones(z_shape, dtype=predictor_utils.DEFAULT_FLOAT_DTYPE)
105+
ones_2 = edsl.ones(z_shape, dtype=predictor_utils.DEFAULT_FLOAT_DTYPE)
106+
zeros = edsl.sub(ones_1, ones_2)
107+
zeros = edsl.cast(zeros, dtype=predictor_utils.DEFAULT_FIXED_DTYPE)
108+
activation_output = edsl.maximum([zeros, z])
105109
elif self.activation == Activation.IDENTITY:
106110
activation_output = z
107111
else:

pymoose/pymoose/predictors/multilayer_perceptron_predictor_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"MLP_regressor_2hidden_layers_2target_identity",
2626
[[550.57188666, 431.31144628], [57.07484159, 128.76424831]],
2727
),
28+
("MPL_regressor_1hidden_layers_1target_relu", [32.55903733, 16.92872095]),
2829
]
2930
_SK_CLASSIFIER_MODELS = [
3031
("MLP_classifier_1hidden_layers_2classes_logistic", [0.60823407, 0.39176593]),
@@ -60,6 +61,21 @@
6061
0.01489627,
6162
],
6263
),
64+
(
65+
"MPL_classfier_1hidden_layers_10classes_relu",
66+
[
67+
0.18669855,
68+
0.01198506,
69+
0.02292363,
70+
0.04383366,
71+
0.4636751,
72+
0.08422828,
73+
0.0207348,
74+
0.07604881,
75+
0.0196595,
76+
0.07021261,
77+
],
78+
),
6379
]
6480

6581

0 commit comments

Comments
 (0)