-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.py
More file actions
33 lines (26 loc) · 788 Bytes
/
Copy pathstate.py
File metadata and controls
33 lines (26 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import torch
import torch.nn as nn
import torch.optim as optim
# 定义一个简单的模型
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.fc = nn.Linear(10, 1) # 一个简单的线性层
def forward(self, x):
return self.fc(x)
# 初始化模型和优化器
model = SimpleModel()
optimizer = optim.SGD(model.parameters(), lr=0.01)
# 进行一次前向传播和反向传播
x = torch.randn(10) # 随机输入
output = model(x)
loss = output.mean()
loss.backward()
# 更新优化器的参数
optimizer.step()
# 查看优化器中的state
optimizer_state = optimizer.state_dict()
# 打印所有的 state 和 param_groups
print("Optimizer state:")
for p in model.parameters():
print(optimizer.state[p])