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

Overload of nonzero is deprecated #11

Open
claudiohfg opened this issue Jul 17, 2020 · 3 comments
Open

Overload of nonzero is deprecated #11

claudiohfg opened this issue Jul 17, 2020 · 3 comments

Comments

@claudiohfg
Copy link

Hi, I was executing the example and got the following warning:

[running kmeans]: 0it [00:00, ?it/s]

running k-means on cpu..

/pytorch/torch/csrc/utils/python_arg_parser.cpp:756: UserWarning: This overload of nonzero is deprecated:
	nonzero(Tensor input, *, Tensor out)
Consider using one of the following signatures instead:
	nonzero(Tensor input, *, bool as_tuple)
[running kmeans]: 7it [00:02,  3.24it/s, center_shift=0.000091, iteration=7, tol=0.000100] 

Here is the code I ran to get this message.

import torch
import numpy as np
import matplotlib.pyplot as plt
from kmeans_pytorch import kmeans, kmeans_predict

# set random seed
np.random.seed(123)

# data
data_size, dims, num_clusters = 500000, 100, 3
x = np.random.randn(data_size, dims) / 6
x = torch.from_numpy(x)

# set device
if torch.cuda.is_available():
    device = torch.device('cuda:0')
else:
    device = torch.device('cpu')

# k-means
cluster_ids_x, cluster_centers = kmeans(
    X=x, num_clusters=num_clusters, distance='euclidean', device=device
)

Thanks in advance.

@cbroker1
Copy link

yep. same.

@tomerip
Copy link

tomerip commented Sep 24, 2020

This was solved by PyTorch in this PR: pytorch/vision#2705
A solution that follows this logic without upgrading PyTorch is replacing this line:
selected = torch.nonzero(choice_cluster == index).squeeze().to(device)
with this line:
selected = torch.where((choice_cluster == index) == True)[0].squeeze().to(device)

@sunhao123456789
Copy link

怎么解决啊大佬们

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

No branches or pull requests

4 participants