-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodels.py
More file actions
118 lines (90 loc) · 3.19 KB
/
models.py
File metadata and controls
118 lines (90 loc) · 3.19 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
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
from abc import ABC, abstractmethod
from enum import Enum
import os
import numpy as np
import rpy2.robjects as robjects
import rpy2.robjects.numpy2ri as rpyn
from rpy2.robjects.packages import STAP
import torch
from constants import *
import net
class ModelType(Enum):
GPR = 0
NEURAL_NET = 1
class Model(ABC):
def __init__(self, model, model_type):
self.model_type = model_type
def __enter__(self):
return self
def __exit__(self):
self.close()
def close(self):
pass
def get_type(self):
return self.model_type
@abstractmethod
def train(self, X_train, y_train):
pass
@abstractmethod
def evaluate(self, X):
pass
class GPRModel(Model):
def __init__(self):
self.activate_R()
self.is_trained = False
super().__init__(self, ModelType.GPR)
def train(self, X_train, y_train):
X_trainR = robjects.r.matrix(
X_train, nrow=X_train.shape[0], ncol=X_train.shape[1]
)
y_trainR = robjects.r.matrix(y_train, nrow=y_train.shape[0], ncol=1)
self.model = self.gpr_lib.train_new_GP(X_trainR, y_trainR)
self.is_trained = True
def evaluate(self, X, clip=True, n=1):
X_evalR = robjects.r.matrix(X, nrow=X.shape[0], ncol=X.shape[1])
if not self.is_trained:
raise Exception("GPR model needs to be trained before evaluating.")
resultR = self.gpr_lib.sample_GP(self.model, X_evalR, n)
result = np.array(resultR)
if clip:
result = np.clip(result, 0, 1)
samples, variances = result[:, 0], result[:, 1]
return samples, variances
def activate_R(self):
with open("gpr_lib.R", "r") as f:
s = f.read()
self.gpr_lib = STAP(s, "gpr_lib")
robjects.r("Sys.setenv(MKL_DEBUG_CPU_TYPE = '5')")
rpyn.activate()
def close(self):
# Clean up R's GPR model object
self.gpr_lib.delete_GP(self.model)
class NeuralNetModel(Model):
def __init__(self, models_path):
self.models_path = models_path
self.models = []
self.is_trained = False
super().__init__(self, ModelType.NEURAL_NET)
@classmethod
def load_trained_models(cls, models_path):
obj = cls(models_path)
for filename in os.listdir(models_path):
if "bag_model" in filename:
model = torch.load(os.path.join(models_path, filename), map_location=torch.device(net.DEVICE))
obj.models.append(model)
obj.is_trained = True
return obj
def check_path(self):
if not os.path.exists(self.models_path):
os.makedirs(self.models_path)
def train(self, X_train, y_train, **kwargs):
self.check_path()
self.models = net.train_bagged(X_train, y_train, self.models_path, **kwargs)
self.is_trained = True
def evaluate(self, X, clip=True):
if not self.is_trained:
raise Exception("Neural net model needs to be trained before evaluating.")
predictions, variances = net.eval_bagged(X, self.models)
if clip:
predictions = np.clip(predictions, 0, 1)
return predictions, variances