def test_model_updates(model, dataset): before = list(model.parameters())[0].clone().detach().cpu().numpy() cnn.train_model(model, dataset, epochs=1) after = list(model.parameters())[0].clone().detach().cpu().numpy() for b, a in zip(before, after): # Make sure something changed. assert (b != a).any()
def train(net, train_loader, dtype, device, parameters): """ Simplified training function for ax optimization. Args: net: Neural network object. train_loader: Training data loader. parameters (dict): Dictionary of parameter values. dtype: Data type. device: Device specifying cpu or gpu training. """ cnn.train_model(model=net, dataset=train_loader, dtype=dtype, device=device, optim_args={"lr": parameters["lr"]}, **parameters) return
def train(net, train_loader, dtype, device, parameters): """ Simplified training function for ax optimization. Args: net: Neural network object. train_loader: Training data loader. parameters (dict): Dictionary of parameter values. dtype: Data type. device: Device specifying cpu or gpu training. """ cnn.train_model( model=net, dataset=train_loader, optim_fn=optim.Adam, early_stop=False, epochs=parameters["epochs"], optim_args={ "lr": parameters["lr"], "momentum": parameters["momentum"] }, ) return
for i in range(REPEATS): print(f"Running trial {i+1}/{REPEATS}") net = cnn.NgnCnn(args.neurons, seed=i) for group in group_config: net_copy = copy.deepcopy(net) if "Dropout" in group: net_copy.dropout = 0.2 if "Neural Noise" in group: net_copy.neural_noise = (-0.2, 0.5) net_copy.to(device) parameters = group_config[group] log, _ = cnn.train_model( model=net_copy, dataset=data_loader, dtype=dtype, device=device, **parameters, ) accuracy = cnn.predict(net_copy, data_loader, device=device, valid=False) train_accuracy = cnn.predict(net_copy, data_loader, train=True) print(group, accuracy) results.iloc[counter] = [ group, train_accuracy["Accuracy"][0], accuracy["Accuracy"][0], i ] counter += 1