Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add by tony #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys
sys.path.insert(0,"/content/federated-learning/")

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import copy
import numpy as np
from torchvision import datasets, transforms
import torch

from utils.sampling import mnist_iid, mnist_noniid, cifar_iid
from utils.options import args_parser
from models.Update import LocalUpdate
from models.Nets import MLP, CNNMnist, CNNCifar
from models.Fed import FedAvg
from models.test import test_img
8 changes: 6 additions & 2 deletions main_fed.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@

if __name__ == '__main__':
# parse args
# if 這邊是調用option 的資料
args = args_parser()
args.device = torch.device('cuda:{}'.format(args.gpu) if torch.cuda.is_available() and args.gpu != -1 else 'cpu')
# How we set up the function if you don't use gpu you don't

# load dataset and split users
args.device = torch.device('cuda:{}'.format(args.tpu) if torch.cuda.is_available() and args.gpu != -1 else 'cpu')

# load dataset and split users
# search the iid data if you want more you need set up by tfdata set
if args.dataset == 'mnist':
trans_mnist = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
dataset_train = datasets.MNIST('../data/mnist/', train=True, download=True, transform=trans_mnist)
Expand Down
4 changes: 2 additions & 2 deletions models/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ def test_img(net_g, datatest, args):
data_loader = DataLoader(datatest, batch_size=args.bs)
l = len(data_loader)
for idx, (data, target) in enumerate(data_loader):
if args.gpu != -1:
if args.tpu != -1:
data, target = data.cuda(), target.cuda()
log_probs = net_g(data)
# sum up batch loss
test_loss += F.cross_entropy(log_probs, target, reduction='sum').item()
# get the index of the max log-probability
y_pred = log_probs.data.max(1, keepdim=True)[1]
correct += y_pred.eq(target.data.view_as(y_pred)).long().cpu().sum()
correct += y_pred.eq(target.data.view_as(y_pred)).long().tpu().sum()

test_loss /= len(data_loader.dataset)
accuracy = 100.00 * correct / len(data_loader.dataset)
Expand Down
6 changes: 4 additions & 2 deletions utils/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ def args_parser():
parser.add_argument('--iid', action='store_true', help='whether i.i.d or not')
parser.add_argument('--num_classes', type=int, default=10, help="number of classes")
parser.add_argument('--num_channels', type=int, default=3, help="number of channels of imges")
parser.add_argument('--gpu', type=int, default=0, help="GPU ID, -1 for CPU")
parser.add_argument('--tpu', type=int, default=0, help="tpu ID, -1 for CPU")
parser.add_argument('--stopping_rounds', type=int, default=10, help='rounds of early stopping')
parser.add_argument('--verbose', action='store_true', help='verbose print')
parser.add_argument('--seed', type=int, default=1, help='random seed (default: 1)')
parser.add_argument('--all_clients', action='store_true', help='aggregation over all clients')
args = parser.parse_args()
# if for colab need to add
#args=[]
args = parser.parse_args(args=[])
return args