From 2097b4e26ddb0d2b2520ce52ac18e0167d58086c Mon Sep 17 00:00:00 2001
From: Joerg Martin <joerg.martin@ptb.de>
Date: Mon, 13 Dec 2021 17:09:03 +0100
Subject: [PATCH] Allowed for intervals between std_y update

---
 .../configurations/eiv_california.json        |  2 +-
 Experiments/configurations/eiv_concrete.json  |  2 +-
 Experiments/configurations/eiv_energy.json    |  2 +-
 Experiments/configurations/eiv_kin8nm.json    |  2 +-
 Experiments/configurations/eiv_msd.json       |  2 +-
 Experiments/configurations/eiv_naval.json     |  2 +-
 Experiments/configurations/eiv_power.json     |  2 +-
 Experiments/configurations/eiv_protein.json   |  2 +-
 Experiments/configurations/eiv_wine.json      |  2 +-
 Experiments/configurations/eiv_yacht.json     |  2 +-
 .../configurations/noneiv_california.json     |  2 +-
 .../configurations/noneiv_concrete.json       |  2 +-
 Experiments/configurations/noneiv_energy.json |  2 +-
 Experiments/configurations/noneiv_kin8nm.json |  2 +-
 Experiments/configurations/noneiv_msd.json    |  2 +-
 Experiments/configurations/noneiv_naval.json  |  2 +-
 Experiments/configurations/noneiv_power.json  |  2 +-
 .../configurations/noneiv_protein.json        |  2 +-
 Experiments/configurations/noneiv_wine.json   |  2 +-
 Experiments/configurations/noneiv_yacht.json  |  2 +-
 Experiments/train_eiv.py                      | 27 +++++++++++++++----
 Experiments/train_noneiv.py                   | 27 +++++++++++++++----
 22 files changed, 64 insertions(+), 30 deletions(-)

diff --git a/Experiments/configurations/eiv_california.json b/Experiments/configurations/eiv_california.json
index 29a7162..4786d5b 100644
--- a/Experiments/configurations/eiv_california.json
+++ b/Experiments/configurations/eiv_california.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.1,
 	"lr_update": 20,
-	"epoch_offset": 10,
+	"std_y_update_points": 10,
 	"eiv_prediction_number_of_draws": 100,
 	"eiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/eiv_concrete.json b/Experiments/configurations/eiv_concrete.json
index 11121c1..0e23434 100644
--- a/Experiments/configurations/eiv_concrete.json
+++ b/Experiments/configurations/eiv_concrete.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 20,
-	"epoch_offset": 10,
+	"std_y_update_points": 10,
 	"eiv_prediction_number_of_draws": 100,
 	"eiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/eiv_energy.json b/Experiments/configurations/eiv_energy.json
index bca6774..f13ab1b 100644
--- a/Experiments/configurations/eiv_energy.json
+++ b/Experiments/configurations/eiv_energy.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 100,
-	"epoch_offset": 100,
+	"std_y_update_points": 100,
 	"eiv_prediction_number_of_draws": 100,
 	"eiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/eiv_kin8nm.json b/Experiments/configurations/eiv_kin8nm.json
index fa3718a..df2e4ba 100644
--- a/Experiments/configurations/eiv_kin8nm.json
+++ b/Experiments/configurations/eiv_kin8nm.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 20,
-	"epoch_offset": 19,
+	"std_y_update_points": 19,
 	"eiv_prediction_number_of_draws": 100,
 	"eiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/eiv_msd.json b/Experiments/configurations/eiv_msd.json
index 1c25276..a738f2e 100644
--- a/Experiments/configurations/eiv_msd.json
+++ b/Experiments/configurations/eiv_msd.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 4,
-	"epoch_offset": 4,
+	"std_y_update_points": 4,
 	"eiv_prediction_number_of_draws": 100,
 	"eiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/eiv_naval.json b/Experiments/configurations/eiv_naval.json
