Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: fix UserWarning in get_norm_adj_mat and accelerate csr2tensor #1225

Merged
merged 2 commits into from
Apr 4, 2022

Conversation

Sherry-XLL
Copy link
Member

When running LightGCN, NGCF and NCL, there will be a UserWarning as follows:

UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor.

The reason is that in get_norm_adj_mat, the type of L.row and L.col is numpy.ndarray, so i =torch.LongTensor(np.array([row, col])) creates a tensor from a list of numpy.ndarrays.

# covert norm_adj matrix to tensor
L = sp.coo_matrix(L)
row = L.row
col = L.col
i = torch.LongTensor([row, col])

In SGL, numpy.ndarray is converted to list by .tolist() to avoid the warning.

x = torch.sparse.FloatTensor(
torch.LongTensor([matrix.row.tolist(), matrix.col.tolist()]),
torch.FloatTensor(matrix.data.astype(np.float32)), matrix.shape
).to(self.device)

I test the running time of three methods on ml-1m, and converting the list to a single numpy.ndarray with numpy.array()before converting to a tensor is the quickest.

  • 含 numpy 的 list 直接转 LongTensor:
stime = time.time()
row = L.row
col = L.col
i = torch.LongTensor([row, col])
etime = time.time()
print(f'用时: {etime-stime}s')
0.3401050567626953s
  • 先将 list 的 numpy 转为 list,再转 LongTensor:
stime = time.time()
row = L.row.tolist()
col = L.col.tolist()
i = torch.LongTensor([row, col])
etime = time.time()
print(f'用时: {etime-stime}s')
0.14787578582763672s
  • 含 numpy 的 list 先转 numpy 再转 LongTensor:
stime = time.time()
row = L.row
col = L.col
i = torch.LongTensor(np.array([row, col]))
etime = time.time()
print(f'用时: {etime-stime}s')
0.0045320987701416016s

@Sherry-XLL Sherry-XLL requested a review from 2017pxy April 4, 2022 03:01
@2017pxy 2017pxy merged commit 86b20cd into RUCAIBox:master Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants