Newer
Older
"metadata": {},
"outputs": [],
"source": [
"from datasets.Femnist import Femnist\n",
"from graphs import SmallWorld\n",
"from collections import defaultdict\n",
"import os\n",
"import json\n",
"import numpy as np\n"
]
},
{
"cell_type": "code",
"source": [
"a = FEMNIST\n",
"a"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"b = SmallWorld(6, 2, 2, 1)"
]
},
{
"cell_type": "code",
"source": [
"b.adj_list"
]
},
{
"cell_type": "code",
"source": [
"for i in range(12):\n",
" print(b.neighbors(i))"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"clients = []"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"num_samples = []\n",
"data = defaultdict(lambda : None)"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"datadir = \"./leaf/data/femnist/data/train\"\n",
"files = os.listdir(datadir)\n",
"total_users=0\n",
"users = set()"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"files = os.listdir(datadir)[0:1]"
]
},
{
"cell_type": "code",
"source": [
"for f in files:\n",
" file_path = os.path.join(datadir, f)\n",
" print(file_path)\n",
" with open(file_path, 'r') as inf:\n",
" client_data = json.load(inf)\n",
" current_users = len(client_data['users'])\n",
" print(\"Current_Users: \", current_users)\n",
" total_users += current_users\n",
" users.update(client_data['users'])\n",
"\n",
"print(\"total_users: \", total_users)\n",
"print(\"total_users: \", len(users))\n",
"print(client_data['user_data'].keys())\n",
"print(np.array(client_data['user_data']['f3408_47']['x']).shape)\n",
"print(np.array(client_data['user_data']['f3408_47']['y']).shape)\n",
"print(np.array(client_data['user_data']['f3327_11']['x']).shape)\n",
"print(np.array(client_data['user_data']['f3327_11']['y']).shape)\n",
"print(np.unique(np.array(client_data['user_data']['f3327_11']['y'])))"
]
},
{
"cell_type": "code",
"source": [
"file = 'run.py'\n",
"with open(file, 'r') as inf:\n",
" print(inf.readline().strip())\n",
" print(inf.readlines())"
]
},
{
"cell_type": "code",
"source": [
"def f(l):\n",
" l[2] = 'c'\n",
"\n",
"a = ['a', 'a', 'a']\n",
"print(a)\n",
"f(a)\n",
"print(a)"
]
},
{
"cell_type": "code",
"source": [
"l = ['a', 'b', 'c']\n",
"print(l[:-1])"
]
},
{
"cell_type": "code",
"from localconfig import LocalConfig\n",
" config = LocalConfig(file_path)\n",
" for section in config:\n",
" print(\"Section: \", section)\n",
" for key, value in config.items(section):\n",
" print((key, value))\n",
" print(dict(config.items('DATASET')))\n",
"config = read_ini(\"config.ini\")\n",
"for section in config:\n",
" print(section)\n",
"#d = dict(config.sections())"
{
"cell_type": "code",
"metadata": {},
"source": [
"def func(a = 1, b = 2, c = 3):\n",
" print(a + b + c)\n",
"\n",
"l = [3, 5, 7]\n",
"\n",
"func(*l)"
]
},
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from torch import multiprocessing as mp\n",
"\n",
"mp.spawn(fn = func, nprocs = 2, args = [], kwargs = {'a': 4, 'b': 5, 'c': 6})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"l = '[0.4, 0.2, 0.3, 0.1]'\n",
"type(eval(l))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from decentralizepy.datasets.Femnist import Femnist\n",
"f1 = Femnist(0, 1, 'leaf/data/femnist/data/train')\n",
"ts = f1.get_trainset(1)\n",
"for data, target in ts:\n",
" print(data)\n",
" break"
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from decentralizepy.datasets.Femnist import Femnist\n",
"from decentralizepy.graphs.SmallWorld import SmallWorld\n",
"from decentralizepy.mappings.Linear import Linear\n",
"\n",
"f = Femnist(2, 'leaf/data/femnist/data/train', sizes=[0.6, 0.4])\n",
"g = SmallWorld(4, 1, 0.5)\n",
"l = Linear(2, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from decentralizepy.node.Node import Node\n",
"from torch import multiprocessing as mp\n",
"import logging\n",
"n1 = Node(0, l, g, f, \"./results\", logging.DEBUG)\n",
"n2 = Node(1, l, g, f, \"./results\", logging.DEBUG)\n",
"# mp.spawn(fn = Node, nprocs = 2, args=[l,g,f])"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"from testing import f"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"from torch import multiprocessing as mp\n",
"import torch\n",
"m1 = torch.nn.Linear(1,1)\n",
"o1 = torch.optim.SGD(m1.parameters(), 0.6)\n",
"print(m1)\n",
"\n",
"mp.spawn(fn = f, nprocs = 2, args=[m1, o1])\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"metadata": {},
"source": [
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"o1.param_groups"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with torch.no_grad():\n",
" o1.param_groups[0][\"params\"][0].copy_(torch.zeros(1,))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"o1.param_groups"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m1.state_dict()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"loss = getattr(torch.nn.functional, 'nll_loss')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"loss"
]
},
{
"cell_type": "code",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Section: GRAPH\n",
"('package', 'decentralizepy.graphs.SmallWorld')\n",
"('graph_class', 'SmallWorld')\n",
"Section: DATASET\n",
"('dataset_package', 'decentralizepy.datasets.Femnist')\n",
"('dataset_class', 'Femnist')\n",
"('model_class', 'CNN')\n",
"('n_procs', 36)\n",
"('train_dir', 'leaf/data/femnist/per_user_data/train')\n",
"('test_dir', 'leaf/data/femnist/data/test')\n",
"('sizes', '')\n",
"Section: OPTIMIZER_PARAMS\n",
"('optimizer_package', 'torch.optim')\n",
"('optimizer_class', 'Adam')\n",
"('lr', 0.01)\n",
"Section: TRAIN_PARAMS\n",
"('training_package', 'decentralizepy.training.Training')\n",
"('training_class', 'Training')\n",
"('epochs_per_round', 1)\n",
"('shuffle', True)\n",
"('loss_package', 'torch.nn')\n",
"('loss_class', 'CrossEntropyLoss')\n",
"Section: COMMUNICATION\n",
"('comm_package', 'decentralizepy.communication.TCP')\n",
"('comm_class', 'TCP')\n",
"('addresses_filepath', 'ip_addr.json')\n",
"Section: SHARING\n",
"('sharing_package', 'decentralizepy.sharing.Sharing')\n",
"('sharing_class', 'Sharing')\n",
"{'dataset_package': 'decentralizepy.datasets.Femnist', 'dataset_class': 'Femnist', 'model_class': 'CNN', 'n_procs': 36, 'train_dir': 'leaf/data/femnist/per_user_data/train', 'test_dir': 'leaf/data/femnist/data/test', 'sizes': ''}\n"
]
"source": [
"%matplotlib inline\n",
"\n",
"from decentralizepy.node.Node import Node\n",
"from decentralizepy.graphs.SmallWorld import SmallWorld\n",
"from decentralizepy.mappings.Linear import Linear\n",
"from torch import multiprocessing as mp\n",
"import torch\n",
"import logging\n",
"\n",
"from localconfig import LocalConfig\n",
"\n",
"def read_ini(file_path):\n",
" config = LocalConfig(file_path)\n",
" for section in config:\n",
" print(\"Section: \", section)\n",
" for key, value in config.items(section):\n",
" print((key, value))\n",
" print(dict(config.items('DATASET')))\n",
" return config\n",
" \n",
"config = read_ini(\"config.ini\")\n",
"my_config = dict()\n",
"for section in config:\n",
" my_config[section] = dict(config.items(section))\n",
"\n",
"#f = Femnist(2, 'leaf/data/femnist/data/train', sizes=[0.6, 0.4])\n",
"g.read_graph_from_file(\"36_nodes.edges\", \"edges\")\n",
"l = Linear(1, 36)\n",
"\n",
"#Node(0, 0, l, g, my_config, 20, \"results\", logging.DEBUG)\n",
"\n",
"mp.spawn(fn = Node, nprocs = g.n_procs, args=[0,l,g,my_config,20,\"results\",logging.INFO])\n",
"\n",
"# mp.spawn(fn = Node, args = [l, g, config, 10, \"results\", logging.DEBUG], nprocs=2)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from decentralizepy.mappings.Linear import Linear\n",
"from testing import f\n",
"from torch import multiprocessing as mp\n",
"\n",
"l = Linear(1, 2)\n",
"mp.spawn(fn = f, nprocs = 2, args = [0, 2, \"ip_addr.json\", l])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from decentralizepy.datasets.Femnist import Femnist\n",
"\n",
"f = Femnist()\n",
"\n",
"f.file_per_user('leaf/data/femnist/data/train','leaf/data/femnist/per_user_data/train')\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"a = set()\n",
"a.update([2, 3, 4, 5])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{2, 3, 4, 5}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 5,
"source": [
"from decentralizepy.graphs.SmallWorld import SmallWorld\n",
"\n",
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
"s = SmallWorld(36, 2, .5)\n",
"s.write_graph_to_file('36_nodes.edges')"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['/home/risharma/miniconda3/envs/decpy/lib/python3.9/site-packages/ipykernel_launcher.py',\n",
" '--ip=127.0.0.1',\n",
" '--stdin=9008',\n",
" '--control=9006',\n",
" '--hb=9005',\n",
" '--Session.signature_scheme=\"hmac-sha256\"',\n",
" '--Session.key=b\"eac5d2f8-c460-45f1-a268-1e4b46a6efd6\"',\n",
" '--shell=9007',\n",
" '--transport=\"tcp\"',\n",
" '--iopub=9009',\n",
" '--f=/tmp/tmp-21212479paJaUBJBN84.json']"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import sys\n",
"sys.argv"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
}
],
"metadata": {
"interpreter": {
"hash": "996934296aa9d79be6c3d800a38d8fdb7dfa8fe7bb07df178f1397cde2cb8742"
},
"kernelspec": {
"display_name": "Python 3.9.7 64-bit ('tff': conda)",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}