Skip to content

Commit ed8a892

Browse files
authored
Updates
1 parent 7587156 commit ed8a892

File tree

3 files changed

+139
-3
lines changed

3 files changed

+139
-3
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
# PyTorch-Ignite playground
1+
# PyTorch-Ignite Playground
22

3-
Click on the badge to launch the playground in Binder: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/pytorch-ignite/playground/HEAD?filepath=index.ipynb)
3+
- Click on the badge to launch the playground in Binder: [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/pytorch-ignite/playground/HEAD?labpath=index.ipynb)
4+
5+
- Click on the badge to launch the playground in Google Colab [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pytorch-ignite/playground/blob/main/index.ipynb)
46

57

68
## About this repository
@@ -9,7 +11,7 @@ A Binder-compatible repo with an `environment.yml` file copied from https://gith
911

1012
Access this Binder by clicking the blue badge above or at the following URL:
1113

12-
https://mybinder.org/v2/gh/pytorch-ignite/playground/HEAD?filepath=index.ipynb
14+
https://mybinder.org/v2/gh/pytorch-ignite/playground/HEAD?labpath=index.ipynb
1315

1416
### Notes
1517

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ dependencies:
66
- pytorch
77
- cpuonly
88
- ignite
9+
- numpy

index.ipynb

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,139 @@
3131
"print(torch.__version__, ignite.__version__)"
3232
]
3333
},
34+
{
35+
"attachments": {},
36+
"cell_type": "markdown",
37+
"metadata": {
38+
"vscode": {
39+
"languageId": "plaintext"
40+
}
41+
},
42+
"source": [
43+
"### Basic setup with various events filtering use-cases"
44+
]
45+
},
46+
{
47+
"attachments": {},
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"from ignite.engine import Engine, Events\n",
52+
"from ignite.utils import setup_logger, logging\n",
53+
"\n",
54+
"\n",
55+
"train_data = range(10)\n",
56+
"eval_data = range(4)\n",
57+
"max_epochs = 5\n",
58+
"\n",
59+
"\n",
60+
"def train_step(engine, batch):\n",
61+
" print(f\"{engine.state.epoch} / {engine.state.max_epochs} | {engine.state.iteration} - batch: {batch}\", flush=True)\n",
62+
"\n",
63+
"trainer = Engine(train_step)\n",
64+
"\n",
65+
"# Enable trainer logger for a debug mode\n",
66+
"# trainer.logger = setup_logger(\"trainer\", level=logging.DEBUG)\n",
67+
"\n",
68+
"evaluator = Engine(lambda e, b: None)\n",
69+
"\n",
70+
"\n",
71+
"@trainer.on(Events.EPOCH_COMPLETED(every=2))\n",
72+
"def run_validation():\n",
73+
" print(f\"{trainer.state.epoch} / {trainer.state.max_epochs} | {trainer.state.iteration} - run validation\", flush=True)\n",
74+
" evaluator.run(eval_data)\n",
75+
"\n",
76+
"\n",
77+
"@trainer.on(Events.ITERATION_COMPLETED(every=7))\n",
78+
"def log_events_filtering__every():\n",
79+
" print(f\"{trainer.state.epoch} / {trainer.state.max_epochs} | {trainer.state.iteration} - calling log_events_filtering__every\", flush=True)\n",
80+
"\n",
81+
" \n",
82+
"@trainer.on(Events.EPOCH_COMPLETED(once=3))\n",
83+
"def log_events_filtering__once():\n",
84+
" print(f\"{trainer.state.epoch} / {trainer.state.max_epochs} | {trainer.state.iteration} - calling log_events_filtering__once\", flush=True)\n",
85+
"\n",
86+
"\n",
87+
"def custom_event_filter(engine, event):\n",
88+
" if trainer.state.epoch == 2 and event in (1, 3):\n",
89+
" return True\n",
90+
" return False\n",
91+
" \n",
92+
"\n",
93+
"@evaluator.on(Events.ITERATION_COMPLETED(event_filter=custom_event_filter))\n",
94+
"def log_events_filtering__event_filter():\n",
95+
" print(f\"{trainer.state.epoch} / {trainer.state.max_epochs} | {evaluator.state.iteration} - calling log_events_filtering__event_filter\", flush=True)\n",
96+
" \n",
97+
"\n",
98+
"trainer.run(train_data, max_epochs=max_epochs)"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": null,
104+
"metadata": {
105+
"vscode": {
106+
"languageId": "plaintext"
107+
}
108+
},
109+
"outputs": [],
110+
"source": []
111+
},
112+
{
113+
"attachments": {},
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"### Distributed run with 2 processes"
118+
]
119+
},
120+
{
121+
"cell_type": "code",
122+
"execution_count": null,
123+
"metadata": {
124+
"vscode": {
125+
"languageId": "plaintext"
126+
}
127+
},
128+
"outputs": [],
129+
"source": [
130+
"import time\n",
131+
"import ignite.distributed as idist\n",
132+
"\n",
133+
"from ignite.engine import Engine, Events\n",
134+
"from ignite.utils import setup_logger, logging\n",
135+
"\n",
136+
"\n",
137+
"def pprint(*args, **kwargs):\n",
138+
" rank = idist.get_rank()\n",
139+
" time.sleep(rank * 0.1)\n",
140+
" print(f\"Rank {rank}:\", end=\" \")\n",
141+
" print(*args, **kwargs)\n",
142+
"\n",
143+
"\n",
144+
"def run(local_rank): \n",
145+
" rank = idist.get_rank() \n",
146+
" torch.manual_seed(12 + rank)\n",
147+
" \n",
148+
" train_data = range(10)\n",
149+
" eval_data = range(4)\n",
150+
" max_epochs = 5\n",
151+
"\n",
152+
" def train_step(engine, batch):\n",
153+
" pprint(f\"{engine.state.epoch} / {engine.state.max_epochs} | {engine.state.iteration} - batch: {batch}\", flush=True)\n",
154+
"\n",
155+
" trainer = Engine(train_step)\n",
156+
" \n",
157+
" @trainer.on(Events.EPOCH_COMPLETED)\n",
158+
" def sync():\n",
159+
" idist.barrier()\n",
160+
"\n",
161+
" trainer.run(train_data, max_epochs=max_epochs) \n",
162+
" \n",
163+
" \n",
164+
"idist.spawn(\"gloo\", run, (), nproc_per_node=2) "
165+
]
166+
},
34167
{
35168
"cell_type": "code",
36169
"execution_count": null,

0 commit comments

Comments
 (0)