index 3358831..092f4f8 100644
--- a/Experiments/configurations/eiv_naval.json
+++ b/Experiments/configurations/eiv_naval.json
@@ -9,7 +9,7 @@
         "report_point": 5,
         "p": 0.2,
         "lr_update": 20,
-        "epoch_offset": 20,
+        "std_y_update_points": 20,
 	"eiv_prediction_number_of_draws": 100,
 	"eiv_prediction_number_of_batches": 10,
         "init_std_y_list": [0.5],
diff --git a/Experiments/configurations/eiv_power.json b/Experiments/configurations/eiv_power.json
index 842f6e2..ab5649f 100644
--- a/Experiments/configurations/eiv_power.json
+++ b/Experiments/configurations/eiv_power.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 10,
-	"epoch_offset": 15,
+	"std_y_update_points": 15,
 	"eiv_prediction_number_of_draws": 100,
 	"eiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/eiv_protein.json b/Experiments/configurations/eiv_protein.json
index 97f5946..9623799 100644
--- a/Experiments/configurations/eiv_protein.json
+++ b/Experiments/configurations/eiv_protein.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 10,
-	"epoch_offset": 10,
+	"std_y_update_points": 10,
 	"eiv_prediction_number_of_draws": 100,
 	"eiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/eiv_wine.json b/Experiments/configurations/eiv_wine.json
index 0b40a61..f0beaf4 100644
--- a/Experiments/configurations/eiv_wine.json
+++ b/Experiments/configurations/eiv_wine.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 30,
-	"epoch_offset": 50,
+	"std_y_update_points": 50,
 	"eiv_prediction_number_of_draws": 100,
 	"eiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/eiv_yacht.json b/Experiments/configurations/eiv_yacht.json
index c668113..f95841b 100644
--- a/Experiments/configurations/eiv_yacht.json
+++ b/Experiments/configurations/eiv_yacht.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 200,
-	"epoch_offset": 20,
+	"std_y_update_points": [20,20],
 	"eiv_prediction_number_of_draws": 100,
 	"eiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/noneiv_california.json b/Experiments/configurations/noneiv_california.json
index 5005b43..7c8f468 100644
--- a/Experiments/configurations/noneiv_california.json
+++ b/Experiments/configurations/noneiv_california.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.1,
 	"lr_update": 20,
-	"epoch_offset": 0 ,
+	"std_y_update_points": 0 ,
 	"noneiv_prediction_number_of_draws": 100,
 	"noneiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/noneiv_concrete.json b/Experiments/configurations/noneiv_concrete.json
index 66552b7..15e28c5 100644
--- a/Experiments/configurations/noneiv_concrete.json
+++ b/Experiments/configurations/noneiv_concrete.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 20,
-	"epoch_offset": 10,
+	"std_y_update_points": 10,
 	"noneiv_prediction_number_of_draws": 100,
 	"noneiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/noneiv_energy.json b/Experiments/configurations/noneiv_energy.json
index 74eb45c..18c068e 100644
--- a/Experiments/configurations/noneiv_energy.json
+++ b/Experiments/configurations/noneiv_energy.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 100,
-	"epoch_offset": 100,
+	"std_y_update_points": 100,
 	"noneiv_prediction_number_of_draws": 100,
 	"noneiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/noneiv_kin8nm.json b/Experiments/configurations/noneiv_kin8nm.json
index 22615f6..8ecf817 100644
--- a/Experiments/configurations/noneiv_kin8nm.json
+++ b/Experiments/configurations/noneiv_kin8nm.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 20,
-	"epoch_offset": 19,
+	"std_y_update_points": 19,
 	"noneiv_prediction_number_of_draws": 100,
 	"noneiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/noneiv_msd.json b/Experiments/configurations/noneiv_msd.json
