Skip to content

Commit 7c66964

Browse files
committed
plugin csvwriter
1 parent d86ebeb commit 7c66964

File tree

2 files changed

+149
-3
lines changed

2 files changed

+149
-3
lines changed

en/plugin/writer-csv.md

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,75 @@
1-
# CSV Writer
1+
# CSV Table Logger
22

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+
```

zh/plugin/writer-csv.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,77 @@
11
# CSV表格记录器
22

3-
waiting for update...
3+
如果你希望在训练过程中,将一些配置信息、指标信息记录在本地的CSV文件中(格式和SwanLab网页中的“表格视图“一致),那么非常推荐你使用`CSV记录器`插件。
4+
5+
:::warning 改进插件
6+
SwanLab插件均为开源代码,你可以在[Github源代码](https://github.com/SwanHubX/SwanLab/blob/main/swanlab/plugin/writer.py)中查看,欢迎提交你的建议和PR!
7+
:::
8+
9+
## 插件用法
10+
11+
**1. 初始化CSV记录器:**
12+
13+
```python
14+
from swanlab.plugin.writer import CSVWriter
15+
16+
csv_writer = CSVWriter(dir="logs")
17+
```
18+
19+
`dir`参数指定了CSV文件的保存路径,默认保存到当前工作目录。
20+
21+
**2. 传入插件:**
22+
23+
```python
24+
swanlab.init(
25+
...
26+
callbacks=[csv_writer]
27+
)
28+
```
29+
30+
执行代码后,就会在`logs`目录下生成一个`swanlab_run.csv`文件,并开始记录数据。后续的每一次训练,都会在该csv文件中添加新的行。
31+
32+
如果想要指定其他文件名,可以传入`filename`参数:
33+
34+
```python
35+
csv_writer = CSVWriter(dir="logs", filename="my_csv_file.csv")
36+
```
37+
38+
39+
## 示例代码
40+
41+
42+
```python
43+
import swanlab
44+
from swanlab.plugin.writer import CSVWriter
45+
import random
46+
47+
csv_writer = CSVWriter(dir="logs")
48+
49+
# 创建一个SwanLab项目
50+
swanlab.init(
51+
# 设置项目名
52+
project="my-awesome-project",
53+
54+
# 设置超参数
55+
config={
56+
"learning_rate": 0.02,
57+
"architecture": "CNN",
58+
"dataset": "CIFAR-100",
59+
"epochs": 10,
60+
"batch_size": 128
61+
},
62+
callbacks=[csv_writer]
63+
)
64+
65+
# 模拟一次训练
66+
epochs = 10
67+
offset = random.random() / 5
68+
for epoch in range(2, epochs):
69+
acc = 1 - 2 ** -epoch - random.random() / epoch - offset
70+
loss = 2 ** -epoch + random.random() / epoch + offset
71+
72+
# 记录训练指标
73+
swanlab.log({"acc": acc, "loss2": loss})
74+
75+
# [可选] 完成训练,这在notebook环境中是必要的
76+
swanlab.finish()
77+
```

0 commit comments

Comments
 (0)