From 2e2d8b283a93d5c8d38058d25a3fd9c06dcc4c09 Mon Sep 17 00:00:00 2001 From: Angela Yueran Jia Date: Sun, 18 Aug 2024 15:03:28 -0700 Subject: [PATCH] Fix model out_ch and use maxfreq=10kHz data --- decoder.py | 4 ++-- main.py | 8 ++++---- models.py | 13 +++++++++---- train.py | 18 ++++++++++-------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/decoder.py b/decoder.py index 91fd559..dcfaa80 100644 --- a/decoder.py +++ b/decoder.py @@ -52,7 +52,7 @@ def configure_optimizers(self): # We will reduce the learning rate by 0.1 after 100 and 150 epochs scheduler = optim.lr_scheduler.MultiStepLR(optimizer, - milestones=[1000, 1500, 1800], + milestones=[100, 150, 200], gamma=0.1) return [optimizer], [scheduler] @@ -83,7 +83,7 @@ def validation_step(self, batch, batch_idx): loss = self.loss_function(preds, y) acc = (preds == y).float().mean() self.log("val_acc", acc, on_step=False, on_epoch=True) - self.log("val_loss", loss, prog_bar=True) + self.log("val_loss", loss, prog_bar=False) return loss def test_step(self, batch, batch_idx): diff --git a/main.py b/main.py index c5462c7..0f2949d 100644 --- a/main.py +++ b/main.py @@ -13,12 +13,12 @@ if len(sys.argv) == 1: """Run functions in this scratch area. """ - valid_file = 'C:\\Users\\aj14\\Desktop\\SMI\\data\\valid_30to1kHz_2kshots_dec=256_randampl.h5py' - train_file = 'C:\\Users\\aj14\\Desktop\\SMI\\data\\training_30to1kHz_10kshots_dec=256_randampl.h5py' - test_file = 'C:\\Users\\aj14\\Desktop\\SMI\\data\\test_30to1kHz_2kshots_dec=256_randampl.h5py' + valid_file = 'C:\\Users\\aj14\\Desktop\\SMI\\data\\valid_max10kHz_30to1kHz_2kshots_dec=256_randampl.h5py' + train_file = 'C:\\Users\\aj14\\Desktop\\SMI\\data\\training_max10kHz_30to1kHz_10kshots_dec=256_randampl.h5py' + test_file = 'C:\\Users\\aj14\\Desktop\\SMI\\data\\test_max10kHz_30to1kHz_2kshots_dec=256_randampl.h5py' print('begin main', datetime.datetime.now()) - step_list = [256, 128, 64] # step sizes for rolling input + step_list = [256, 128, 64, 32] # step sizes for rolling input for step in step_list: runner = train.TrainingRunner(train_file, valid_file, test_file, step) runner.scan_hyperparams() diff --git a/models.py b/models.py index 4a0b8c8..a64a044 100644 --- a/models.py +++ b/models.py @@ -8,6 +8,7 @@ class CNN(nn.Module): def __init__(self, input_size, output_size, ch_in=1, activation='LeakyReLU'): super(CNN, self).__init__() + self.ch_in = ch_in self.conv_layers = nn.Sequential( nn.Conv1d(ch_in, 16, kernel_size=7), # Lout = 250, given L = 256 act_fn_by_name[activation], @@ -19,18 +20,22 @@ def __init__(self, input_size, output_size, ch_in=1, activation='LeakyReLU'): act_fn_by_name[activation], nn.MaxPool1d(2), # Lout = 26, given L = 53 nn.Dropout(0.1), - nn.Conv1d(64, output_size, kernel_size=7), # Lout = 20, given L = 26 + nn.Conv1d(64, 64, kernel_size=7), # Lout = 20, given L = 26 act_fn_by_name[activation], nn.MaxPool1d(2) # Lout = 10, given L = 20 ) self.fc_layers = nn.Sequential( - nn.Linear(10, 16), + nn.Linear(640, 16), nn.ReLU(), nn.Linear(16, output_size) ) def forward(self, x): - out = self.conv_layers(x) # expect out [128*num_groups, 10] - out = self.fc_layers(out) # expect out [128*num_groups, 1] + out = self.conv_layers(x) + # print(f"post conv out size: {out.size()}") # [128, 64, 10] + out = out.view(out.size(0), self.ch_in, -1) + # print(f"post conv out reshaped size: {out.size()}") # confirmed [128, 1, 640] + out = self.fc_layers(out) # expect out [128, 1, 1] + # print(f"post fc out size: {out.size()}") # confirmed: [128, 1, 1] return out \ No newline at end of file diff --git a/train.py b/train.py index 1403b98..4da2568 100644 --- a/train.py +++ b/train.py @@ -56,8 +56,8 @@ def open_hdf5(self, rolling=True, step=256, group_size=256, ch_in = 1): start_idxs = torch.arange(num_groups) * step # starting indices for each group idxs = torch.arange(group_size)[:, None] + start_idxs idxs = torch.transpose(idxs, dim0=0, dim1=1) # indices in shape [num_groups, group_size] - self.inputs = torch.cat(list(pds[:, idxs]), dim=0) # [num_shots * num_groups, group_size] - grouped_vels = torch.cat(list(vels[:, idxs]), dim=0) # [num_shots * num_groups, group_size] + self.inputs = pds[:, idxs].reshape(-1, group_size) # [num_shots * num_groups, group_size] + grouped_vels = vels[:, idxs].reshape(-1, group_size) # [num_shots * num_groups, group_size] self.targets = torch.unsqueeze(torch.mean(grouped_vels, dim=1), dim=1) # [num_shots * num_groups, 1] else: # STEP INPUT @@ -72,8 +72,8 @@ def open_hdf5(self, rolling=True, step=256, group_size=256, ch_in = 1): assert False, 'ch > 1 not implemented' # total number of group_size length sequences = num_shots * num_groups - # print(self.inputs.size()) # [10k*64, 256] - # print(self.targets.size()) # [10k*64, 1] + # print("open_hdf5 input size", self.inputs.size()) # [self.length, 256] + # print("open_hdf5 target size", self.targets.size()) # [self.length, 1] def __len__(self): # print("__len__:", self.length) @@ -106,7 +106,9 @@ def __init__(self, training_h5, validation_h5, testing_h5, step=256, # get dataloaders self.set_dataloaders() print("dataloaders set:", datetime.datetime.now()) - input_ref = next(iter(self.train_loader)) + iter_train_loader = iter(self.train_loader) + print("loader len: ", len(iter_train_loader)) + input_ref = next(iter_train_loader) # print("loaded next(iter", datetime.datetime.now()) self.input_size = input_ref[0].shape[2] # group_size self.output_size = input_ref[1].shape[2] # 1 @@ -196,9 +198,9 @@ def train_model(self, model_name, save_name=None, **kwargs): return model, result def scan_hyperparams(self): - lr_list = [1e-3, 1e-4] - act_list = ['LeakyReLU', 'ReLU'] - optim_list = ['Adam', 'SGD'] + lr_list = [1e-3, 1e-4] # [1e-3, 1e-4, 1e-5] + act_list = ['LeakyReLU'] #, 'ReLU'] + optim_list = ['Adam'] #, 'SGD'] for lr, activation, optim in product(lr_list, act_list, optim_list): #, 1e-2, 3e-2]: model_config = {"input_size": self.input_size, "output_size": self.output_size,