I have a very small network like this:
import torch.nn as nn
class ConEcNet(nn.Module):
def __init__(self, input_dim, embedding_dim):
super(ConEcNet, self).__init__()
self.W_inputs = nn.Parameter(0.1*torch.randn(input_dim))
self.W_0 = nn.Linear(input_dim, embedding_dim)
def forward(self, inputs):
return self.W_0(inputs * self.W_inputs)
i.e. the input vector is first multiplied elementwise by the vector in W_inputs
(i.e. each input feature has an individual importance weight) and then passed to the linear layer W_0
. This works fine if my inputs are a regular tensor, however, if the inputs are a sparse tensor, the first multiplication (inputs * self.W_inputs
) results in a RuntimeError: mul operands have incompatible sizes
.
Since I’m working with BOW features, I would really like to use sparse inputs. Does nn.Parameter
in general not work with sparse tensors? Is there a way around this besides casting the inputs to a regular tensor?
I’m using torch version 1.5.0
.
Example:
import numpy as np
from scipy.sparse import coo_matrix
import torch
def coo2tensor(coo):
"""
convert scipy sparse coo matrix to sparse torch tensor
"""
values = coo.data
indices = np.vstack((coo.row, coo.col))
i = torch.LongTensor(indices)
v = torch.FloatTensor(values)
shape = coo.shape
return torch.sparse.FloatTensor(i, v, torch.Size(shape))
cnet = ConEcNet(10, 5)
a = torch.randn(3, 10)
cnet(a) # works
b = coo2tensor(coo_matrix(np.random.randn(3, 10)))
cnet(c) # runtime error
cnet.W_0(c) # works