Skip to content

Commit b743e85

Browse files
committed
[DOCS-1786] Fix run forking code example
- Specify the project - Log both run2 metrics in a single call - Use a context manager for each code block in the page
1 parent 3e42437 commit b743e85

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

models/runs/forking.mdx

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ To fork a run, use the `fork_from` argument in [`wandb.init()`](/models/ref/pyth
2525
import wandb
2626

2727
# Initialize a run to be forked later
28-
original_run = wandb.init(project="your_project_name", entity="your_entity_name")
29-
# ... perform training or logging ...
30-
original_run.finish()
28+
with wandb.init(project="your_project_name", entity="your_entity_name") as original_run:
29+
# ... perform training or logging ...
30+
pass
3131

3232
# Fork the run from a specific step
33-
forked_run = wandb.init(
33+
with wandb.init(
3434
project="your_project_name",
3535
entity="your_entity_name",
3636
fork_from=f"{original_run.id}?_step=200",
37-
)
38-
forked_run.finish()
37+
) as forked_run:
38+
pass
3939
```
4040

4141
## Continue from a forked run
@@ -48,29 +48,32 @@ import wandb
4848
import math
4949

5050
# Initialize the first run and log some metrics
51-
run1 = wandb.init("your_project_name", entity="your_entity_name")
52-
for i in range(300):
53-
run1.log({"metric": i})
54-
run1.finish()
51+
with wandb.init(project="your_project_name", entity="your_entity_name") as run1:
52+
for i in range(300):
53+
run1.log({"metric": i})
5554

5655
# Fork from the first run at a specific step and log the metric starting from step 200
57-
run2 = wandb.init(
58-
"your_project_name", entity="your_entity_name", fork_from=f"{run1.id}?_step=200"
59-
)
60-
61-
# Continue logging in the new run
62-
# For the first few steps, log the metric as is from run1
63-
# After step 250, start logging the spikey pattern
64-
for i in range(200, 300):
65-
if i < 250:
66-
run2.log({"metric": i}) # Continue logging from run1 without spikes
67-
else:
68-
# Introduce the spikey behavior starting from step 250
69-
subtle_spike = i + (2 * math.sin(i / 3.0)) # Apply a subtle spikey pattern
70-
run2.log({"metric": subtle_spike})
71-
# Additionally log the new metric at all steps
72-
run2.log({"additional_metric": i * 1.1})
73-
run2.finish()
56+
with wandb.init(
57+
project="your_project_name",
58+
entity="your_entity_name",
59+
fork_from=f"{run1.id}?_step=200"
60+
) as run2:
61+
# Continue logging in the new run
62+
# For the first few steps, log the metric as is from run1
63+
# After step 250, start logging the spikey pattern
64+
for i in range(200, 300):
65+
if i < 250:
66+
# Continue logging from run1 without spikes
67+
metric_value = i
68+
else:
69+
# Introduce the spikey behavior starting from step 250
70+
metric_value = i + (2 * math.sin(i / 3.0)) # Apply a subtle spikey pattern
71+
72+
# Log both metrics in a single call to ensure they're logged at the same step
73+
run2.log({
74+
"metric": metric_value,
75+
"additional_metric": i * 1.1
76+
})
7477
```
7578

7679
<Note>

0 commit comments

Comments
 (0)