-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathbase_models.py
133 lines (89 loc) · 3.91 KB
/
base_models.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
# Copyright (c) 2020, Ahmed M. Alaa
# Licensed under the BSD 3-clause license (see LICENSE.txt)
# ---------------------------------------------------------
# Base classes for feedforward, convolutional and recurrent
# neural network (DNN, CNN, RNN) models in pytorch
# ---------------------------------------------------------
# -------------------------------------
# | TO DO: |
# | ------ |
# | Loss functions file |
# | ADD EPOCHS |
# | argument explanation for the DNN |
# | Exception handling |
# | Multiple architectures in RNN |
# | cmd arguments |
# | logger, misc and config files |
# -------------------------------------
from __future__ import absolute_import, division, print_function
import numpy as np
import pandas as pd
import sys
if not sys.warnoptions:
import warnings
warnings.simplefilter("ignore")
import torch
from torch.autograd import Variable
import torch.nn.functional as nnf
from torch.utils.data import random_split
from torch.optim import SGD
from torch.distributions import constraints
import torchvision as torchv
import torchvision.transforms as torchvt
from torch import nn
import torchvision.transforms as transforms
from torch.autograd import grad
import scipy.stats as st
from sklearn.preprocessing import StandardScaler
from copy import deepcopy
import time
from utils.parameters import *
torch.manual_seed(1)
class DNN(nn.Module):
def __init__(self,
n_dim=1,
dropout_prob=0.0,
dropout_active=False,
num_layers=2,
num_hidden=200,
output_size=1,
activation="Tanh",
mode="Regression"
):
super(DNN, self).__init__()
self.n_dim = n_dim
self.num_layers = num_layers
self.num_hidden = num_hidden
self.mode = mode
self.activation = activation
self.device = torch.device('cpu') # Make this an option
self.output_size = output_size
self.dropout_prob = dropout_prob
self.dropout_active = dropout_active
self.model = build_architecture(self)
def fit(self, X, y, learning_rate=1e-3, loss_type="MSE", batch_size=100, num_iter=500, verbosity=False):
self.X = torch.tensor(X.reshape((-1, self.n_dim))).float()
self.y = torch.tensor(y).float()
loss_dict = {"MSE": torch.nn.MSELoss}
self.loss_fn = loss_dict[loss_type](reduction='mean')
self.loss_trace = []
batch_size = np.min((batch_size, X.shape[0]))
optimizer = torch.optim.Adam(self.parameters(), lr=learning_rate)
for _ in range(num_iter):
batch_idx = np.random.choice(list(range(X.shape[0])), batch_size )
y_pred = self.model(self.X[batch_idx, :])
self.loss = self.loss_fn(y_pred.reshape((batch_size, self.n_dim)), self.y[batch_idx].reshape((batch_size, self.n_dim)))
self.loss_trace.append(self.loss.detach().numpy())
if verbosity:
print("--- Iteration: %d \t--- Loss: %.3f" % (_, self.loss.item()))
self.model.zero_grad()
optimizer.zero_grad() # clear gradients for this training step
self.loss.backward() # backpropagation, compute gradients
optimizer.step()
def predict(self, X, numpy_output=True):
X = torch.tensor(X.reshape((-1, self.n_dim))).float()
if numpy_output:
prediction = self.model(X).detach().numpy()
else:
prediction = self.model(X)
return prediction