PyTorch:正确提取学习的权重
PyTorch: Extract learned weights correctly
我正在尝试从线性层中提取权重,但它们似乎没有改变,尽管误差单调下降(即正在进行训练)。打印权重总和,没有任何反应,因为它保持不变:
np.sum(model.fc2.weight.data.numpy())
以下是代码片段:
def train(epochs):
model.train()
for epoch in range(1, epochs+1):
# Train on train set
print(np.sum(model.fc2.weight.data.numpy()))
for batch_idx, (data, target) in enumerate(train_loader):
data, target = Variable(data), Variable(data)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
和
# Define model
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(100, 80, bias=False)
init.normal(self.fc1.weight, mean=0, std=1)
self.fc2 = nn.Linear(80, 87)
self.fc3 = nn.Linear(87, 94)
self.fc4 = nn.Linear(94, 100)
def forward(self, x):
x = self.fc1(x)
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.relu(self.fc4(x))
return x
也许我查看了错误的参数,尽管我检查了文档。感谢您的帮助!
使用model.parameters()
获取任何模型或层的可训练权重。记得放在list()里面,不然打印不出来
以下代码片段有效
>>> import torch
>>> import torch.nn as nn
>>> l = nn.Linear(3,5)
>>> w = list(l.parameters())
>>> w
我正在尝试从线性层中提取权重,但它们似乎没有改变,尽管误差单调下降(即正在进行训练)。打印权重总和,没有任何反应,因为它保持不变:
np.sum(model.fc2.weight.data.numpy())
以下是代码片段:
def train(epochs):
model.train()
for epoch in range(1, epochs+1):
# Train on train set
print(np.sum(model.fc2.weight.data.numpy()))
for batch_idx, (data, target) in enumerate(train_loader):
data, target = Variable(data), Variable(data)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
和
# Define model
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(100, 80, bias=False)
init.normal(self.fc1.weight, mean=0, std=1)
self.fc2 = nn.Linear(80, 87)
self.fc3 = nn.Linear(87, 94)
self.fc4 = nn.Linear(94, 100)
def forward(self, x):
x = self.fc1(x)
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.relu(self.fc4(x))
return x
也许我查看了错误的参数,尽管我检查了文档。感谢您的帮助!
使用model.parameters()
获取任何模型或层的可训练权重。记得放在list()里面,不然打印不出来
以下代码片段有效
>>> import torch
>>> import torch.nn as nn
>>> l = nn.Linear(3,5)
>>> w = list(l.parameters())
>>> w