当前位置:网站首页>RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an

RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an

2022-06-12 08:51:00 Hey, it's me

The reason for the error : Input x And the output y( Or model parameters ) Different storage locations

This error is mainly due to input x And the output y( Or model parameters ) Different storage locations
If you are error 1: Input x stay cuda(gpu) in , The model parameters are in cpu in
Want to input x Put in gpu in , Generally, it is to find the input parameters x, Then call the use parameter x Add a line of code before x.to(device)( among device=“cuda”)
If you are error 2: Input x stay cpu in , Model parameters cuda(gpu) In the middle
Find the definition model Code for , Add a line of code after the definition model.to(device)
The specific operation is as follows :

error 1:RuntimeError: Input and parameter tensors are not at the same device, found input tensor at cuda:0 and parameter tensor at cpu

1.1 Input x stay cuda(gpu) in , The model parameters are in cpu in

Test code demo: :
When the input x stay gpu, however model Store in cpu in So running the following code will report an error 1

import torch
import torch.nn as nn
from torch.nn import LSTM
device = "cuda" if torch.cuda.is_available() else "cpu"     #  Yes gpu use gpu,  It doesn't work cpu
x = torch.Tensor([[1,2,3], [2,3,4]])  # x shape (2,3)  (seq_len,  Word vector dimension )
class Testmodel(nn.Module):
    def __init__(self, input_dim, lstm_layer, lstm_hidden_dim, dropout):
        super(Testmodel, self).__init__()
        self.lstm_encoding = LSTM(input_dim, num_layers=lstm_layer, hidden_size=lstm_hidden_dim,
                                  dropout=0.5)  #
    def forward(self, x: torch.Tensor):
        output, (hn, cn) = self.lstm_encoding(x)
        return output

model = Testmodel(
    input_dim=3,
    lstm_layer=2,
    lstm_hidden_dim=4,
    dropout=0.5,
)

#  When the input x stay gpu,  however model Store in cpu in   So there's an error 
x = x.to(device)    #  take x Put in gpu In the memory 
output = model(x)  #  call forward Method  x (2,3) lstm  Input dimensions 3,  Output dimension 4,
print(output)   # output shape (2,4)

1.2 resolvent

Method 1: Comment the following code directly , Enter x Put in cpu Memory and output are consistent

x = x.to(device)    #  take x Put in gpu In the memory 

Method 2( recommend ): Add a line of code model.to(device), Put the parameters of the model into gpu in , And the input x Consistent position , The case code after modification is as follows

import torch
import torch.nn as nn
from torch.nn import LSTM
device = "cuda" if torch.cuda.is_available() else "cpu"     #  Yes gpu use gpu,  It doesn't work cpu
x = torch.Tensor([[1,2,3], [2,3,4]])  # x shape (2,3) (seq_len,  Word vector dimension )
class Testmodel(nn.Module):
    def __init__(self, input_dim, lstm_layer, lstm_hidden_dim, dropout):
        super(Testmodel, self).__init__()
        self.lstm_encoding = LSTM(input_dim, num_layers=lstm_layer, hidden_size=lstm_hidden_dim,
                                  dropout=0.5)  #
    def forward(self, x: torch.Tensor):
        output, (hn, cn) = self.lstm_encoding(x)
        return output

model = Testmodel(
    input_dim=3,
    lstm_layer=2,
    lstm_hidden_dim=4,
    dropout=0.5,
)
model.to(device)  # !!!!!!!!!!!! The newly added code is here 
#  When the input x stay gpu,  however model stay cpu in   So there's an error 
x = x.to(device)    #  take x Put in gpu In the memory 
output = model(x)  #  call forward Method  x (2,3) lstm  Input dimensions 3,  Output dimension 4,
print(output)   # output shape (2,4)

error 2.RuntimeError: Input and parameter tensors are not at the same device, found input tensor at cpu and parameter tensor at cuda:0

2.1 Enter in cpu in , Output ( Model parameters ) stay cuda(gpu) in

Test code demo:
When the input x stay cpu, however model Store in gpu in So running the following code will report an error 2

import torch
import torch.nn as nn
from torch.nn import LSTM
device = "cuda" if torch.cuda.is_available() else "cpu"     #  Yes gpu use gpu,  It doesn't work cpu
x = torch.Tensor([[1,2,3], [2,3,4]])  # x shape (2,3)  (seq_len,  Word vector dimension )
class Testmodel(nn.Module):
    def __init__(self, input_dim, lstm_layer, lstm_hidden_dim, dropout):
        super(Testmodel, self).__init__()
        self.lstm_encoding = LSTM(input_dim, num_layers=lstm_layer, hidden_size=lstm_hidden_dim,
                                  dropout=0.5)  #
    def forward(self, x: torch.Tensor):
        output, (hn, cn) = self.lstm_encoding(x)
        return output

model = Testmodel(
    input_dim=3,
    lstm_layer=2,
    lstm_hidden_dim=4,
    dropout=0.5,
)
model.to(device)    #  Put the model parameters in gpu in 
#  When the input x stay gpu,  however model stay cpu in   So there's an error 

output = model(x)  #  call forward Method  x (2,3) lstm  Input dimensions 3,  Output dimension 4,
print(output)   # output shape (2,4)

2.2 resolvent

Method 1: Find the code model.to(device) Direct comments , Put model parameters into cpu In memory and input x Consistent position

model.to(device)    #  Put the model parameters in gpu in 

Method 2( recommend ): Add a line of code x = x.to(device) , Put the parameters of the model into gpu in , And the input x Consistent position , The case code after modification is as follows

import torch
import torch.nn as nn
from torch.nn import LSTM
device = "cuda" if torch.cuda.is_available() else "cpu"     #  Yes gpu use gpu,  It doesn't work cpu
x = torch.Tensor([[1,2,3], [2,3,4]])  # x shape (2,3) (seq_len,  Word vector dimension )
class Testmodel(nn.Module):
    def __init__(self, input_dim, lstm_layer, lstm_hidden_dim, dropout):
        super(Testmodel, self).__init__()
        self.lstm_encoding = LSTM(input_dim, num_layers=lstm_layer, hidden_size=lstm_hidden_dim,
                                  dropout=0.5)  #
    def forward(self, x: torch.Tensor):
        output, (hn, cn) = self.lstm_encoding(x)
        return output

model = Testmodel(
    input_dim=3,
    lstm_layer=2,
    lstm_hidden_dim=4,
    dropout=0.5,
)
model.to(device)  #  Put model parameters into gpu In the memory 
#  When the input x stay gpu,  however model stay cpu in   So there's an error 
x = x.to(device)    # !!!!!!!!!!!! The newly added code is here 
output = model(x)  #  call forward Method  x (2,3) lstm  Input dimensions 3,  Output dimension 4,
print(output)   # output shape (2,4)

My level is limited , If you have any mistakes, please correct them

原网站

版权声明
本文为[Hey, it's me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120843417777.html