def main(): mlp_hiddens = [1000] filter_sizes = [(9, 9), (5, 5), (5, 5)] feature_maps = [80, 50, 20] pooling_sizes = [(3, 3), (2, 2), (2, 2)] save_to = "DvC.pkl" image_size = (128, 128) output_size = 2 learningRate = 0.1 num_epochs = 300 num_batches = None if socket.gethostname() == 'tim-X550JX': host_plot = 'http://*****:*****@ %s' % ('CNN ', datetime.datetime.now(), socket.gethostname()), channels=[['train_error_rate', 'valid_error_rate'], ['train_total_gradient_norm']], after_epoch=True, server_url=host_plot)) model = Model(cost) main_loop = MainLoop(algorithm, stream_data_train, model=model, extensions=extensions) main_loop.run()
# switch models if args.model == 'lenet': cnn = LeNet(enable_lat=args.enable_lat, epsilon=args.epsilon, pro_num=args.pro_num, batch_size=args.batchsize, batch_norm=args.batchnorm, if_dropout=args.dropout) elif args.model == 'resnet': cnn = ResNet50(enable_lat=args.enable_lat, epsilon=args.epsilon, pro_num=args.pro_num, batch_size=args.batchsize, if_dropout=args.dropout) cnn.apply(conv_init) elif args.model == 'vgg': cnn = VGG16(enable_lat=args.enable_lat, epsilon=args.epsilon, pro_num=args.pro_num, batch_size=args.batchsize, if_dropout=args.dropout) elif args.model == 'alexnet': cnn = AlexNet(enable_lat=args.enable_lat, epsilon=args.epsilon, pro_num=args.pro_num, batch_size=args.batchsize, if_dropout=args.dropout) cnn.cuda() if os.path.exists(real_model_path):
optimizer.step() return self.net.state_dict() def weight_init(m): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() net = LeNet().to(device) net.apply(weight_init) optimizer_server = optim.SGD(net.parameters(), lr=LR, momentum=0.9) client_list = [] # train_data_root = '/home/dchen/dataset/MNIST/IID/' + str(CLIENT_NUM) + '/train/' # test_data_root = '/home/dchen/dataset/MNIST/IID/' + str(CLIENT_NUM) + '/test/' train_data_root = '/home/dchen/dataset/MNIST/Non-IID1/' + str(CLIENT_NUM) + '/train/' test_data_root = '/home/dchen/dataset/MNIST/Non-IID1/' + str(CLIENT_NUM) + '/test/' for i in range(CLIENT_NUM): client_name = 'client' + str(i) client_list.append(Client(client_name, train_data_root + client_name + '/', test_data_root + client_name + '/')) center_params_dict = dict() center_params_modules = list(self.net.named_parameters())
def main(): mlp_hiddens = [1000] filter_sizes = [(9,9),(5,5),(5,5)] feature_maps = [80, 50, 20] pooling_sizes = [(3,3),(2,2),(2,2)] save_to="DvC.pkl" image_size = (128, 128) output_size = 2 learningRate=0.1 num_epochs=300 num_batches=None if socket.gethostname()=='tim-X550JX':host_plot = 'http://*****:*****@ %s' % ('CNN ', datetime.datetime.now(), socket.gethostname()), channels=[['train_error_rate', 'valid_error_rate'], ['train_total_gradient_norm']], after_epoch=True, server_url=host_plot)) model = Model(cost) main_loop = MainLoop( algorithm, stream_data_train, model=model, extensions=extensions) main_loop.run()
def weight_init(m): # 使用isinstance来判断m属于什么类型 if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels # torch.manual_seed(7) # 随机种子,是否每次做相同初始化赋值 m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.BatchNorm2d): # m中的 weight,bias 其实都是 Variable,为了能学习参数以及后向传播 m.weight.data.fill_(1) m.bias.data.zero_() net = LeNet() attacker_net = LeNet() # 初始化网络参数 net.apply(weight_init) # apply函数会递归地搜索网络内的所有module并把参数表示的函数应用到所有的module上 attacker_net.apply(weight_init) # # 提取网络参数 # net_dic = net.state_dict() # 定义损失函数 criterion = nn.CrossEntropyLoss() # 交叉熵损失函数,通常用于多分类问题上 optimizer_server = optim.Adam(net.parameters(), lr=LR, betas=(0.9, 0.99)) optimizer_backdoor = optim.Adam(attacker_net.parameters(), lr=LR, betas=(0.9, 0.99)) # 分配用户参数 send_back() client_0_net = LeNet() client_1_net = LeNet() client_2_net = LeNet() # client_0_net.load_state_dict(net_dic)
def main(port_data): mlp_hiddens = [500] filter_sizes = [(3,3),(3,3)] feature_maps = [20, 20] pooling_sizes = [(3,3),(2,2)] save_to="DvC.pkl" image_size = (128, 128) output_size = 2 learningRate=0.1 num_epochs=300 num_batches=None if socket.gethostname()=='tim-X550JX':host_plot = 'http://*****:*****@ %s' % ('CNN ', datetime.datetime.now(), socket.gethostname()), channels=[['train_error_rate', 'valid_error_rate'], ['train_total_gradient_norm']], after_epoch=True, server_url=host_plot)) model = Model(cost) main_loop = MainLoop( algorithm, stream_data_train, model=model, extensions=extensions) main_loop.run()
def main(port_data): mlp_hiddens = [500] filter_sizes = [(3, 3), (3, 3)] feature_maps = [20, 20] pooling_sizes = [(3, 3), (2, 2)] save_to = "DvC.pkl" image_size = (128, 128) output_size = 2 learningRate = 0.1 num_epochs = 300 num_batches = None if socket.gethostname() == 'tim-X550JX': host_plot = 'http://*****:*****@ %s' % ('CNN ', datetime.datetime.now(), socket.gethostname()), channels=[['train_error_rate', 'valid_error_rate'], ['train_total_gradient_norm']], after_epoch=True, server_url=host_plot)) model = Model(cost) main_loop = MainLoop(algorithm, stream_data_train, model=model, extensions=extensions) main_loop.run()