This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X = [x for x in range(11)] | |
y = [1.6*x + 4 + np.random.normal(10, 1) for x in X] | |
X, y | |
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | |
[8.059610387807004, | |
11.05288064074008, | |
11.353963162111054, | |
13.816355592580631, | |
14.13887152857681, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
seq_model = nn.Sequential( | |
nn.Linear(1, 13), | |
nn.Tanh(), | |
nn.Linear(13, 1)) | |
seq_model | |
>>> Sequential( | |
(0): Linear(in_features=1, out_features=13, bias=True) | |
(1): Tanh() | |
(2): Linear(in_features=13, out_features=1, bias=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def training_loop(n_epochs, optimiser, model, loss_fn, X_train, X_val, y_train, y_val): | |
for epoch in range(1, n_epochs + 1): | |
output_train = model(X_train) # forwards pass | |
loss_train = loss_fn(output_train, y_train) # calculate loss | |
output_val = model(X_val) | |
loss_val = loss_fn(output_val, y_val) | |
optimiser.zero_grad() # set gradients to zero | |
loss_train.backward() # backwards pass | |
optimiser.step() # update model parameters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
optimiser = optim.SGD(seq_model.parameters(), lr=1e-3) | |
training_loop( | |
n_epochs = 500000, | |
optimiser = optimiser, | |
model = seq_model, | |
loss_fn = nn.MSELoss(), | |
X_train = X_train, | |
X_val = X_val, | |
y_train = y_train, | |
y_val = y_val) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
N = 100 # number of samples | |
L = 1000 # length of each sample (number of values for each sine wave) | |
T = 20 # width of the wave | |
x = np.empty((N,L), np.float32) # instantiate empty array | |
x[:] = np.arange(L)) + np.random.randint(-4*T, 4*T, N).reshape(N,1) | |
y = np.sin(x/1.0/T).astype(np.float32) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LSTM(nn.Module): | |
def __init__(self, hidden_layers=64): | |
super(LSTM, self).__init__() | |
self.hidden_layers = hidden_layers | |
# lstm1, lstm2, linear are all layers in the network | |
self.lstm1 = nn.LSTMCell(1, self.hidden_layers) | |
self.lstm2 = nn.LSTMCell(self.hidden_layers, self.hidden_layers) | |
self.linear = nn.Linear(self.hidden_layers, 1) | |
def forward(self, y, future_preds=0): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = torch.from_numpy(y[3:, :-1]) | |
b = a.split(1, dim=1) | |
b[0].shape | |
>>> torch.Size([97, 1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# y = (100, 1000) | |
train_input = torch.from_numpy(y[3:, :-1]) # (97, 999) | |
train_target = torch.from_numpy(y[3:, 1:]) # (97, 999) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test_input = torch.from_numpy(y[:3, :-1]) # (3, 999) | |
test_target = torch.from_numpy(y[:3, 1:]) # (3, 999) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model = LSTM() | |
criterion = nn.MSELoss() | |
optimiser = optim.LBFGS(model.parameters(), lr=0.08) |
OlderNewer