Updated thesis

This commit is contained in:
2024-05-11 19:50:50 +02:00
parent db8a527949
commit 96e4ed042c
24 changed files with 606 additions and 25 deletions

View File

@@ -1,6 +1,7 @@
import torch
import torch.nn as nn
class DiffusionModel(nn.Module):
def __init__(self, time_dim: int = 64):
super(DiffusionModel, self).__init__()
@@ -16,7 +17,6 @@ class DiffusionModel(nn.Module):
pos_enc = torch.cat((pos_enc_a, pos_enc_b), dim=-1)
return pos_enc
def forward(self, x, t, inputs):
t = t.unsqueeze(-1).type(torch.float)
t = self.pos_encoding(t, self.time_dim)
@@ -31,33 +31,61 @@ class DiffusionModel(nn.Module):
x = self.layers[-1](x)
return x
class SimpleDiffusionModel(DiffusionModel):
def __init__(self, input_size: int, hidden_sizes: list, other_inputs_dim: int, time_dim: int = 64):
def __init__(
self,
input_size: int,
hidden_sizes: list,
other_inputs_dim: int,
time_dim: int = 64,
dropout_rate: float = 0.2,
):
super(SimpleDiffusionModel, self).__init__(time_dim)
self.other_inputs_dim = other_inputs_dim
self.layers.append(nn.Linear(input_size + time_dim + other_inputs_dim, hidden_sizes[0]))
self.layers.append(
nn.Linear(input_size + time_dim + other_inputs_dim, hidden_sizes[0])
)
self.layers.append(nn.ReLU())
self.layers.append(nn.Dropout(dropout_rate))
for i in range(1, len(hidden_sizes)):
self.layers.append(nn.Linear(hidden_sizes[i - 1] + time_dim + other_inputs_dim, hidden_sizes[i]))
self.layers.append(
nn.Linear(
hidden_sizes[i - 1] + time_dim + other_inputs_dim, hidden_sizes[i]
)
)
self.layers.append(nn.ReLU())
self.layers.append(nn.Dropout(dropout_rate))
self.layers.append(
nn.Linear(hidden_sizes[-1] + time_dim + other_inputs_dim, input_size)
)
self.layers.append(nn.Linear(hidden_sizes[-1] + time_dim + other_inputs_dim, input_size))
class GRUDiffusionModel(DiffusionModel):
def __init__(self, input_size: int, hidden_sizes: list, other_inputs_dim: int, gru_hidden_size: int, time_dim: int = 64):
def __init__(
self,
input_size: int,
hidden_sizes: list,
other_inputs_dim: int,
gru_hidden_size: int,
time_dim: int = 64,
):
super(GRUDiffusionModel, self).__init__(time_dim)
self.other_inputs_dim = other_inputs_dim
self.gru_hidden_size = gru_hidden_size
# GRU layer
self.gru = nn.GRU(input_size=input_size + time_dim + other_inputs_dim,
hidden_size=gru_hidden_size,
num_layers=3,
batch_first=True)
self.gru = nn.GRU(
input_size=input_size + time_dim + other_inputs_dim,
hidden_size=gru_hidden_size,
num_layers=3,
batch_first=True,
)
# Fully connected layers after GRU
self.fc_layers = nn.ModuleList()
@@ -76,16 +104,20 @@ class GRUDiffusionModel(DiffusionModel):
# Positional encoding for each time step
t = t.unsqueeze(-1).type(torch.float)
t = self.pos_encoding(t, self.time_dim) # Shape: [batch_size, seq_len, time_dim]
t = self.pos_encoding(
t, self.time_dim
) # Shape: [batch_size, seq_len, time_dim]
# repeat time encoding for each time step t is shape [batch_size, time_dim], i want [batch_size, seq_len, time_dim]
t = t.unsqueeze(1).repeat(1, seq_len, 1)
# Concatenate x, t, and inputs along the feature dimension
x = torch.cat((x, t, inputs), dim=-1) # Shape: [batch_size, seq_len, input_size + time_dim + other_inputs_dim]
x = torch.cat(
(x, t, inputs), dim=-1
) # Shape: [batch_size, seq_len, input_size + time_dim + other_inputs_dim]
# Pass through GRU
output, hidden = self.gru(x) # Hidden Shape: [batch_size, seq_len, 1]
output, hidden = self.gru(x) # Hidden Shape: [batch_size, seq_len, 1]
# Get last hidden state
x = hidden[-1]
@@ -94,4 +126,4 @@ class GRUDiffusionModel(DiffusionModel):
for layer in self.fc_layers:
x = layer(x)
return x
return x

View File

@@ -44,8 +44,8 @@ class PolicyEvaluator:
date,
idx_samples,
test_loader,
charge_thresholds=np.arange(-1000, 1000, 5),
discharge_thresholds=np.arange(-1000, 1000, 5),
charge_thresholds=np.arange(-1000, 1000, 100),
discharge_thresholds=np.arange(-1000, 1000, 100),
penalty: int = 0,
state_of_charge: float = 0.0,
):

View File

@@ -52,7 +52,7 @@ data_processor.set_full_day_skip(False)
#### Hyperparameters ####
data_processor.set_output_size(1)
inputDim = data_processor.get_input_size()
epochs = 16
epochs = 300
# add parameters to clearml
quantiles = task.get_parameter("general/quantiles", cast=True)
@@ -142,9 +142,9 @@ optimal_penalty, profit, charge_cycles = (
policy_evaluator.optimize_penalty_for_target_charge_cycles(
idx_samples=idx_samples,
test_loader=test_loader,
initial_penalty=1000,
initial_penalty=20000,
target_charge_cycles=283,
initial_learning_rate=3,
initial_learning_rate=13,
max_iterations=150,
tolerance=1,
)