You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>
Copy file name to clipboardExpand all lines: content/guides/models/sweeps/add-w-and-b-to-your-code.md
+87-65Lines changed: 87 additions & 65 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ title: Add W&B (wandb) to your code
8
8
weight: 2
9
9
---
10
10
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.
12
12
13
13
### Original training script
14
14
@@ -51,12 +51,13 @@ def main():
51
51
52
52
### Training script with W&B Python SDK
53
53
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.
55
58
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:
60
61
61
62
1. Import the Weights & Biases Python SDK.
62
63
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/" >}}).
{{% 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" %}}
137
153
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.
139
158
140
159
```yaml
141
160
# config.yaml
@@ -146,8 +165,8 @@ metric:
146
165
goal: maximize
147
166
name: val_acc
148
167
parameters:
149
-
batch_size:
150
-
values: [16,32,64]
168
+
batch_size:
169
+
values: [16,32,64]
151
170
lr:
152
171
min: 0.0001
153
172
max: 0.1
@@ -157,7 +176,8 @@ parameters:
157
176
158
177
For more information on how to create a W&B Sweep configuration, see [Define sweep configuration]({{< relref "/guides/models/sweeps/define-sweep-configuration/" >}}).
159
178
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.
161
181
162
182
Next, we add the following to the code example:
163
183
@@ -192,7 +212,7 @@ def main():
192
212
with open("./config.yaml") as file:
193
213
config = yaml.load(file, Loader=yaml.FullLoader)
194
214
195
-
run = wandb.init(config=config)
215
+
wandb.init(config=config)
196
216
197
217
# Note that we define values from `wandb.config`
198
218
# instead of defining hard values
@@ -219,7 +239,9 @@ def main():
219
239
main()
220
240
```
221
241
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.
223
245
224
246
```bash
225
247
NUM=5
@@ -231,24 +253,24 @@ Next, initialize the sweep with the [`wandb sweep`]({{< relref "/ref/cli/wandb-s
231
253
wandb sweep --project sweep-demo-cli config.yaml
232
254
```
233
255
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
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" >}})
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" >}}).
243
268
244
-
{{% /tab %}}
245
-
{{< /tabpane >}}
269
+
{{% /tab %}} {{< /tabpane >}}
246
270
271
+
## Consideration when logging metrics
247
272
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.
252
274
253
275
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:
0 commit comments