index 1c4f338..a0e2f73 100644
--- a/Experiments/configurations/noneiv_msd.json
+++ b/Experiments/configurations/noneiv_msd.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 4,
-	"epoch_offset": 4,
+	"std_y_update_points": 4,
 	"noneiv_prediction_number_of_draws": 100,
 	"noneiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/noneiv_naval.json b/Experiments/configurations/noneiv_naval.json
index 5dea6c4..2da83b7 100644
--- a/Experiments/configurations/noneiv_naval.json
+++ b/Experiments/configurations/noneiv_naval.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 20,
-	"epoch_offset": 20,
+	"std_y_update_points": 20,
 	"noneiv_prediction_number_of_draws": 100,
 	"noneiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/noneiv_power.json b/Experiments/configurations/noneiv_power.json
index 6e524e6..48aac9d 100644
--- a/Experiments/configurations/noneiv_power.json
+++ b/Experiments/configurations/noneiv_power.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 10,
-	"epoch_offset": 15,
+	"std_y_update_points": 15,
 	"noneiv_prediction_number_of_draws": 100,
 	"noneiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/noneiv_protein.json b/Experiments/configurations/noneiv_protein.json
index 1edd04f..454deae 100644
--- a/Experiments/configurations/noneiv_protein.json
+++ b/Experiments/configurations/noneiv_protein.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 10,
-	"epoch_offset": 10,
+	"std_y_update_points": 10,
 	"noneiv_prediction_number_of_draws": 100,
 	"noneiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/noneiv_wine.json b/Experiments/configurations/noneiv_wine.json
index 717fb33..ddd2199 100644
--- a/Experiments/configurations/noneiv_wine.json
+++ b/Experiments/configurations/noneiv_wine.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 30,
-	"epoch_offset": 50,
+	"std_y_update_points": 50,
 	"noneiv_prediction_number_of_draws": 100,
 	"noneiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/configurations/noneiv_yacht.json b/Experiments/configurations/noneiv_yacht.json
index edb775c..1a89921 100644
--- a/Experiments/configurations/noneiv_yacht.json
+++ b/Experiments/configurations/noneiv_yacht.json
@@ -9,7 +9,7 @@
 	"report_point": 5,
 	"p": 0.2,
 	"lr_update": 200,
-	"epoch_offset": 20,
+	"std_y_update_points": 20,
 	"noneiv_prediction_number_of_draws": 100,
 	"noneiv_prediction_number_of_batches": 10,
 	"init_std_y_list": [0.5],
diff --git a/Experiments/train_eiv.py b/Experiments/train_eiv.py
index 78f6d61..bb7c900 100644
--- a/Experiments/train_eiv.py
+++ b/Experiments/train_eiv.py
@@ -40,10 +40,11 @@ report_point = conf_dict["report_point"]
 p = conf_dict["p"]
 lr_update = conf_dict["lr_update"]
 # offset before updating sigma_y after each epoch
-epoch_offset = conf_dict["epoch_offset"]
+std_y_update_points = conf_dict["std_y_update_points"]
 # will be used to predict the RMSE and update sigma_y accordingly
 eiv_prediction_number_of_draws = conf_dict["eiv_prediction_number_of_draws"]
-eiv_prediction_number_of_batches = conf_dict["eiv_prediction_number_of_batches"]
+eiv_prediction_number_of_batches = \
+        conf_dict["eiv_prediction_number_of_batches"]
 init_std_y_list = conf_dict["init_std_y_list"]
 fixed_std_x = conf_dict['fixed_std_x']
 gamma = conf_dict["gamma"]
@@ -54,7 +55,8 @@ print(f"Training on {long_dataname} data")
 
 try:
     gpu_number = conf_dict["gpu_number"]
-    device = torch.device(f'cuda:{gpu_number}' if torch.cuda.is_available() else 'cpu')
+    device = torch.device(f'cuda:{gpu_number}' if torch.cuda.is_available()
+            else 'cpu')
 except KeyError:
     device = torch.device('cpu')
 
@@ -65,7 +67,7 @@ seed_list = range(seed_range[0], seed_range[1])
 
 def set_seeds(seed):
     torch.backends.cudnn.benchmark = False
