Skip to content

Commit b183319

Browse files
0.10.3
refining rnn
1 parent fccb68f commit b183319

2 files changed

Lines changed: 97 additions & 74 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotPython"
10-
version = "0.10.2"
10+
version = "0.10.3"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/light/regression/rnnlightregression.py

Lines changed: 96 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -142,63 +142,64 @@ def __init__(
142142
# set log_graph=True in Trainer to see the graph (in traintest.py)
143143
self.example_input_array = torch.zeros((batch_size, self._L_in))
144144

145-
# Initialize RNN
146-
# input_size = number of features (= 11)
147-
# num_layers=1: only a single RNN and not stacked
148-
rnn_units = 64 #self.hparams.l1
149-
fc_units = 64 # self.hparams.l1
150-
151-
# TODO: make this a hyperparameter
152-
rnn_nonlinearity = "relu"
153-
154-
self.rnn_layer = nn.RNN(
155-
input_size=self._L_in,
156-
hidden_size=rnn_units,
157-
num_layers=1,
158-
nonlinearity=rnn_nonlinearity,
159-
bias=True,
160-
batch_first=True,
161-
bidirectional=False,
162-
)
163-
164-
# Initialize Hidden- and Output-Layer
165-
self.fc = nn.Linear(rnn_units, fc_units)
166-
# self.output_layer = nn.Linear(fc_units, self._L_out)
167-
self.layers =nn.Linear(fc_units, self._L_out)
168-
169-
# Initialize Activation Function and Dropouts
170-
# self.dropout1 = nn.Dropout(dropout[0])
171-
# self.dropout2 = nn.Dropout(dropout[1])
172-
# self.dropout3 = nn.Dropout(dropout[2])
173-
# TODO: use different dropout for different layers
174-
self.dropout1 = nn.Dropout(self.hparams.dropout_prob)
175-
self.dropout2 = nn.Dropout(self.hparams.dropout_prob // 10.0)
176-
self.dropout3 = nn.Dropout(self.hparams.dropout_prob // 100.0)
177-
178-
activation_fct = nn.ReLU()
179-
self.activation_fct = activation_fct
145+
# # Initialize RNN
146+
# # input_size = number of features (= 11)
147+
# # num_layers=1: only a single RNN and not stacked
148+
# rnn_units = 64 #self.hparams.l1
149+
# fc_units = 64 # self.hparams.l1
150+
151+
# # TODO: make this a hyperparameter
152+
# rnn_nonlinearity = "relu"
153+
154+
# self.rnn_layer = nn.RNN(
155+
# input_size=self._L_in,
156+
# hidden_size=rnn_units,
157+
# num_layers=1,
158+
# nonlinearity=rnn_nonlinearity,
159+
# bias=True,
160+
# batch_first=True,
161+
# bidirectional=False,
162+
# )
163+
164+
# # Initialize Hidden- and Output-Layer
165+
# self.fc = nn.Linear(rnn_units, fc_units)
166+
# # self.output_layer = nn.Linear(fc_units, self._L_out)
167+
# self.layers =nn.Linear(fc_units, self._L_out)
168+
169+
# # Initialize Activation Function and Dropouts
170+
# # self.dropout1 = nn.Dropout(dropout[0])
171+
# # self.dropout2 = nn.Dropout(dropout[1])
172+
# # self.dropout3 = nn.Dropout(dropout[2])
173+
# # TODO: use different dropout for different layers
174+
# self.dropout1 = nn.Dropout(self.hparams.dropout_prob)
175+
# self.dropout2 = nn.Dropout(self.hparams.dropout_prob // 10.0)
176+
# self.dropout3 = nn.Dropout(self.hparams.dropout_prob // 100.0)
177+
178+
# activation_fct = nn.ReLU()
179+
# self.activation_fct = activation_fct
180180
# self.activation_fct = self.hparams.act_fn
181181

182+
# ###########################################
182183
# old:
183-
# if self.hparams.l1 < 4:
184-
# raise ValueError("l1 must be at least 4")
185-
186-
# hidden_sizes = [self.hparams.l1, self.hparams.l1 // 2, self.hparams.l1 // 2, self.hparams.l1 // 4]
187-
188-
# # Create the network based on the specified hidden sizes
189-
# layers = []
190-
# layer_sizes = [self._L_in] + hidden_sizes
191-
# layer_size_last = layer_sizes[0]
192-
# for layer_size in layer_sizes[1:]:
193-
# layers += [
194-
# nn.Linear(layer_size_last, layer_size),
195-
# self.hparams.act_fn,
196-
# nn.Dropout(self.hparams.dropout_prob),
197-
# ]
198-
# layer_size_last = layer_size
199-
# layers += [nn.Linear(layer_sizes[-1], self._L_out)]
200-
# # nn.Sequential summarizes a list of modules into a single module, applying them in sequence
201-
# self.layers = nn.Sequential(*layers)
184+
if self.hparams.l1 < 4:
185+
raise ValueError("l1 must be at least 4")
186+
187+
hidden_sizes = [self.hparams.l1, self.hparams.l1 // 2, self.hparams.l1 // 2, self.hparams.l1 // 4]
188+
189+
# Create the network based on the specified hidden sizes
190+
layers = []
191+
layer_sizes = [self._L_in] + hidden_sizes
192+
layer_size_last = layer_sizes[0]
193+
for layer_size in layer_sizes[1:]:
194+
layers += [
195+
nn.Linear(layer_size_last, layer_size),
196+
self.hparams.act_fn,
197+
nn.Dropout(self.hparams.dropout_prob),
198+
]
199+
layer_size_last = layer_size
200+
layers += [nn.Linear(layer_sizes[-1], self._L_out)]
201+
# nn.Sequential summarizes a list of modules into a single module, applying them in sequence
202+
self.layers = nn.Sequential(*layers)
202203

203204
def forward(self, x: torch.Tensor) -> torch.Tensor:
204205
"""
@@ -211,26 +212,32 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
211212
torch.Tensor: A tensor containing the output of the model.
212213
213214
"""
214-
# print(f"input: {x.shape}")
215-
x = self.dropout1(x)
216-
# print(f"dropout1: {x.shape}")
217-
x, _ = self.rnn_layer(x)
218-
# print(f"rnn_layer: {x.shape}")
219-
# x = x[:, -1, :]
220-
# print(f"slicing: {x.shape}")
221-
x = self.dropout2(x)
222-
# print(f"dropout2: {x.shape}")
223-
x = self.activation_fct(self.fc(x))
224-
# print(f"activation_fct: {x.shape}")
225-
x = self.dropout3(x)
226-
# print(f"dropout3: {x.shape}")
227-
x = self.output_layer(x)
228-
# print(f"output_layer: {x.shape}")
229-
return x
215+
# # print(f"input: {x.shape}")
216+
# x = self.dropout1(x)
217+
# # print(f"dropout1: {x.shape}")
218+
# x, _ = self.rnn_layer(x)
219+
# # print(f"rnn_layer: {x.shape}")
220+
# # x = x[:, -1, :]
221+
# # print(f"slicing: {x.shape}")
222+
# x = self.dropout2(x)
223+
# # print(f"dropout2: {x.shape}")
224+
# x = self.activation_fct(self.fc(x))
225+
# # print(f"activation_fct: {x.shape}")
226+
# x = self.dropout3(x)
227+
# # print(f"dropout3: {x.shape}")
228+
# x = self.output_layer(x)
229+
# # print(f"output_layer: {x.shape}")
230+
# return x
230231

231232
# old:
232-
# x = self.layers(x)
233-
# return x
233+
x = self.layers(x)
234+
# check if the number of columns in x is 1, otherwise throw an error
235+
try:
236+
assert x.shape[1] == 1
237+
except AssertionError:
238+
print(f"forward x.shape: {x.shape}")
239+
raise AssertionError("Number of columns in x is not 1.")
240+
return x
234241

235242
def training_step(self, batch: tuple) -> torch.Tensor:
236243
"""
@@ -244,8 +251,23 @@ def training_step(self, batch: tuple) -> torch.Tensor:
244251
245252
"""
246253
x, y = batch
254+
# reshape the tensor y to be a column vector (len(y) rows and 1 column)
247255
y = y.view(len(y), 1)
256+
# check if the number of rows in x is equal to the number of rows in y, otherwise throw an error
257+
try:
258+
assert x.shape[0] == y.shape[0]
259+
except AssertionError:
260+
print(f"training_step x.shape: {x.shape}")
261+
print(f"training_step y.shape: {y.shape}")
262+
raise AssertionError("Number of rows in x and y must be equal")
248263
y_hat = self(x)
264+
# check if the number of rows in y_hat is equal to the number of rows in y, otherwise throw an error
265+
try:
266+
assert y_hat.shape[0] == y.shape[0]
267+
except AssertionError:
268+
print(f"training_step y_hat.shape: {y_hat.shape}")
269+
print(f"training_step y.shape: {y.shape}")
270+
raise AssertionError("Number of rows in y_hat and y must be equal")
249271
val_loss = F.mse_loss(y_hat, y)
250272
# mae_loss = F.l1_loss(y_hat, y)
251273
# self.log("train_loss", val_loss, on_step=True, on_epoch=True, prog_bar=True)
@@ -266,6 +288,7 @@ def validation_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False)
266288
267289
"""
268290
x, y = batch
291+
# reshape the tensor y to be a column vector (len(y) rows and 1 column)
269292
y = y.view(len(y), 1)
270293
y_hat = self(x)
271294
val_loss = F.mse_loss(y_hat, y)
@@ -288,8 +311,8 @@ def test_step(self, batch: tuple, batch_idx: int, prog_bar: bool = False) -> tor
288311
torch.Tensor: A tensor containing the loss for this batch.
289312
"""
290313
x, y = batch
291-
y_hat = self(x)
292314
y = y.view(len(y), 1)
315+
y_hat = self(x)
293316
val_loss = F.mse_loss(y_hat, y)
294317
# mae_loss = F.l1_loss(y_hat, y)
295318
self.log("val_loss", val_loss, prog_bar=prog_bar)

0 commit comments

Comments
 (0)