我如何创建一个损失函数来推动实际的 NN 权重移动?
How can I create a loss function that will push the actual NN weights to move?
我有一个简单的神经网络:
import torch
import torch.nn as nn
import torch.optim as optim
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.fc1 = nn.Linear(1, 5)
self.fc2 = nn.Linear(5, 10)
self.fc3 = nn.Linear(10, 1)
def forward(self, x):
x = self.fc1(x)
x = torch.relu(x)
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Model()
opt = optim.Adam(net.parameters())
我还有一些输入功能:
features = torch.rand((3,1))
我可以用一个简单的MSE损失函数正常训练它:
for i in range(10):
opt.zero_grad()
out = net(features)
loss = torch.mean(torch.square(torch.tensor(5) - torch.sum(out)))
print('loss:', loss)
loss.backward()
opt.step()
但是,我正在尝试创建一个将实际权重值考虑在内的损失函数:
loss = 1 - torch.mean(torch.tensor([torch.sum(w_arr) for w_arr in net.parameters()]))
但是我收到一个错误:
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
这里的目标是让每个权重的值尽可能接近 1(或任何其他值)。
一个快速的错误修复方法是在创建张量时包含 requires_grad = True
。这样-
loss = 1 - torch.mean(torch.tensor([torch.sum(w_arr) for w_arr in net.parameters()], requires_grad=True))
但是当将权重列表转换为张量时,torch 不知道该张量的来源,因此损失不会减少。一种方法是
for i in range(500):
opt.zero_grad()
out = net(features)
loss = torch.mean(torch.square(torch.tensor(5) - torch.sum(out)))
len_w = 0
for w_arr in net.parameters():
loss += torch.mean(torch.abs(1 - w_arr))
len_w += 1
loss /= len_w
print('loss:', loss)
loss.backward()
opt.step()
这种损失计算方式,确保所有权重都接近+1
。
我有一个简单的神经网络:
import torch
import torch.nn as nn
import torch.optim as optim
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.fc1 = nn.Linear(1, 5)
self.fc2 = nn.Linear(5, 10)
self.fc3 = nn.Linear(10, 1)
def forward(self, x):
x = self.fc1(x)
x = torch.relu(x)
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Model()
opt = optim.Adam(net.parameters())
我还有一些输入功能:
features = torch.rand((3,1))
我可以用一个简单的MSE损失函数正常训练它:
for i in range(10):
opt.zero_grad()
out = net(features)
loss = torch.mean(torch.square(torch.tensor(5) - torch.sum(out)))
print('loss:', loss)
loss.backward()
opt.step()
但是,我正在尝试创建一个将实际权重值考虑在内的损失函数:
loss = 1 - torch.mean(torch.tensor([torch.sum(w_arr) for w_arr in net.parameters()]))
但是我收到一个错误:
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
这里的目标是让每个权重的值尽可能接近 1(或任何其他值)。
一个快速的错误修复方法是在创建张量时包含 requires_grad = True
。这样-
loss = 1 - torch.mean(torch.tensor([torch.sum(w_arr) for w_arr in net.parameters()], requires_grad=True))
但是当将权重列表转换为张量时,torch 不知道该张量的来源,因此损失不会减少。一种方法是
for i in range(500):
opt.zero_grad()
out = net(features)
loss = torch.mean(torch.square(torch.tensor(5) - torch.sum(out)))
len_w = 0
for w_arr in net.parameters():
loss += torch.mean(torch.abs(1 - w_arr))
len_w += 1
loss /= len_w
print('loss:', loss)
loss.backward()
opt.step()
这种损失计算方式,确保所有权重都接近+1
。