-
Notifications
You must be signed in to change notification settings - Fork 0
/
mckayla.py
151 lines (128 loc) · 4.24 KB
/
mckayla.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
import torch.nn as nn
import torch.optim as optim
from alias_utils.data import house
from alias_utils.model import RunnerBuilder as Builder
from alias_utils.loss import MSELoss
from alias_utils.metrics import (
Loss as LossMetric,
RSquared as RSquaredMetric,
DesignMatNorm as MNormMetric,
TestLoss as TestLossMetric,
)
from matplotlib import pyplot as plt
import matplotlib as mpl
# Shouldn't need this line anymore--it should now check and automatically disable TeX if it's not working.
mpl.rcParams["text.usetex"] = False
def train_with_optimizer(optimizer, optimizer_name):
train_data, test_data = house.data(train_percent=0.7)
wrapper = (
Builder()
.name(f"Model-{optimizer_name}")
.loss(MSELoss())
.optimizer(optimizer)
.steps(
nn.Linear(13, 8),
nn.Linear(8, 8),
nn.Linear(8, 1),
)
.with_metric(LossMetric())
.with_metric(RSquaredMetric())
.with_metric(MNormMetric())
.with_metric(TestLossMetric(loss=MSELoss(), data=test_data))
.build()
)
wrapper.train(train_data, n_epochs=5000)
wrapper.plot(log=False, normalize=True)
wrapper.plot_two(LossMetric, RSquaredMetric)
wrapper.plot_two(LossMetric, RSquaredMetric, log=True)
wrapper.plot_two(LossMetric, MNormMetric)
wrapper.plot_two(LossMetric, TestLossMetric)
plt.tight_layout()
plt.show()
print("Success")
def adam():
# Read in the data
train_data, test_data = house.data(train_percent=0.3)
# Construct a basic layered model
wrapper = (
Builder()
.name("Basic 3-Layer")
.loss(MSELoss())
.optimizer(optim.Adam)
.steps(
nn.Linear(13, 8),
nn.Linear(8, 8),
nn.Linear(8, 1),
)
.with_metric(LossMetric())
.with_metric(RSquaredMetric())
.with_metric(MNormMetric())
.build()
)
# Train the model
wrapper.train(train_data, n_epochs=5000)
# print(wrapper.model.M)
wrapper.plot(log=False, normalize=True)
wrapper.plot_two(LossMetric, RSquaredMetric)
wrapper.plot_two(LossMetric, RSquaredMetric, log=True)
wrapper.plot_two(LossMetric, MNormMetric)
wrapper.plot_two(LossMetric, MNormMetric, log=True)
plt.tight_layout()
plt.show()
# test the model
outputs = wrapper.test(test_data)
print(len(outputs))
y_pred, test_y = outputs[0]
y_pred = y_pred.squeeze(dim=1)
y_pred.detach()
test_y.detach()
# print('y_pred',y_pred)
# print('test_y',test_y)
# get the metrics
test_loss = MSELoss()
test_loss_vals = test_loss(y_pred=y_pred, y_true=test_y)
print()
print(test_loss_vals)
print()
plt.plot(test_loss_vals.detach().numpy())
plt.title("Test MSE Loss")
# plt.xlabel("")
plt.ylabel("Loss")
plt.show()
print("Success")
# # Read in the data
# data = house.data()
# # Construct a basic layered model
# wrapper = (
# Builder()
# .name("Basic 3-Layer")
# .loss(MSELoss())
# .optimizer(optim.Adam)
# .steps(
# nn.Linear(13, 8),
# nn.Linear(8, 8),
# nn.Linear(8, 1),
# )
# .with_metric(LossMetric())
# .with_metric(RSquaredMetric())
# .build()
# )
# # Train the model
# wrapper.train(data, n_epochs=5000)
# # wrapper.plot(log=False, normalize=True)
# wrapper.plot_two(LossMetric, RSquaredMetric)
# wrapper.plot_two(LossMetric, RSquaredMetric, log=True)
# wrapper.plot_two(0, 1)
# plt.tight_layout()
# plt.show()
# print("Success")
if __name__ == "__main__":
# adam()
train_with_optimizer(optim.Adam, "Adam Optimizer")
# train_with_optimizer(optim.SGD, "Stochastic Gradient Descent Optimizer") #is not working
# train_with_optimizer(optim.AdamW, "Adam with Weight Decay Optimizer")
# train_with_optimizer(optim.Adagrad, "Adaptive Gradient Optimizer")
# train_with_optimizer(optim.Adadelta, "Adadelta Optimizer")
# train_with_optimizer(optim.ASGD, "Averaged Stochastic Gradient Descent Optimizer")
# train_with_optimizer(optim.NAdam, "Nesterov-Accelerated Adam Optimizer")
# train_with_optimizer(optim.LBFGS, "Limited-Memory BFGS Optimizer")