Pytorch running slower on GPU than CPU












0














I'm fairly new to python/machine learning and I've been trying to learn PyTorch. Here's a tutorial I've been following:
https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html
, which is basically a very simple RNN to classify name into its language. When I run it on a CPU, the final results are



100000 100% (2m 25s) 0.3983 Tsujimoto / Japanese ✓
Training completed! Time taken: 145.90 seconds.


But when I run it on a GPU I get the follwing



100000 100% (5m 56s) 0.1462 Mcgregor / Scottish ✓
Training completed! Time taken: 356.96 seconds.


The only thing I changed was the train function by sending the input tensors to device (device = torch.device("cuda:3" if torch.cuda.is_available() else "cpu")) and of course putting the rnn itself to the GPU (rnn = RNN(n_letters, n_hidden, n_categories).to(device):



def train(category_tensor, line_tensor):
hidden = rnn.initHidden().to(device)
category_tensor, line_tensor = category_tensor.to(device), line_tensor.to(device)
rnn.zero_grad()
for i in range(line_tensor.size()[0]):
output, hidden = rnn(line_tensor[i], hidden)
loss = F.nll_loss(output, category_tensor)
loss.backward()
optimizer.step()
return output, loss.item()


Am I doing something wrong?



Thanks in advance!










share|improve this question


















  • 2




    You may try different hidden_sizes, see here for this: stackoverflow.com/questions/53325418/… So with higher dim parameters it might be that GPU wins over CPU, however in general RNNs with for-loops are difficult to parallelize and can be painfully slow on GPU. So do not expect too much gain of it. To get most of RNNs it's best to use batching so that computation can be done in parallel.
    – blue-phoenox
    Nov 20 '18 at 10:00


















0














I'm fairly new to python/machine learning and I've been trying to learn PyTorch. Here's a tutorial I've been following:
https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html
, which is basically a very simple RNN to classify name into its language. When I run it on a CPU, the final results are



100000 100% (2m 25s) 0.3983 Tsujimoto / Japanese ✓
Training completed! Time taken: 145.90 seconds.


But when I run it on a GPU I get the follwing



100000 100% (5m 56s) 0.1462 Mcgregor / Scottish ✓
Training completed! Time taken: 356.96 seconds.


The only thing I changed was the train function by sending the input tensors to device (device = torch.device("cuda:3" if torch.cuda.is_available() else "cpu")) and of course putting the rnn itself to the GPU (rnn = RNN(n_letters, n_hidden, n_categories).to(device):



def train(category_tensor, line_tensor):
hidden = rnn.initHidden().to(device)
category_tensor, line_tensor = category_tensor.to(device), line_tensor.to(device)
rnn.zero_grad()
for i in range(line_tensor.size()[0]):
output, hidden = rnn(line_tensor[i], hidden)
loss = F.nll_loss(output, category_tensor)
loss.backward()
optimizer.step()
return output, loss.item()


Am I doing something wrong?



Thanks in advance!










share|improve this question


















  • 2




    You may try different hidden_sizes, see here for this: stackoverflow.com/questions/53325418/… So with higher dim parameters it might be that GPU wins over CPU, however in general RNNs with for-loops are difficult to parallelize and can be painfully slow on GPU. So do not expect too much gain of it. To get most of RNNs it's best to use batching so that computation can be done in parallel.
    – blue-phoenox
    Nov 20 '18 at 10:00
















0












0








0


1





I'm fairly new to python/machine learning and I've been trying to learn PyTorch. Here's a tutorial I've been following:
https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html
, which is basically a very simple RNN to classify name into its language. When I run it on a CPU, the final results are



100000 100% (2m 25s) 0.3983 Tsujimoto / Japanese ✓
Training completed! Time taken: 145.90 seconds.


But when I run it on a GPU I get the follwing



100000 100% (5m 56s) 0.1462 Mcgregor / Scottish ✓
Training completed! Time taken: 356.96 seconds.


The only thing I changed was the train function by sending the input tensors to device (device = torch.device("cuda:3" if torch.cuda.is_available() else "cpu")) and of course putting the rnn itself to the GPU (rnn = RNN(n_letters, n_hidden, n_categories).to(device):



def train(category_tensor, line_tensor):
hidden = rnn.initHidden().to(device)
category_tensor, line_tensor = category_tensor.to(device), line_tensor.to(device)
rnn.zero_grad()
for i in range(line_tensor.size()[0]):
output, hidden = rnn(line_tensor[i], hidden)
loss = F.nll_loss(output, category_tensor)
loss.backward()
optimizer.step()
return output, loss.item()


Am I doing something wrong?



Thanks in advance!










share|improve this question













I'm fairly new to python/machine learning and I've been trying to learn PyTorch. Here's a tutorial I've been following:
https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html
, which is basically a very simple RNN to classify name into its language. When I run it on a CPU, the final results are



100000 100% (2m 25s) 0.3983 Tsujimoto / Japanese ✓
Training completed! Time taken: 145.90 seconds.


But when I run it on a GPU I get the follwing



100000 100% (5m 56s) 0.1462 Mcgregor / Scottish ✓
Training completed! Time taken: 356.96 seconds.


The only thing I changed was the train function by sending the input tensors to device (device = torch.device("cuda:3" if torch.cuda.is_available() else "cpu")) and of course putting the rnn itself to the GPU (rnn = RNN(n_letters, n_hidden, n_categories).to(device):



def train(category_tensor, line_tensor):
hidden = rnn.initHidden().to(device)
category_tensor, line_tensor = category_tensor.to(device), line_tensor.to(device)
rnn.zero_grad()
for i in range(line_tensor.size()[0]):
output, hidden = rnn(line_tensor[i], hidden)
loss = F.nll_loss(output, category_tensor)
loss.backward()
optimizer.step()
return output, loss.item()


Am I doing something wrong?



Thanks in advance!







python pytorch






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 8:47









Rcwt

1083




1083








  • 2




    You may try different hidden_sizes, see here for this: stackoverflow.com/questions/53325418/… So with higher dim parameters it might be that GPU wins over CPU, however in general RNNs with for-loops are difficult to parallelize and can be painfully slow on GPU. So do not expect too much gain of it. To get most of RNNs it's best to use batching so that computation can be done in parallel.
    – blue-phoenox
    Nov 20 '18 at 10:00
















  • 2




    You may try different hidden_sizes, see here for this: stackoverflow.com/questions/53325418/… So with higher dim parameters it might be that GPU wins over CPU, however in general RNNs with for-loops are difficult to parallelize and can be painfully slow on GPU. So do not expect too much gain of it. To get most of RNNs it's best to use batching so that computation can be done in parallel.
    – blue-phoenox
    Nov 20 '18 at 10:00










2




2




You may try different hidden_sizes, see here for this: stackoverflow.com/questions/53325418/… So with higher dim parameters it might be that GPU wins over CPU, however in general RNNs with for-loops are difficult to parallelize and can be painfully slow on GPU. So do not expect too much gain of it. To get most of RNNs it's best to use batching so that computation can be done in parallel.
– blue-phoenox
Nov 20 '18 at 10:00






You may try different hidden_sizes, see here for this: stackoverflow.com/questions/53325418/… So with higher dim parameters it might be that GPU wins over CPU, however in general RNNs with for-loops are difficult to parallelize and can be painfully slow on GPU. So do not expect too much gain of it. To get most of RNNs it's best to use batching so that computation can be done in parallel.
– blue-phoenox
Nov 20 '18 at 10:00



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389218%2fpytorch-running-slower-on-gpu-than-cpu%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389218%2fpytorch-running-slower-on-gpu-than-cpu%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]