Hi !, I just started training my model with PyTorch. However, I met a problem when I trained my model with GPU. The error message is shown below.
RuntimeError: Tensor for argument #2 ‘weight’ is on CPU, but expected it to be on GPU (while checking arguments for cudnn_batch_norm)
And this is my code of model
class BN(nn.Module):
def init(self, input):
super(BN, self).init()
self.input = input
self.bn = nn.BatchNorm1d(input.size()[1], momentum=0.5)
self.bn.cuda()
def forward(self, x):
x = self.bn(x.float())
x = torch.as_tensor(x).long()
return x
class DRLSTM(nn.Module):
def init(self,
vocab_size,
embedding_dim,
hidden_size,
embeddings=None,
padding_idx=0,
dropout=0.5,
num_classes=3,
device=“cpu”):
super(DRLSTM, self).__init__()
self.vocab_size = vocab_size
self.embedding_dim = embedding_dim
self.hidden_size = hidden_size
self.num_classes = num_classes
self.dropout = dropout
self.device = device
self.bn = nn.BatchNorm1d(64, momentum=0.8).to(self.device)
self.debug = False
self._word_embedding = nn.Embedding(self.vocab_size,
self.embedding_dim,
padding_idx=padding_idx,
_weight=embeddings)
# print ('embedding_dim: ')
# print (embedding_dim)
if self.dropout:
self._rnn_dropout = RNNDropout(p=self.dropout)
# self._rnn_dropout = nn.Dropout(p=self.dropout)
self._encoding = Seq2SeqEncoder(nn.LSTM,
self.embedding_dim,
self.hidden_size,
bidirectional=True)
# self._encoding1 = Seq2SeqEncoder(nn.LSTM,
# self.embedding_dim,
# int(self.hidden_size/2),
# bidirectional=True)
# self._encoding2 = Seq2SeqEncoder(nn.LSTM,
# self.embedding_dim,
# #self.hidden_size,
# self.hidden_size,
# bidirectional=True)
self._attention = SoftmaxAttention()
self._projection = nn.Sequential(nn.Linear(4 * 2 * self.hidden_size,
self.hidden_size),
nn.ReLU()
)
self._composition = Seq2SeqEncoder(nn.LSTM,
self.hidden_size,
self.hidden_size,
bidirectional=True)
self._composition1 = Seq2SeqEncoder(nn.LSTM,
self.hidden_size,
self.hidden_size,
bidirectional=True)
self._composition2 = Seq2SeqEncoder(nn.LSTM,
2 * self.hidden_size,
self.hidden_size,
bidirectional=True)
self._classification = nn.Sequential(nn.Dropout(p=self.dropout),
nn.Linear(2 * 4 * self.hidden_size,
self.hidden_size),
nn.Tanh(),
nn.Dropout(p=self.dropout),
nn.Linear(self.hidden_size,
self.num_classes))
# Initialize all weights and biases in the model.
self.apply(_init_model_weights)
def forward(self,
premises,
premises_lengths,
hypotheses,
hypotheses_lengths):
premises_mask = get_mask(premises, premises_lengths).to(self.device)
hypotheses_mask = get_mask(hypotheses, hypotheses_lengths) \
.to(self.device)
# BN1 = nn.BatchNorm1d(premises.size()[1], momentum=0.5).to(self.device)
# BN2 = nn.BatchNorm1d(hypotheses.size()[1], momentum=0.5).to(self.device)
# premises = BN1(premises.float())
# hypotheses = BN2(hypotheses.float())
# premises = self.bn(premises.float())
# hypotheses = self.bn(hypotheses.float())
# premises = torch.as_tensor(premises).long()
# hypotheses = torch.as_tensor(hypotheses).long()
bn1 = BN(premises)
premises = bn1(premises)
bn2 = BN(hypotheses)
hypotheses = bn2(hypotheses)
I wonder why the error comes.
Thank you!