-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_defuzzy_layer.py
132 lines (97 loc) · 3.28 KB
/
test_defuzzy_layer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import pytest
from torchfuzzy import DefuzzyLinearLayer
import torch
import numpy as np
def test_from_array_correct_initialization():
model = DefuzzyLinearLayer.from_array([
[1,2],
[1,2],
[1,2]])
assert model.Z.shape == (1, 3, 2)
def test_from_dimensions_correct_initialization():
model = DefuzzyLinearLayer.from_dimensions(4, 7)
assert model.Z.shape == (1, 7, 4)
def test_1d_input_1d_output():
batch_size = 1
size_in = 1
size_out = 1
x = torch.randn((batch_size, size_in))
model = DefuzzyLinearLayer.from_dimensions(size_in, size_out)
y_pred = model(x)
assert y_pred.shape == (batch_size,size_out)
def test_1d_input_2d_output():
batch_size = 1
size_in = 1
size_out = 2
x = torch.randn((batch_size, size_in))
model = DefuzzyLinearLayer.from_dimensions(size_in, size_out)
y_pred = model(x)
assert y_pred.shape == (batch_size, size_out)
def test_1d_input_7d_output():
batch_size = 1
size_in = 1
size_out = 7
x = torch.randn((batch_size, size_in))
model = DefuzzyLinearLayer.from_dimensions(size_in, size_out)
y_pred = model(x)
assert y_pred.shape == (batch_size,size_out)
def test_2d_input_1d_output():
batch_size = 1
size_in = 2
size_out = 1
x = torch.randn((batch_size, size_in))
model = DefuzzyLinearLayer.from_dimensions(size_in, size_out)
y_pred = model(x)
assert y_pred.shape == (batch_size,size_out)
def test_2d_input_2d_output():
batch_size = 1
size_in = 2
size_out = 2
x = torch.randn((batch_size, size_in))
model = DefuzzyLinearLayer.from_dimensions(size_in, size_out)
y_pred = model(x)
assert y_pred.shape == (batch_size,size_out)
def test_7d_input_7d_output():
batch_size = 1
size_in = 7
size_out = 7
x = torch.randn((batch_size, size_in))
model = DefuzzyLinearLayer.from_dimensions(size_in, size_out)
y_pred = model(x)
assert y_pred.shape == (batch_size,size_out)
def test_inference_1():
model = DefuzzyLinearLayer.from_array([
[1, 2],
[1, 2],
[1, 2]])
x = torch.tensor([[0.0, 1.0]],requires_grad=False)
inference = model.forward(x).detach().numpy()
assert len(inference) == 1
assert inference[0] == pytest.approx([2, 2, 2], abs=1e-2)
def test_inference_2():
model = DefuzzyLinearLayer.from_array([
[1, 2],
[1, 2],
[1, 2]])
x = torch.tensor([[1.0, 0.0]],requires_grad=False)
inference = model.forward(x).detach().numpy()
assert len(inference) == 1
assert inference[0] == pytest.approx([1, 1, 1], abs=1e-2)
def test_inference_3():
model = DefuzzyLinearLayer.from_array([
[1, 2],
[1, 2],
[1, 2]])
x = torch.tensor([[1.0, 1.0]],requires_grad=False)
inference = model.forward(x).detach().numpy()
assert len(inference) == 1
assert inference[0] == pytest.approx([1.5, 1.5, 1.5], abs=1e-2)
def test_inference_4():
model = DefuzzyLinearLayer.from_array([
[1, 2],
[1, 2],
[1, 2]])
x = torch.tensor([[111.0, 111.0]],requires_grad=False)
inference = model.forward(x).detach().numpy()
assert len(inference) == 1
assert inference[0] == pytest.approx([1.5, 1.5, 1.5], abs=1e-2)