|
1 | | -# CSV Writer |
| 1 | +# CSV Table Logger |
2 | 2 |
|
3 | | -waiting for update... |
| 3 | +If you wish to record some configuration information and metrics locally in a CSV file during training (in a format consistent with the "Table View" on the SwanLab webpage), we highly recommend using the `CSV Logger` plugin. |
| 4 | + |
| 5 | +:::warning Improving the Plugin |
| 6 | +All SwanLab plugins are open-source. You can view the [Github source code](https://github.com/SwanHubX/SwanLab/blob/main/swanlab/plugin/writer.py). We welcome your suggestions and PRs! |
| 7 | +::: |
| 8 | + |
| 9 | +## Plugin Usage |
| 10 | + |
| 11 | +**1. Initialize the CSV Logger:** |
| 12 | + |
| 13 | +```python |
| 14 | +from swanlab.plugin.writer import CSVWriter |
| 15 | + |
| 16 | +csv_writer = CSVWriter(dir="logs") |
| 17 | +``` |
| 18 | + |
| 19 | +The `dir` parameter specifies the save path for the CSV file. By default, it is saved in the current working directory. |
| 20 | + |
| 21 | +**2. Pass the Plugin:** |
| 22 | + |
| 23 | +```python |
| 24 | +swanlab.init( |
| 25 | + ... |
| 26 | + callbacks=[csv_writer] |
| 27 | +) |
| 28 | +``` |
| 29 | + |
| 30 | +After executing the code, a `swanlab_run.csv` file will be generated in the `logs` directory, and data recording will begin. For each subsequent training session, a new row will be added to this CSV file. |
| 31 | + |
| 32 | +If you want to specify a different file name, you can pass the `filename` parameter: |
| 33 | + |
| 34 | +```python |
| 35 | +csv_writer = CSVWriter(dir="logs", filename="my_csv_file.csv") |
| 36 | +``` |
| 37 | + |
| 38 | +## Example Code |
| 39 | + |
| 40 | +```python |
| 41 | +import swanlab |
| 42 | +from swanlab.plugin.writer import CSVWriter |
| 43 | +import random |
| 44 | + |
| 45 | +csv_writer = CSVWriter(dir="logs") |
| 46 | + |
| 47 | +# Create a SwanLab project |
| 48 | +swanlab.init( |
| 49 | + # Set the project name |
| 50 | + project="my-awesome-project", |
| 51 | + |
| 52 | + # Set hyperparameters |
| 53 | + config={ |
| 54 | + "learning_rate": 0.02, |
| 55 | + "architecture": "CNN", |
| 56 | + "dataset": "CIFAR-100", |
| 57 | + "epochs": 10, |
| 58 | + "batch_size": 128 |
| 59 | + }, |
| 60 | + callbacks=[csv_writer] |
| 61 | +) |
| 62 | + |
| 63 | +# Simulate a training session |
| 64 | +epochs = 10 |
| 65 | +offset = random.random() / 5 |
| 66 | +for epoch in range(2, epochs): |
| 67 | + acc = 1 - 2 ** -epoch - random.random() / epoch - offset |
| 68 | + loss = 2 ** -epoch + random.random() / epoch + offset |
| 69 | + |
| 70 | + # Log training metrics |
| 71 | + swanlab.log({"acc": acc, "loss2": loss}) |
| 72 | + |
| 73 | +# [Optional] Finish training, which is necessary in notebook environments |
| 74 | +swanlab.finish() |
| 75 | +``` |
0 commit comments