Python 将权重和偏差转换为稀疏张量
我正在尝试将Python 将权重和偏差转换为稀疏张量,python,neural-network,pytorch,conv-neural-network,tensor,Python,Neural Network,Pytorch,Conv Neural Network,Tensor,我正在尝试将torch.nn.Parameters转换为稀疏张量。Pytorch文档说参数是一个子类。张量支持到_sparse方法,但如果我将参数转换为sparse,它将给出: TypeError:无法将'torch.cuda.sparse.FloatTensor'指定为参数'weight'(torch.nn.parameter或不需要) 有没有办法绕过这一点,对参数使用稀疏张量? 下面是产生问题的示例代码: for name, module in net.named_modules():
torch.nn.Parameters
转换为稀疏张量。Pytorch文档说参数是一个子类。张量支持到_sparse
方法,但如果我将参数
转换为sparse,它将给出:TypeError:无法将'torch.cuda.sparse.FloatTensor'指定为参数'weight'(torch.nn.parameter或不需要)
有没有办法绕过这一点,对参数使用稀疏张量?
下面是产生问题的示例代码:
for name, module in net.named_modules():
if isinstance(module, torch.nn.Conv2d):
module.weight = module.weight.data.to_sparse()
module.bias = module.bias.data.to_sparse()
返回无法分配给模块.weight
的张量的稀疏副本,因为这是torch.nn.参数的实例。因此,你应该:
module.weight = torch.nn.Parameter(module.weight.data.to_sparse())
module.bias = torch.nn.Parameter(module.bias.data.to_sparse())
请注意,Parameters
是一种特定类型的张量,标记为来自nn.Module
的参数,因此它们不同于普通张量。Module.weight.data=Module.weight.data.to_sparse()
或来自torch.nn.parameter导入参数\n Module.weight=参数(module.weight.data.to_sparse())
Hi,请回答,以便我标记itHaha,没关系,只要解决问题即可