PyTorch:如何编写一个只返回权重的神经网络?



我正在训练一个学习一些权重的神经网络,并根据这些权重计算转换,这些转换将产生预测模型与权重相结合。我的网络无法正确学习,因此我正在编写一个不同的网络,它除了返回独立于输入x的权重外什么都不做(在使用 softmax 和转置进行规范化之后(。这样,我想找出问题出在网络还是出在网络外的变换估计中。但这行不通。这就是我所拥有的。

class DoNothingNet(torch.nn.Module):
def __init__(self, n_vertices=6890, n_joints=14):
super(DoNothingNet, self).__init__()
self.weights = nn.parameter.Parameter(torch.randn(n_vertices, n_joints))
def forward(self, x, indices):
self.weights = F.softmax(self.weights, dim=1)
return self.weights.transpose(0,1)

但是行self.weights = F.softmax(self.weights, dim=1)不起作用并产生错误TypeError: cannot assign 'torch.cuda.FloatTensor' as parameter 'weights' (torch.nn.Parameter or None expected)。我该如何解决这个问题?代码甚至有意义吗?

nn.模块跟踪 nn 类型的所有字段。用于训练的参数。在代码中,每次转发调用您都会尝试通过将参数权重分配给张量类型来更改参数权重,因此会发生错误。

以下代码输出规范化权重,而不更改存储的权重。希望这会有所帮助。

import torch
from torch import nn
from torch.nn import functional as F
class DoNothingNet(torch.nn.Module):
def __init__(self, n_vertices=6890, n_joints=14):
super(DoNothingNet, self).__init__()
self.weights = nn.parameter.Parameter(torch.randn(n_vertices, n_joints))
def forward(self, x, indices):
output = F.softmax(self.weights, dim=1)
return output.transpose(0,1)

最新更新