def test_lib(profile: str = './profiles/McDo_faultfree.json', nb_nets: int = 5): acc_dict = {} handler.from_json(profile) for scaling in SCALINGS: avg_list = [] for _ in range(nb_nets): handler.perturb_tensors(scaling) avg_list.append(utils.test_accuracy(net, testloader)) handler.restore() acc_dict[scaling] = avg_list return acc_dict
def test_legacy(profile: np.ndarray = np.zeros_like(lanmax_state_space), nb_nets: int = 5): acc_dict = {} wm.pis = profile for scaling in SCALINGS: avg_list = [] wm.scaling = scaling for _ in range(nb_nets): wm.binarize() avg_list.append(utils.test_accuracy(net, testloader)) wm.restore() acc_dict[scaling] = avg_list return acc_dict
batch_size=5, shuffle=True, num_workers=2) testset = utils.R2Dataset(2, 10000) testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2) # Loading Net net = utils.Xor() net.load_state_dict(torch.load(PATH)) # Initial Test init_accuracy = utils.test_accuracy(net, testloader) print('Initial accuracy of the network: %3.2f %%' % (100 * init_accuracy)) # Defining loss and optim criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) utils.train_net(net, optimizer, criterion, trainloader, nb_epochs=100, prt=False) # Final test accuracy = utils.test_accuracy(net, testloader)
for pert in pert_list: criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) net_copy = copy.deepcopy(net) handler = H.Handler(net_copy) handler.device = device pert.width = repr.width handler.add_net_parameters(representation=repr, perturb=pert) mse_list = handler.compute_MSE() min_mse_index = mse_list.index(min(mse_list)) net_copy.eval() init_acc = utils.test_accuracy(net_copy, testloader) handler_acc = utils.test_accuracy(handler, testloader) net_copy.train() utils.train_net(handler, optimizer, criterion, trainloader, 5, prt=False) init_trained_acc = utils.test_accuracy(net_copy, testloader) handler_trained_acc = utils.test_accuracy(handler, testloader) csv_data.append([ net.name,
"activations": { "net": null, "modules":null } } To unwrap this, let's go step by step. The "weights" section dictates which parameters (weights and biases) will be perturbed. You can specify an entire network under the "net" field, specific modules or layers in the "modules" list and specific parameters in the "tensors" list. These modules and parameters need to be in the net.named_modules() and net.named_parameters() respectively in order to be found by the constructor. Specifying representations and perturbations is done by giving the name of the desired class in the "name" field, and creating a field for each constructor argument. If no representation or perturbation is wanted, specifying null respectively will void that field. The same principles hold true for activations. Note: the saved file from handler.to_json() is not guaranteed to match the one used to load it, as there are multiple inputs that can give the same output, and for the time being we adress everything by tensor instead of looking to group tensors to simplify the saved file. """ import FaultyMemory.utils as utils accuracy = utils.test_accuracy(my_handler, testloader) config_path = './profiles/saved_handler.json' my_handler.from_json(config_path)