首页 > 解决方案 > PyTorch - 稀疏张量没有步幅

问题描述

我正在使用 TF-IDF 方法和 LSTM 使用以下代码为包含 1000 条评论的小型数据集构建我的第一个情感分析模型。我正在通过预处理火车数据并将其提供给 Vectorizer,如下所示

def tfidf_features(X_train, X_val, X_test):
tfidf_vectorizer = TfidfVectorizer(analyzer='word', token_pattern = '(\S+)', min_df = 5, max_df = 
0.9, ngram_range=(1,2))
X_train=tfidf_vectorizer.fit_transform(X_train)
X_val=tfidf_vectorizer.transform(X_val)
X_test=tfidf_vectorizer.transform(X_test)

return X_train, X_val, X_test, tfidf_vectorizer.vocabulary_

我正在使用以下代码将我的 csr_matrix 转换为 pytorch 张量

def spy_sparse2torch_sparse(data):
samples=data.shape[0]
features=data.shape[1]
values=data.data
coo_data=data.tocoo()
indices=torch.LongTensor([coo_data.row,coo_data.col])
t=torch.sparse.FloatTensor(indices,torch.from_numpy(values).float(),[samples,features])
return t

我得到了训练句子张量

   tensor(indices=tensor([[  0,   0,   1,  ..., 599, 599, 599],
                   [ 97, 131,  49,  ..., 109,  65,  49]]),
   values=tensor([0.6759, 0.7370, 0.6076,  ..., 0.3288, 0.3927, 0.3288]),
   size=(600, 145), nnz=1607, layout=torch.sparse_coo)

我正在使用下面的代码创建一个 TensorDataSet,其中我还将我的标签数据从颠簸转换为火炬张量

train_data = TensorDataset(train_x, torch.from_numpy(train_y))

我已经定义了我的 LSTM 网络并使用以下参数调用它

n_vocab = len(vocabulary)
n_embed = 100
n_hidden = 256
n_output = 1   # 1 ("positive") or 0 ("negative")
n_layers = 2

net = Sentiment_Lstm(n_vocab, n_embed, n_hidden, n_output, n_layers)

我还定义了损失和优化器。现在我正在使用以下代码训练我的模型

print_every = 100
step = 0
n_epochs = 4  # validation loss increases from ~ epoch 3 or 4
clip = 5  # for gradient clip to prevent exploding gradient problem in LSTM/RNN

for epoch in range(n_epochs):
h = net.init_hidden(batch_size)

for inputs, labels in train_loader:
    step += 1

    # making requires_grad = False for the latest set of h
    h = tuple([each.data for each in h])   

    net.zero_grad()
    output, h = net(inputs)
    loss = criterion(output.squeeze(), labels.float())
    loss.backward()
    nn.utils.clip_grad_norm(net.parameters(), clip)
    optimizer.step()

    if (step % print_every) == 0:
        net.eval()
        valid_losses = []
        v_h = net.init_hidden(batch_size)

        for v_inputs, v_labels in valid_loader:
            v_inputs, v_labels = inputs.to(device), labels.to(device)

            v_h = tuple([each.data for each in v_h])

            v_output, v_h = net(v_inputs)
            v_loss = criterion(v_output.squeeze(), v_labels.float())
            valid_losses.append(v_loss.item())

        print("Epoch: {}/{}".format((epoch+1), n_epochs),
              "Step: {}".format(step),
              "Training Loss: {:.4f}".format(loss.item()),
              "Validation Loss: {:.4f}".format(np.mean(valid_losses)))
        net.train()

但是,我在线上遇到了一个重大错误,output, h = net(inputs)因为RuntimeError: sparse tensors do not have strides

其他网站上给出的解决方法是无法理解的。我期待我需要进行确切的代码更改才能解决此问题。

标签: nlppytorchlstmtensortf-idf

解决方案


Pytorch 不支持稀疏 (S) 到稀疏矩阵的乘法。

让我们考虑一下:torch.sparse.mm(c1,c2),其中 c1 和 c2 是 sparse_coo_tensor 矩阵。

case1: 如果我们尝试将 c1 和 c2 设为 S --> 它会给出错误 RuntimeError: sparse tensors do not have strides。

case2:如果 c1 是密集的 (D) 而 c2 是 S --> 它给出了相同的错误。

case3:仅当 c1 为 S 且 c2 为 D 时 --> 工作正常。

参考:https ://blog.csdn.net/w55100/article/details/109086131

我猜您的 Sentiment_Lstm 中发生的矩阵乘法可能属于前两种情况。从而引发此错误。

通过使用密集输入格式,它应该可以工作。


推荐阅读