【发布时间】:2020-10-02 22:03:13
【问题描述】:
我正在尝试将torch.nn.Parameters 转换为稀疏张量。 Pytorch 文档说Parameters 是Tensor's 子类。张量支持to_sparse 方法,但如果我将Parameters 转换为稀疏,它会给我:TypeError: cannot assign 'torch.cuda.sparse.FloatTensor' as parameter 'weight' (torch.nn.Parameter or None expected)
有没有办法绕过这个并使用稀疏张量作为参数?强>
这是产生问题的示例代码:
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()
【问题讨论】:
-
module.weight.data = module.weight.data.to_sparse()或from torch.nn.parameter import Parameter \n module.weight = Parameter(module.weight.data.to_sparse()) -
您好,请回答,以便我标记它
-
哈哈,没关系,解决问题就行了
标签: python neural-network pytorch conv-neural-network tensor