def generate_point(net, testloader, prob): # DEPRECATED """ This function is deprecated because of the use of clusters Handlers now store tensor information themselves """ net_pert = copy.deepcopy(net) pert_pert = P.Zeros(prob) c_pert = C.Cluster([pert_pert], networks=[net_pert]) handler_pert = H.Handler(net_pert, [c_pert]) net_acti = copy.deepcopy(net) pert_acti = P.Zeros(prob) c_acti = C.Cluster([pert_acti], network_activations=[net_acti]) handler_acti = H.Handler(net_acti, [c_acti]) net_both = copy.deepcopy(net) pert_both = P.Zeros(prob) c_both = C.Cluster([pert_both], networks=[net_both], network_activations=[net_both]) handler_both = H.Handler(net_both, [c_both]) clean_accuracy = test_accuracy(net, testloader) pert_accuracy = test_accuracy(handler_pert, testloader) acti_accuracy = test_accuracy(handler_acti, testloader) both_accuracy = test_accuracy(handler_both, testloader) return clean_accuracy, pert_accuracy, acti_accuracy, both_accuracy
"Pert", "Repr", "Init acc", "Handler", "Init Trained", "Handler trained", ]] for net in net_list: for repr in repr_list: 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,
root='./data', train=False, download=True, transform=transform) testloader = torch.utils.data.DataLoader( testset, batch_size=512, shuffle=False, num_workers=2) # init params: depth=28, width=10 net = McDo.WRN_McDonnell( depth=28, width=10, num_classes=10, dropit=False, actprec=3).to(device) state_dict = torch.load(PATH, map_location=device)[ 'model_state_dict'] lanmax_state_space = torch.load(PATH, map_location=device)[ 'lanmax_state_space'] net.load_state_dict(state_dict) net.device = device net.eval() handler = H.Handler(net) handler.from_json('./profiles/McDo.json') wm = BinaryWeightMemory(net, p=0.01, scaling=SCALING) wm.pis = lanmax_state_space def iter_weights(): weights = [] for m in net.modules(): if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear): y = np.bincount(m.weight.data.numpy().flatten().astype(int) + 2) ii = np.nonzero(y)[0] print(list(zip(ii, y[ii]))) weights.append(copy.deepcopy(m.weight.data.numpy())) return weights
x = F.relu(self.fc2(x)) x = self.fc3(x) return x net = McDo.WRN_McDonnell(depth=28, width=10, num_classes=10, dropit=False, actprec=3) # net = SimpleNet() nb_clusters = 4 cluster_list = [C.Cluster() for _ in range(nb_clusters)] print(cluster_list) handler = H.Handler(net, cluster_list) handler.from_json('./profiles/McDo.json') handler.assign_clusters() # handler.to_json('./profiles/saved_handler.json') exit() inp = torch.tensor([1.]) out = handler(inp) print(handler) print('out: ', out) """ def confint_mean_testset():