Skip to content

Commit

Permalink
Fix model out_ch and use maxfreq=10kHz data
Browse files Browse the repository at this point in the history
  • Loading branch information
ange1a-j14 committed Aug 18, 2024
1 parent d8ea54f commit 2e2d8b2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
4 changes: 2 additions & 2 deletions decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 9 additions & 4 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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

18 changes: 10 additions & 8 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2e2d8b2

Please sign in to comment.