yining043 / TSP-improve

An improvement-based Deep Reinforcement Learning Algorithm presented in paper https://arxiv.org/abs/1912.05784v2 for solving the TSP problem.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

I got an issues about the code

nnhhoang opened this issue · comments

It say that
assert (
48 torch.arange(rec.size(1), out=rec.data.new()).view(1, -1).expand_as(rec) ==
49 rec.data.sort(1)[0]
50 ).all(), "Invalid tour"
52 # Gather dataset in order of tour
---> 53 d = dataset.gather(1, rec.long().unsqueeze(-1).expand_as(dataset))
54 length = (d[:, 1:] - d[:, :-1]).norm(p=2, dim=2).sum(1) + (d[:, 0] - d[:, -1]).norm(p=2, dim=1)
56 return length

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument index in method wrapper_gather)

So how can fix this problem many thanks !

Hi, I have tried to run on my machine, it it seems to be no problem. Can you provide more details on how to reproduce the issue?

I have fix this issue in problem_tsp.py in function get_costs just add device = torch.device("cuda") then
d = dataset.to(device).gather(1, rec.long().unsqueeze(-1).expand_as(dataset))

but i have the same issue about device in plot.py in line 75
fig = plt.figure(figsize=(8,6))
71 index = torch.cat((
72 city_tour.view(-1,1),
73 city_tour.view(-1,1)[None,0]),0).repeat(1,2).long()
---> 75 xy = torch.gather(coordinates,0,index)
76 plt.xticks(fontsize=12)
77 plt.yticks(fontsize=12)

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:0! (when checking argument for argument index in method wrapper_gather)

Hi, you may find the solution in issue #2 helpful to this problem. Meanwhile, you can also remove the codes regarding the plotting of the graph, since this is only for tensorboard logging and not necessary for training and test.