Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5121,7 +5121,7 @@ def test_sparse_default_std(self):

for col_idx in range(input_tensor.size(1)):
column = input_tensor[:, col_idx]
assert column[column == 0].nelement() >= math.ceil(sparsity * cols)
assert column[column == 0].nelement() >= math.ceil(sparsity * rows)

assert self._is_normal(input_tensor[input_tensor != 0], 0, std)

Expand Down
9 changes: 3 additions & 6 deletions torch/nn/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,17 +360,14 @@ def sparse_(tensor, sparsity, std=0.01):
raise ValueError("Only tensors with 2 dimensions are supported")

rows, cols = tensor.shape
num_zeros = int(math.ceil(rows * sparsity))
num_zeros = int(math.ceil(sparsity * rows))

with torch.no_grad():
tensor.normal_(0, std)
for col_idx in range(cols):
row_indices = list(range(rows))
random.shuffle(row_indices)
row_indices = torch.randperm(rows)

This comment was marked as off-topic.

zero_indices = row_indices[:num_zeros]
for row_idx in zero_indices:
tensor[row_idx, col_idx] = 0

tensor[zero_indices, col_idx] = 0
return tensor


Expand Down