Exemplo n.º 1
0
class FL_client():
    def __init__(self, args):
        if args.dataset == 'cifar':
            self.net = CNNCifar(args=args).to(args.device)
        else:
            self.net = CNNMnist(args=args).to(args.device)
        self.net.train()
        self.loss_func = nn.CrossEntropyLoss()
        self.optimizer = torch.optim.SGD(self.net.parameters(), lr=args.lr)
        self.args = args
        self.w_glob = []
        # key exchange
        self.x = self.gx = 0
        self.keys = defaultdict(int)

    def set_data(self, dataset, idxs):
        self.data = DataLoader(DatasetSplit(dataset, idxs),
                               batch_size=self.args.local_bs,
                               shuffle=True)

    def load_state(self, state_dict):
        self.net.load_state_dict(state_dict)

    def train(self):
        epoch_loss = []
        for _ in range(self.args.local_ep):
            batch_loss = []
            for _, (images, labels) in enumerate(self.data):
                images, labels = images.to(self.args.device), labels.to(
                    self.args.device)
                pred = self.net(images)
                loss = self.loss_func(pred, labels)
                self.optimizer.zero_grad()
                loss.backward()
                self.optimizer.step()
                batch_loss.append(loss.item())
            epoch_loss.append(sum(batch_loss) / len(batch_loss))
        return self.net.state_dict(), sum(epoch_loss) / len(epoch_loss)
Exemplo n.º 2
0
        net_glob15 = customCNNCifar(args=args).to(args.device)
        net_glob20 = customCNNCifar(args=args).to(args.device)
        net_glob25 = customCNNCifar(args=args).to(args.device)
        net_glob30 = customCNNCifar(args=args).to(args.device)
    elif args.model == 'cnn' and args.dataset == 'mnist':
        net_glob = CNNMnist(args=args).to(args.device)

    elif args.model == 'mlp':
        len_in = 1
        for x in img_size:
            len_in *= x
        net_glob = MLP(dim_in=len_in, dim_hidden=64, dim_out=args.num_classes).to(args.device)
    else:
        exit('Error: unrecognized model')
    print(net_glob)
    net_glob.train()
    net_glob1.train()
    net_glob5.train()
    net_glob10.train()
    net_glob15.train()
    net_glob20.train()
    net_glob25.train()
    net_glob30.train()

    # copy weights
    w_glob = net_glob.state_dict()
    w_glob1 = net_glob1.state_dict()
    w_glob5 = net_glob5.state_dict()
    w_glob10 = net_glob10.state_dict()
    w_glob15 = net_glob15.state_dict()
    w_glob20 = net_glob20.state_dict()
Exemplo n.º 3
0
        net_glob10 = CNNMnist(args=args).to(args.device)
        net_glob15 = CNNMnist(args=args).to(args.device)
        net_glob20 = CNNMnist(args=args).to(args.device)
        net_glob25 = CNNMnist(args=args).to(args.device)
        net_glob30 = CNNMnist(args=args).to(args.device)
    elif args.model == 'mlp':
        len_in = 1
        for x in img_size:
            len_in *= x
        net_glob = MLP(dim_in=len_in, dim_hidden=64,
                       dim_out=args.num_classes).to(args.device)
    else:
        exit('Error: unrecognized model')
    print(net_glob)
    net_glob.train()
    net_glob1.train()
    net_glob5.train()
    net_glob10.train()
    net_glob15.train()
    net_glob20.train()
    net_glob25.train()
    net_glob30.train()

    # copy weights
    w_glob = net_glob.state_dict()
    w_glob1 = net_glob1.state_dict()
    w_glob5 = net_glob5.state_dict()
    w_glob10 = net_glob10.state_dict()
    w_glob15 = net_glob15.state_dict()
    w_glob20 = net_glob20.state_dict()
    w_glob25 = net_glob25.state_dict()
    elif args.model == 'cnn' and args.dataset == 'mnist':
        net_glob = CNNMnist(args=args).to(args.device)
        net_glob1 = CNNMnist(args=args).to(args.device)
        net_glob5 = CNNMnist(args=args).to(args.device)
        net_glob7 = CNNMnist(args=args).to(args.device)
        net_glob10 = CNNMnist(args=args).to(args.device)
    elif args.model == 'mlp':
        len_in = 1
        for x in img_size:
            len_in *= x
        net_glob = MLP(dim_in=len_in, dim_hidden=64, dim_out=args.num_classes).to(args.device)
    else:
        exit('Error: unrecognized model')
    print(net_glob)
    net_glob.train()
    net_glob1.train()
    net_glob5.train()
    net_glob7.train()
    net_glob10.train()

    # copy weights
    w_glob = net_glob.state_dict()
    w_glob1 = net_glob1.state_dict()
    w_glob5 = net_glob5.state_dict()
    w_glob7 = net_glob7.state_dict()
    w_glob10 = net_glob10.state_dict()
    # training - NO ATTACK
    loss_train = []
    cv_loss, cv_acc = [], []
    val_loss_pre, counter = 0, 0
    net_best = None
Exemplo n.º 5
0
#		if param.grad is not None:
#			print(type(param))
#			param_norm = param.grad.data.norm(p)
#			total_norm += param_norm.item() ** p
#		total_norm = total_norm ** (1. / p)
#	return total_norm


def norm1(x, p):
    "First-pass implementation of p-norm."
    return (abs(x)**p).sum()**(1. / p)


#print(norm1(net_glob.state_dict(),1))
#net_glob.load_state_dict()
m = net_glob.train()
net_glob.eval()
#print(type(net_glob.state_dict()))

#print(net_glob.state_dict().keys())
#data = list(net_glob.state_dict().items())
#an_array = np.array(data)

#for layer in net_glob.ordered_layers:
#	norm_grad = layer.weight.grad.norm()
#	tone = f + ((norm_grad.numpy()) * 100.0)
best_state = copy.deepcopy(net_glob.state_dict())
#print(net_glob.grad)
print(pnorm(net_glob, 2))
#print(torch.norm(net_glob, dim=None, p=2))
#print(net_glob.state_dict().grad.norm(1))