【发布时间】:2021-08-06 18:05:52
【问题描述】:
我想将张量初始化为稀疏张量。
当张量的维数为2时,我可以使用torch.nn.init.sparse(tensor, sparsity=0.1)
import torch
dim = torch.Size([3,2])
w = torch.Tensor(dim)
torch.nn.init.sparse_(w, sparsity=0.1)
结果
tensor([[ 0.0000, 0.0147],
[-0.0190, 0.0004],
[-0.0004, 0.0000]])
但是当张量维度 > 2 时,这个功能就不起作用了。
v = torch.Tensor(torch.Size([5,5,30,2]))
torch.nn.init.sparse_(v, sparsity=0.1)
结果
ValueError: Only tensors with 2 dimensions are supported
我需要这个,因为我想用它来初始化卷积权重。
torch.nn.init.sparse_() 函数的定义如下
def sparse_(tensor, sparsity, std=0.01):
r"""Fills the 2D input `Tensor` as a sparse matrix, where the
non-zero elements will be drawn from the normal distribution
:math:`\mathcal{N}(0, 0.01)`, as described in `Deep learning via
Hessian-free optimization` - Martens, J. (2010).
Args:
tensor: an n-dimensional `torch.Tensor`
sparsity: The fraction of elements in each column to be set to zero
std: the standard deviation of the normal distribution used to generate
the non-zero values
Examples:
>>> w = torch.empty(3, 5)
>>> nn.init.sparse_(w, sparsity=0.1)
"""
if tensor.ndimension() != 2:
raise ValueError("Only tensors with 2 dimensions are supported")
rows, cols = tensor.shape
num_zeros = int(math.ceil(sparsity * rows))
with torch.no_grad():
tensor.normal_(0, std)
for col_idx in range(cols):
row_indices = torch.randperm(rows)
zero_indices = row_indices[:num_zeros]
tensor[zero_indices, col_idx] = 0
return tensor
如何制作 n 维稀疏张量?
pytorch 中有没有办法创建这种张量?
或者我可以换一种方式吗?
【问题讨论】:
标签: python python-3.x pytorch