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

Python PyTorch:如何编写只返回权重的神经网络?,python,neural-network,pytorch,Python,Neural Network,Pytorch,我正在训练一个神经网络来学习一些权重,然后根据这些权重,我计算转换,生成预测模型和权重。我的网络无法正常学习,因此我正在编写一个不同的网络,它只返回独立于输入x(使用softmax和transpose标准化后)的权重。这样,我想知道问题是在网络中还是在网络外的转换估计中。但这不起作用。这就是我得到的 class DoNothingNet(torch.nn.Module): def __init__(self, n_vertices=6890, n_joints=14):

我正在训练一个神经网络来学习一些权重,然后根据这些权重,我计算转换,生成预测模型和权重。我的网络无法正常学习,因此我正在编写一个不同的网络,它只返回独立于输入
x
(使用softmax和transpose标准化后)的权重。这样,我想知道问题是在网络中还是在网络外的转换估计中。但这不起作用。这就是我得到的

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:无法将'torch.cuda.FloatTensor'指定为参数'weights'(torch.nn.parameter或不需要)
。我该如何解决这个问题?代码有意义吗?

nn.Module跟踪nn.Parameter类型的所有字段以进行培训。在代码中,每次向前调用都试图通过将参数权重指定给张量类型来更改参数权重,因此会发生错误

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

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)