-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemi_supervised_learner.py
More file actions
67 lines (61 loc) · 2.31 KB
/
Copy pathsemi_supervised_learner.py
File metadata and controls
67 lines (61 loc) · 2.31 KB
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
class SSL:
"""
Class for Semi Supervised Learner (SSL), consisting of an autoencoder and a classifier.
"""
def __init__(self, autoencoder, classifier):
"""
Initializes variables.
:param autoencoder: The autoencoder part of the SSL.
:param classifier: The classifier part of the SSL.
"""
self.autoencoder = autoencoder
self.classifier = classifier
def fit_autoencoder(self, x, epochs, batch_size, x_val):
"""
Performs training for the autoencoder.
:param x: Input images.
:param epochs: Number of epochs to train.
:param batch_size: Batch size to use during training.
:param x_val: Validation data.
:return: History object obtained from training.
"""
return self.autoencoder.fit(x,
x,
epochs=epochs,
batch_size=batch_size,
validation_data=(x_val, x_val))
def fit_classifier(self, x, y, epochs, batch_size, validation_data):
"""
Performs training for the classifier.
:param x: Input data.
:param y: Labels encoded as one hot vectors.
:param epochs: Number of epochs to use during training.
:param batch_size: Batch size ot use during training.
:param validation_data: Tuple of (x_val, y_val)
:return: History object obtained from training.
"""
return self.classifier.fit(x,
y,
epochs=epochs,
batch_size=batch_size,
validation_data=validation_data)
def get_encoder(self):
"""
:return: The encoder of the autoencoder.
"""
return self.autoencoder.encoder
def evaluate_classifier(self, x, y):
"""
Evaluates the classifier.
:param x: Input data.
:param y: Labels encoded as one hot vectors.
:return: History object.
"""
return self.classifier.evaluate(x, y)
def forward_ae(self, x):
"""
Forward pass for the autoencoder.
:param x: x
:return: Output from the autoencoder.
"""
return self.autoencoder(x)