-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmarkMethods.py
More file actions
79 lines (59 loc) · 2.42 KB
/
benchmarkMethods.py
File metadata and controls
79 lines (59 loc) · 2.42 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
import torch.nn as nn
import torch
import numpy as np
class LSTMModel(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, output_size, dropout=0.5):
super(LSTMModel, self).__init__()
self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, dropout=dropout)
self.fc = nn.Linear(hidden_size, output_size)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
# LSTM forward pass
lstm_out, _ = self.lstm(x)
# Apply dropout
last_out = self.dropout(lstm_out)
# Fully connected layer on the last time step's output
out = self.fc(last_out)
return out
def LSTM_train(num_epochs, data_loader, model, loss_function, optimizer, verbose=True):
ls = []
model.train()
for epoch in range(num_epochs):
epoch_loss = 0
for inputs, targets in data_loader:
# Reset gradients
optimizer.zero_grad()
# Forward pass
outputs = model(inputs)
# Compute loss
loss = loss_function(outputs, targets)
# Backward pass and optimization
loss.backward()
optimizer.step()
# Accumulate loss
epoch_loss += loss.item()
# Save loss for the epoch
ls.append(epoch_loss / len(data_loader))
if verbose:
# Print average loss for the epoch
print(f'Epoch {epoch+1}/{num_epochs}, Loss: {epoch_loss / len(data_loader)}')
return model, ls
def MC_LSTM(model, x, num_runs=30):
# Set model to training mode to enable dropout during inference
model.train()
# Collect predictions
predictions = []
# Perform Monte Carlo simulations
with torch.no_grad():
for _ in range(num_runs):
predictions.append(model(x).numpy())
# Convert predictions to a numpy array
predictions = np.array(predictions)
# Calculate mean and standard deviation
mean_pred = np.mean(predictions, axis=0)
std_dev_pred = np.std(predictions, axis=0)
# Calculate 95% confidence interval
z_score = 1.96 # Z-score for 95% confidence interval
lower_bound = mean_pred - z_score * std_dev_pred
upper_bound = mean_pred + z_score * std_dev_pred
return mean_pred, lower_bound, upper_bound, std_dev_pred