/
utilities.py
145 lines (113 loc) · 4.63 KB
/
utilities.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
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
from matplotlib import gridspec
import numpy as np
import random
random.seed(1)
np.random.seed(1)
torch.manual_seed(1)
class LMS(torch.nn.Module):
def __init__(self):
super(LMS, self).__init__()
self.neuron = torch.nn.Linear(10, 1)
def forward(self, x):
return self.neuron(x)
class nonlinear_LMS(torch.nn.Module):
def __init__(self):
super(nonlinear_LMS, self).__init__()
self.neuron = torch.nn.Linear(10, 1)
def forward(self, x):
return 2 * torch.tanh(self.neuron(x))
class deep_network(torch.nn.Module):
def __init__(self, layers):
super(deep_network, self).__init__()
self.layers = []
for i in range(len(layers)):
if i == 0:
self.layers.append(torch.nn.Linear(10, layers[i]))
else:
self.layers.append(torch.nn.Linear(layers[i-1], layers[i]))
self.layers = nn.ModuleList(self.layers)
def forward(self, x):
for i in range(len(self.layers)-1):
x = F.relu(self.layers[i](x))
return self.layers[-1](x)
def train_network(net, X, y, N, epochs, learning_rate):
loss_fn = nn.MSELoss()
optimizer = torch.optim.SGD(net.parameters(), lr=learning_rate)
train_loss = [0 for i in range(epochs)]
test_loss = [0 for i in range(epochs)]
for t in range(epochs):
# Forward pass
y_pred = net(X[:N, :])
# Compute loss
train_loss[t] = loss_fn(y_pred, y[:N, :])
optimizer.zero_grad()
# Backward pass.
train_loss[t].backward()
# Update
optimizer.step()
test_loss[t] = loss_fn(net(X[N:, :]), y[N:, :])
return train_loss, test_loss
def plot_data(X, y):
plt.figure(figsize=(12, 8))
gs = gridspec.GridSpec(2, 1)
ax = plt.subplot(gs[0])
for i in range(10):
plt.plot(X[:, i], label='$x_{' + str(i + 1) + '}[n]$')
plt.axvline(x=50, color='k')
plt.text(0.25, 0.9, 'Train', horizontalalignment='center', fontsize=14, transform = ax.transAxes)
plt.text(0.75, 0.9, 'Test', horizontalalignment='center', fontsize=14, transform = ax.transAxes)
leg = plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=10, mode="expand", borderaxespad=0., fontsize=11)
leg.get_frame().set_alpha(1)
x_vect = ''.join(['x_{' + str(i + 1) + '}[n], ' for i in range(9)])
plt.title('$\mathbf{x}[n] = [$ $' + x_vect + 'x_{10}[n]$ $]^T$', y=1.15, fontsize=14)
ax = plt.subplot(gs[1])
plt.plot(y)
plt.axvline(x=50, color='k')
plt.text(0.25, 0.9, 'Train', horizontalalignment='center', fontsize=14, transform = ax.transAxes)
plt.text(0.75, 0.9, 'Test', horizontalalignment='center', fontsize=14, transform = ax.transAxes)
leg = plt.legend(['$y[n]$'], fontsize=11)
leg.get_frame().set_alpha(1)
plt.title('$y[n] = \phi( \mathbf{x}[n] )$ where $\phi$ is highly non-linear', y=1.02, fontsize=14)
plt.tight_layout()
plt.show()
def plot_output(X, y, models, deep_network_layers):
plt.figure(figsize=(12,4))
ax = plt.subplot(1,1,1)
plt.plot(y)
[plt.plot(model(torch.Tensor(X)).detach().numpy()) for model in models]
plt.axvline(x=50,color='k')
plt.text(0.25, 0.9, 'Train', horizontalalignment='center', fontsize=14, transform = ax.transAxes)
plt.text(0.75, 0.9, 'Test', horizontalalignment='center', fontsize=14, transform = ax.transAxes)
leg = plt.legend(['$y[n]$','Single Neuron (linear)','Single Neuron (tanh)','Deep Network ('+str(deep_network_layers)+', relu)'],
bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=10, mode="expand", borderaxespad=0., fontsize=11)
leg.get_frame().set_alpha(1)
plt.tight_layout()
plt.show()
def train_models(X, y, models, epochs=100, learning_rate=1e-2):
loss = []
for i, model in enumerate(models):
(train_loss, test_loss) = train_network(model,
X=torch.Tensor(X),
y=torch.Tensor(y).reshape(-1,1),
N=50,
epochs=epochs,
learning_rate=learning_rate)
loss.append((train_loss, test_loss))
return loss
def plot_learning_curves(loss):
plt.figure(figsize=(12,4))
gs = gridspec.GridSpec(1, 3)
for i, l in enumerate(loss):
plt.subplot(gs[i])
plt.plot(l[0])
plt.plot(l[1])
plt.legend(['Train Loss', 'Test Loss'])
plt.tight_layout()
plt.show()