-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractModel.py
65 lines (48 loc) · 2.21 KB
/
AbstractModel.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
import numpy as np
class Model(object):
# initialize the object
def __init__(self, input_size: int, output_size: int) -> None:
# model: the model object that will be used to train and predict
self.model = None
raise NotImplementedError("__init__ not implemented")
# fit the model to the training data
# return history of training in a dictionary [loss, accuracy, val_loss, val_accuracy]
def fit(self, X_train: np.ndarray, y_train: np.ndarray, epochs: int) -> np.ndarray:
raise NotImplementedError("fit not implemented")
# return an array of n floats where each float represents the probability of the corresponding digit
# where n is the number of classes in the training data
def predict(self, X_test: np.ndarray) -> np.ndarray:
raise NotImplementedError("predict not implemented")
# return the mean accuracy on the given test data and labels
def score(self, X_test: np.ndarray, y_test: np.ndarray) -> float:
raise NotImplementedError("score not implemented")
# prints a summary of the model
def summary(self) -> None:
raise NotImplementedError("summary not implemented")
# Optional: save the model to a *.h5 file
def save(self, path: str) -> None:
raise NotImplementedError("save not implemented")
class DummyModel(Model):
def __init__(self):
self.X = None
self.y = None
self.fitted = False
print("Dummy model initialized")
def fit(self, X_train: np.ndarray, y_train: np.ndarray) -> None:
self.X = X_train
self.y = y_train
self.fitted = True
def predict(self, X_test: np.ndarray) -> np.ndarray:
if not self.fitted:
raise Exception("Model not fitted")
return np.array(np.random.rand(1, len(np.unique(self.y))))
def score(self, X_test: np.ndarray, y_test: np.ndarray) -> float:
sum_of_correct = 0
for i in range(len(X_test)):
if np.argmax(self.predict(X_test[i])) == y_test[i]:
sum_of_correct += 1
return sum_of_correct / len(X_test)
def summary(self):
print("Dummy model - summary")
def save(self, path: str) -> None:
print("Dummy model - save")