-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathsk_examples.py
183 lines (154 loc) · 5.01 KB
/
sk_examples.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
ANFIS in torch: examples showing how to use ANFIS with sklearn via skorch
@author: James Power <[email protected]> Apr 12 18:13:10 2019
'''
import numpy as np
from sklearn.datasets import make_classification
import torch
from torch import nn
import torch.nn.functional as F
import skorch
from skorch.callbacks import Callback
import membership
import anfis
import jang_examples
import vignette_examples
import experimental
class FittingCallback(Callback):
'''
In order to use ANFIS-style hybrid learning with sklearn/skorch,
we need to add a callback to do the LSE step after each epoch.
This class contains that callback hook.
'''
def __init__(self):
super(FittingCallback, self).__init__()
def on_epoch_end(self, net, dataset_train=None,
dataset_valid=None, **kwargs):
# Get the dataset: different if we're train or train/test
# In the latter case, we have a Subset that contains the data...
if isinstance(dataset_train, torch.utils.data.dataset.Subset):
dataset_train = dataset_train.dataset
with torch.no_grad():
net.module.fit_coeff(dataset_train.X, dataset_train.y)
# #####
# ##### First example: an ineffective FIS for a simple classifier
# ##### See: https://skorch.readthedocs.io/en/stable/user/quickstart.html
# #####
class MySimpleNet(nn.Module):
'''
Very simple 2-layer net, slightly adapted from the docs:
https://skorch.readthedocs.io/en/stable/user/quickstart.html
'''
def __init__(self, num_in, num_feat, num_hidden=10, nonlin=F.relu):
super(MySimpleNet, self).__init__()
self.dense0 = nn.Linear(num_in, num_hidden)
self.nonlin = nonlin
self.dropout = nn.Dropout(0.5)
self.dense1 = nn.Linear(num_hidden, num_feat)
self.output = nn.Linear(num_feat, 2)
def forward(self, X, **kwargs):
X = self.nonlin(self.dense0(X))
X = self.dropout(X)
X = F.relu(self.dense1(X))
X = F.softmax(self.output(X))
return X
def train_simple_nn(X, y, num_in, num_feat):
nnet = skorch.NeuralNetClassifier(
MySimpleNet,
module__num_in=num_in,
module__num_feat=num_feat,
max_epochs=10,
lr=0.1,
)
nnet.fit(X, y)
return nnet
def fuzzy_classifier(num_in, num_mfs=5):
'''
Make a fuzzy classifier with 5 MFS per input, and one output
'''
sigma = 10 / num_mfs
mulist = torch.linspace(-5, 5, num_mfs).tolist()
invardefs = [('x{}'.format(i), membership.make_gauss_mfs(sigma, mulist))
for i in range(num_in)]
outvars = ['y0']
model = anfis.AnfisNet('Simple classifier', invardefs, outvars)
return model
def train_fuzzy(model, X, y, show_plots=True):
X = torch.tensor(X, dtype=torch.float)
y = torch.tensor(y, dtype=torch.float)
net = skorch.NeuralNet(
model,
max_epochs=50,
criterion=torch.nn.MSELoss,
optimizer=torch.optim.SGD,
optimizer__lr=1e-6,
optimizer__momentum=0.99,
callbacks=[FittingCallback()],
)
if show_plots:
experimental.plot_all_mfs(model, X)
net.fit(X, y)
if show_plots:
experimental.plot_all_mfs(model, X)
def classify_example():
num_in = 5
num_inf = num_in - 2
X, y = make_classification(1000, num_in,
n_informative=num_inf, random_state=0)
X = X.astype(np.float32)
model = fuzzy_classifier(num_in)
train_fuzzy(model, X, y)
train_simple_nn(X, y, num_in, num_inf)
# #####
# ##### Second example: Jang's example 1
# #####
def test_jang(show_plots=True):
model = jang_examples.ex1_model()
train_data = jang_examples.make_sinc_xy()
X, y = train_data.dataset.tensors
net = skorch.NeuralNet(
model,
max_epochs=100,
train_split=None,
criterion=torch.nn.MSELoss,
#criterion__reduction='sum',
optimizer=torch.optim.SGD,
optimizer__lr=1e-4,
optimizer__momentum=0.99,
callbacks=[FittingCallback()],
)
net.fit(X, y)
if show_plots:
experimental.plot_all_mfs(model, X)
y_actual = y
y_pred = model(X)
experimental.plotResults(y_actual, y_pred)
# #####
# ##### Third example: Vignette example 3
# #####
def test_vignette(show_plots=True):
model = vignette_examples.vignette_ex3()
X, y = jang_examples.make_sinc_xy_large().dataset.tensors
net = skorch.NeuralNet(
model,
max_epochs=50,
# train_split=None,
# batch_size=1024,
criterion=torch.nn.MSELoss,
# criterion__reduction='sum',
optimizer=torch.optim.SGD,
optimizer__lr=1e-4,
optimizer__momentum=0.99,
callbacks=[FittingCallback()],
)
net.fit(X, y)
if show_plots:
experimental.plot_all_mfs(model, X)
y_actual = y
y_pred = model(X)
experimental.plotResults(y_actual, y_pred)
classify_example()
#test_jang()
#test_vignette()