Torch在PyTorch上分配零GPU内存

问题描述 投票:1回答:1

我正在尝试使用GPU来训练我的模型,但似乎火炬无法分配GPU内存。

我的模型是基于PyTorch构建的RNN

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

rnn = RNN(n_letters, n_hidden, n_categories_train)
rnn.to(device)
criterion = nn.NLLLoss()
criterion.to(device)
optimizer = torch.optim.SGD(rnn.parameters(), lr=learning_rate, weight_decay=.9)
class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(RNN, self).__init__()

        self.input_size = input_size
        self.hidden_size = hidden_size
        self.output_size = output_size

        self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
        self.i2o = nn.Linear(input_size + hidden_size, output_size)

        self.softmax = nn.LogSoftmax(dim=1)

    def forward(self, input, hidden):
        input = input.cuda()
        hidden = hidden.cuda()

        combined = torch.cat((input, hidden), 1)
        hidden = self.i2h(combined)
        output = self.i2o(combined)
        output = self.softmax(output)

        output = output.cuda()
        hidden = hidden.cuda()

        return output, hidden

    def init_hidden(self):
        return Variable(torch.zeros(1, self.hidden_size).cuda())

培训功能:

def train(category_tensor, line_tensor, rnn, optimizer, criterion):
    rnn.zero_grad()
    hidden = rnn.init_hidden()

    for i in range(line_tensor.size()[0]):
        output, hidden = rnn(line_tensor[i], hidden)

    loss = criterion(output, category_tensor)
    loss.backward()

    optimizer.step()

    return output, loss.item()

获取category_tensor和line_tensor的函数:

def random_training_pair(category_lines, n_letters, all_letters):
    category = random.choice(all_categories_train)
    line = random.choice(category_lines[category])
    category_tensor = Variable(torch.LongTensor([all_categories_train.index(category)]).cuda())
    line_tensor = Variable(process_data.line_to_tensor(line, n_letters, all_letters)).cuda()

    return category, line, category_tensor, line_tensor

我运行了以下代码:

 print(torch.cuda.get_device_name(0))
 print('Memory Usage:')
 print('Allocated:', round(torch.cuda.memory_allocated(0) / 1024 ** 3, 1), 'GB')
 print('Cached:   ', round(torch.cuda.memory_cached(0) / 1024 ** 3, 1), 'GB')

我得到了:

GeForce GTX 1060
Memory Usage:
Allocated: 0.0 GB
Cached:    0.0 GB

我没有得到任何错误,但GPU使用率仅为1%,而CPU使用率约为31%。

我正在使用安装了PyTorch的Windows 10和Anaconda。 CUDA和cuDNN是从Nvidia网站下载的.exe文件安装的。

python deep-learning pytorch torch
1个回答
2
投票

你的问题是to()不是就地操作。如果您调用rnn.to(device),它将返回位于所需设备上的新对象/模型。但它不会将旧物体移动到任何地方!

如此改变:

rnn = RNN(n_letters, n_hidden, n_categories_train)
rnn.to(device)

至:

rnn = RNN(n_letters, n_hidden, n_categories_train).to(device)

对于你用这种方式使用to的所有其他实例,你也必须改变它。

应该为你做的伎俩!

注意:您执行操作的所有张量和参数必须位于同一设备上。如果您的模型在GPU上但输入张量在CPU上,则会收到错误消息。

© www.soinside.com 2019 - 2024. All rights reserved.