Skip to content

Commit 5296335

Browse files
Removes redunant variables from code blocks in add-w-and-b-to-your-code.md (#1187)
This PR: - removes the unused **run** variables from the code blocks, and - fixes a minor typo ("," -> ":") in one of the code blocks --------- Co-authored-by: Matt Linville <matt.linville@wandb.com>
1 parent f666f31 commit 5296335

1 file changed

Lines changed: 87 additions & 65 deletions

File tree

content/guides/models/sweeps/add-w-and-b-to-your-code.md

Lines changed: 87 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ title: Add W&B (wandb) to your code
88
weight: 2
99
---
1010

11-
There are numerous ways to add the W&B Python SDK to your script or Jupyter Notebook. Outlined below is a "best practice" example of how to integrate the W&B Python SDK into your own code.
11+
There are numerous ways to add the W&B Python SDK to your script or notebook. This section provides a "best practice" example that shows how to integrate the W&B Python SDK into your own code.
1212

1313
### Original training script
1414

@@ -51,12 +51,13 @@ def main():
5151

5252
### Training script with W&B Python SDK
5353

54-
The following code examples demonstrate how to add the W&B Python SDK into your code. If you start W&B Sweep jobs in the CLI, you will want to explore the CLI tab. If you start W&B Sweep jobs within a Jupyter notebook or Python script, explore the Python SDK tab.
54+
The following code examples demonstrate how to add the W&B Python SDK into your
55+
code. If you start W&B Sweep jobs in the CLI, you will want to explore the CLI
56+
tab. If you start W&B Sweep jobs within a Jupyter notebook or Python script,
57+
explore the Python SDK tab.
5558

56-
57-
{{< tabpane text=true >}}
58-
{{% tab header="Python script or notebook" %}}
59-
To create a W&B Sweep, we added the following to the code example:
59+
{{< tabpane text=true >}} {{% tab header="Python script or notebook" %}} To
60+
create a W&B Sweep, we added the following to the code example:
6061

6162
1. Import the Weights & Biases Python SDK.
6263
2. Create a dictionary object where the key-value pairs define the sweep configuration. In the proceeding example, the batch size (`batch_size`), epochs (`epochs`), and the learning rate (`lr`) hyperparameters are varied during each sweep. For more information on how to create a sweep configuration, see [Define sweep configuration]({{< relref "/guides/models/sweeps/define-sweep-configuration/" >}}).
@@ -72,26 +73,10 @@ import wandb
7273
import numpy as np
7374
import random
7475

75-
# Define sweep config
76-
sweep_configuration = {
77-
"method": "random",
78-
"name": "sweep",
79-
"metric": {"goal": "maximize", "name": "val_acc"},
80-
"parameters": {
81-
"batch_size": {"values": [16, 32, 64]},
82-
"epochs": {"values": [5, 10, 15]},
83-
"lr": {"max": 0.1, "min": 0.0001},
84-
},
85-
}
86-
87-
# Initialize sweep by passing in config.
88-
# (Optional) Provide a name of the project.
89-
sweep_id = wandb.sweep(sweep=sweep_configuration, project="my-first-sweep")
90-
9176

9277
# Define training function that takes in hyperparameter
9378
# values from `wandb.config` and uses them to train a
94-
# model and return metric
79+
# model and return the metrics
9580
def train_one_epoch(epoch, lr, bs):
9681
acc = 0.25 + ((epoch / 30) + (random.random() / 10))
9782
loss = 0.2 + (1 - ((epoch - 1) / 10 + random.random() / 5))
@@ -104,38 +89,72 @@ def evaluate_one_epoch(epoch):
10489
return acc, loss
10590

10691

107-
def main():
108-
run = wandb.init()
109-
110-
# note that we define values from `wandb.config`
111-
# instead of defining hard values
112-
lr = wandb.config.lr
113-
bs = wandb.config.batch_size
114-
epochs = wandb.config.epochs
115-
116-
for epoch in np.arange(1, epochs):
117-
train_acc, train_loss = train_one_epoch(epoch, lr, bs)
118-
val_acc, val_loss = evaluate_one_epoch(epoch)
92+
# Define a sweep config dictionary
93+
sweep_configuration = {
94+
"method": "random",
95+
"name": "sweep",
96+
"metric": {"goal": "maximize", "name": "val_acc"},
97+
"parameters": {
98+
"batch_size": {"values": [16, 32, 64]},
99+
"epochs": {"values": [5, 10, 15]},
100+
"lr": {"max": 0.1, "min": 0.0001},
101+
},
102+
}
119103

120-
wandb.log(
121-
{
122-
"epoch": epoch,
123-
"train_acc": train_acc,
124-
"train_loss": train_loss,
125-
"val_acc": val_acc,
126-
"val_loss": val_loss,
127-
}
128-
)
104+
# (Optional) Provide a name for the project.
105+
project = "my-first-sweep"
129106

107+
def main():
108+
# Use the `with` context manager statement to automatically end the run.
109+
# This is equivalent to using `run.finish()` at the end of each run
110+
with wandb.init(project=project) as run:
111+
112+
# This code fetches the hyperparameter values from `wandb.config`
113+
# instead of defining them explicitly
114+
lr = run.config["lr"]
115+
bs = run.config["batch_size"]
116+
epochs = run.config["epochs"]
117+
118+
# Execute the training loop and log the performance values to W&B
119+
for epoch in np.arange(1, epochs):
120+
train_acc, train_loss = train_one_epoch(epoch, lr, bs)
121+
val_acc, val_loss = evaluate_one_epoch(epoch)
122+
123+
run.log(
124+
{
125+
"epoch": epoch,
126+
"train_acc": train_acc,
127+
"train_loss": train_loss,
128+
"val_acc": val_acc,
129+
"val_loss": val_loss,
130+
}
131+
)
132+
133+
134+
if __name__ == "__main__":
135+
# Initialize the sweep by passing in the config dictionary
136+
sweep_id = wandb.sweep(sweep=sweep_configuration, project=project)
137+
138+
# Start the sweep job
139+
wandb.agent(sweep_id, function=main, count=4)
130140

131-
# Start sweep job.
132-
wandb.agent(sweep_id, function=main, count=4)
133141
```
134142

135-
{{% /tab %}}
136-
{{% tab header="CLI" %}}
143+
{{% alert %}} The preceding code snippet shows how to initialize a
144+
[`wandb.init()`]({{< relref "/ref/python/init.md" >}}) API within a `with`
145+
context manager statement to generate a background process to sync and log data
146+
as a [W&B Run]({{< relref "/ref/python/run.md" >}}). This ensures the run is
147+
properly terminated after uploading the logged values. An alternative approach
148+
is to call `wandb.init()` and `wandb.finish()` at the beginning and end of the
149+
training script, respectively.
150+
{{% /alert %}}
151+
152+
{{% /tab %}} {{% tab header="CLI" %}}
137153

138-
To create a W&B Sweep, we first create a YAML configuration file. The configuration file contains he hyperparameters we want the sweep to explore. In the proceeding example, the batch size (`batch_size`), epochs (`epochs`), and the learning rate (`lr`) hyperparameters are varied during each sweep.
154+
To create a W&B Sweep, we first create a YAML configuration file. The
155+
configuration file contains he hyperparameters we want the sweep to explore. In
156+
the proceeding example, the batch size (`batch_size`), epochs (`epochs`), and
157+
the learning rate (`lr`) hyperparameters are varied during each sweep.
139158

140159
```yaml
141160
# config.yaml
@@ -146,8 +165,8 @@ metric:
146165
goal: maximize
147166
name: val_acc
148167
parameters:
149-
batch_size:
150-
values: [16,32,64]
168+
batch_size:
169+
values: [16, 32, 64]
151170
lr:
152171
min: 0.0001
153172
max: 0.1
@@ -157,7 +176,8 @@ parameters:
157176
158177
For more information on how to create a W&B Sweep configuration, see [Define sweep configuration]({{< relref "/guides/models/sweeps/define-sweep-configuration/" >}}).
159178
160-
Note that you must provide the name of your Python script for the `program` key in your YAML file.
179+
You must provide the name of your Python script for the `program` key
180+
in your YAML file.
161181

162182
Next, we add the following to the code example:
163183

@@ -192,7 +212,7 @@ def main():
192212
with open("./config.yaml") as file:
193213
config = yaml.load(file, Loader=yaml.FullLoader)
194214
195-
run = wandb.init(config=config)
215+
wandb.init(config=config)
196216
197217
# Note that we define values from `wandb.config`
198218
# instead of defining hard values
@@ -219,7 +239,9 @@ def main():
219239
main()
220240
```
221241

222-
Navigate to your CLI. Within your CLI, set a maximum number of runs the sweep agent should try. This is step optional. In the following example we set the maximum number to five.
242+
In your CLI, set a maximum number of runs for the sweep
243+
agent to try. This is optional. This example we set the
244+
maximum number to 5.
223245

224246
```bash
225247
NUM=5
@@ -231,24 +253,24 @@ Next, initialize the sweep with the [`wandb sweep`]({{< relref "/ref/cli/wandb-s
231253
wandb sweep --project sweep-demo-cli config.yaml
232254
```
233255

234-
This returns a sweep ID. For more information on how to initialize sweeps, see [Initialize sweeps]({{< relref "./initialize-sweeps.md" >}}).
256+
This returns a sweep ID. For more information on how to initialize sweeps, see
257+
[Initialize sweeps]({{< relref "./initialize-sweeps.md" >}}).
235258

236-
Copy the sweep ID and replace `sweepID` in the proceeding code snippet to start the sweep job with the [`wandb agent`]({{< relref "/ref/cli/wandb-agent.md" >}}) command:
259+
Copy the sweep ID and replace `sweepID` in the proceeding code snippet to start
260+
the sweep job with the [`wandb agent`]({{< relref "/ref/cli/wandb-agent.md" >}})
261+
command:
237262

238263
```bash
239264
wandb agent --count $NUM your-entity/sweep-demo-cli/sweepID
240265
```
241266

242-
For more information on how to start sweep jobs, see [Start sweep jobs]({{< relref "./start-sweep-agents.md" >}}).
267+
For more information, see [Start sweep jobs]({{< relref "./start-sweep-agents.md" >}}).
243268

244-
{{% /tab %}}
245-
{{< /tabpane >}}
269+
{{% /tab %}} {{< /tabpane >}}
246270

271+
## Consideration when logging metrics
247272

248-
249-
## Consideration when logging metrics
250-
251-
Ensure to log the metric you specify in your sweep configuration explicitly to W&B. Do not log metrics for your sweep inside of a sub-directory.
273+
Be sure to log the sweep's metric to W&B explicitly. Do not log metrics for your sweep inside a subdirectory.
252274

253275
For example, consider the proceeding psuedocode. A user wants to log the validation loss (`"val_loss": loss`). First they pass the values into a dictionary. However, the dictionary passed to `wandb.log` does not explicitly access the key-value pair in the dictionary:
254276

@@ -309,7 +331,7 @@ def train():
309331
def main():
310332
wandb.init(entity="<entity>", project="my-first-sweep")
311333
val_metrics = train()
312-
wandb.log({"val_loss", val_metrics["val_loss"]})
334+
wandb.log({"val_loss": val_metrics["val_loss"]})
313335

314336

315337
sweep_configuration = {
@@ -324,4 +346,4 @@ sweep_configuration = {
324346
sweep_id = wandb.sweep(sweep=sweep_configuration, project="my-first-sweep")
325347

326348
wandb.agent(sweep_id, function=main, count=10)
327-
```
349+
```

0 commit comments

Comments
 (0)