-    np.random.seed(seed) 
+    np.random.seed(seed)
     random.seed(seed) 
     torch.manual_seed(seed)
 
@@ -88,7 +90,22 @@ class UpdatedTrainEpoch(train_and_store.TrainEpoch):
         """
         Overwrites the corresponding method
         """
-        if epoch >= epoch_offset:
+        def update_std_y(epoch_number):
+            """
+            Check whether to update std_y according to `epoch_number` and
+            `std_y_update_points`. If the later is an integer, after all epochs
+            greater than this number an update will be made (i.e. `True` will
+            be returned). If it is a list, only `epoch_number` greater than
+            `std_y_update_points[0]` that divide `std_y_update_points[1]` will
+            result in a True.
+            """
+            if type(std_y_update_points) is int:
+                return epoch >= std_y_update_points
+            else:
+                assert type(std_y_update_points) is list
+                return epoch_number >= std_y_update_points[0]\
+                            and epoch_number % std_y_update_points[1] == 0
+        if update_std_y(epoch):
             pred_collection = []
             y_collection = []
             for i, (x,y) in  enumerate(self.train_dataloader):
diff --git a/Experiments/train_noneiv.py b/Experiments/train_noneiv.py
index e63dbf2..8b89296 100644
--- a/Experiments/train_noneiv.py
+++ b/Experiments/train_noneiv.py
@@ -40,10 +40,11 @@ report_point = conf_dict["report_point"]
 p = conf_dict["p"]
 lr_update = conf_dict["lr_update"]
 # offset before updating sigma_y after each epoch
-epoch_offset = conf_dict["epoch_offset"]
+std_y_update_points = conf_dict["std_y_update_points"]
 # will be used to predict the RMSE and update sigma_y accordingly
 noneiv_prediction_number_of_draws = conf_dict["noneiv_prediction_number_of_draws"]
-noneiv_prediction_number_of_batches = conf_dict["noneiv_prediction_number_of_batches"]
+noneiv_prediction_number_of_batches = \
+    conf_dict["noneiv_prediction_number_of_batches"]
 init_std_y_list = conf_dict["init_std_y_list"]
 gamma = conf_dict["gamma"]
 hidden_layers = conf_dict["hidden_layers"]
@@ -53,7 +54,8 @@ print(f"Training on {long_dataname} data")
 
 try:
     gpu_number = conf_dict["gpu_number"]
-    device = torch.device(f'cuda:{gpu_number}' if torch.cuda.is_available() else 'cpu')
+    device = torch.device(f'cuda:{gpu_number}' if torch.cuda.is_available()
+            else 'cpu')
 except KeyError:
     device = torch.device('cpu')
 
@@ -87,11 +89,26 @@ class UpdatedTrainEpoch(train_and_store.TrainEpoch):
         """
         Overwrites the corresponding method
         """
-        if epoch >= epoch_offset:
+        def update_std_y(epoch_number):
+            """
+            Check whether to update std_y according to `epoch_number` and
+            `std_y_update_points`. If the later is an integer, after all epochs
+            greater than this number an update will be made (i.e. `True` will
+            be returned). If it is a list, only `epoch_number` greater than
+            `std_y_update_points[0]` that divide `std_y_update_points[1]` will
+            result in a True.
+            """
+            if type(std_y_update_points) is int:
+                return epoch >= std_y_update_points
+            else:
+                assert type(std_y_update_points) is list
+                return epoch_number >= std_y_update_points[0]\
+                            and epoch_number % std_y_update_points[1] == 0
+        if update_std_y(epoch):
             pred_collection = []
             y_collection = []
             for i, (x,y) in  enumerate(self.train_dataloader):
-                if i>= noneiv_prediction_number_of_batches:
+                if i >= noneiv_prediction_number_of_batches:
                     break
                 if len(y.shape) <= 1:
                     y = y.view((-1,1))
-- 
GitLab