提问人:chungimungi 提问时间:11/2/2023 最后编辑:chungimungi 更新时间:11/2/2023 访问量:51
kaggle 上的 GPU 不使用 GPU 来训练模型
GPU on kaggle not using GPU for training the model
问:
我已经为在某个数据集上训练的自定义转换器编写了代码。我的问题在于模型没有利用我的 gpu,即使我已经根据我所知初始化了代码中的所有内容。有人可以帮我弄清楚为什么使用cpu进行训练吗?我使用 kaggle 的 P100 gpu 作为我的加速器。
import torch
import torch.nn as nn
import torch.optim as optim
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from torch.utils.data import Dataset, DataLoader
import numpy as np
import torchinfo
torch.manual_seed(42)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Read the CSV file
df = pd.read_csv("/kaggle/input/shakepase/sd.csv")
# Use LabelEncoder to encode the categorical data
le_play = LabelEncoder()
le_player = LabelEncoder()
le_player_line = LabelEncoder()
df['Play'] = torch.tensor(le_play.fit_transform(df['Play']))
df['Player'] = torch.tensor(le_player.fit_transform(df['Player']))
df['PlayerLine'] = torch.tensor(le_player_line.fit_transform(df['PlayerLine']))
class CustomQADataset(Dataset):
def __init__(self, data, num_classes):
self.data = data
self.num_classes = num_classes
def __len__(self):
return len(self.data)
def __getitem__(self, index):
play = self.data.iloc[index]['Play']
player = self.data.iloc[index]['Player']
player_line = self.data.iloc[index]['PlayerLine']
label = self.data.iloc[index]['Labels']
play = torch.tensor(play, dtype=torch.long)
player = torch.tensor(player, dtype=torch.long)
player_line = torch.tensor(player_line, dtype=torch.long)
label = torch.tensor(label, dtype=torch.long)
return play.to(device), player.to(device), player_line.to(device), label.to(device)
# Define the Transformer model
class QATransformerModel(nn.Module):
def __init__(self, vocab_size_play, vocab_size_player, vocab_size_player_line, d_model, nhead, num_encoder_layers, num_decoder_layers, dim_feedforward, num_classes, dropout=0.1):
super(QATransformerModel, self).__init__()
self.play_embedding = nn.Embedding(vocab_size_play, d_model)
self.player_embedding = nn.Embedding(vocab_size_player, d_model)
self.player_line_embedding = nn.Embedding(vocab_size_player_line, d_model)
self.transformer = nn.Transformer(d_model, nhead, num_encoder_layers, num_decoder_layers, dim_feedforward, dropout)
self.fc = nn.Linear(d_model, num_classes)
def forward(self, plays, players, player_lines):
play_emb = self.play_embedding(plays)
player_emb = self.player_embedding(players)
player_line_emb = self.player_line_embedding(player_lines)
# Combine the embeddings
combined = play_emb + player_emb + player_line_emb # Sum the embeddings instead of concatenating
# Transformer model
output = self.transformer(combined, combined) # Use the same data as source and target
# Linear layer for generating answers
output = self.fc(output) # No need to reshape the output
return output
# Define hyperparameters
vocab_size_play = len(le_play.classes_)
vocab_size_player = len(le_player.classes_)
vocab_size_player_line = len(le_player_line.classes_)
d_model = 128
nhead = 4
num_encoder_layers = 4
num_decoder_layers = 4
dim_feedforward = 256
num_classes = len(df['Labels'].unique())
dropout = 0.1
batch_size = 64
learning_rate = 0.001
num_epochs = 100
# Split the dataset into training and evaluation sets
train_df, eval_df = train_test_split(df, test_size=0.2, random_state=42)
# Create data loaders for both training and evaluation sets
train_dataset = CustomQADataset(train_df, num_classes)
eval_dataset = CustomQADataset(eval_df, num_classes)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
eval_loader = DataLoader(eval_dataset, batch_size=batch_size, shuffle=False)
# Define and create the model
model = QATransformerModel(vocab_size_play, vocab_size_player, vocab_size_player_line, d_model, nhead, num_encoder_layers, num_decoder_layers, dim_feedforward, num_classes, dropout).to(device)
# Move the model to the GPU(s)
model.to(device)
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
criterion = nn.CrossEntropyLoss()
for epoch in range(num_epochs):
model.train()
total_correct = 0
total_samples = 0
for play, player, player_line, label in train_loader:
optimizer.zero_grad()
output = model(play, player, player_line)
loss = criterion(output, label) # Use the 'Label' column as the target
loss.backward()
optimizer.step()
_, predicted = torch.max(output, 1)
total_correct += (predicted == label).sum().item()
total_samples += label.size(0)
model.eval()
eval_loss = 0.0
total_correct = 0
total_samples = 0
with torch.no_grad():
for play, player, player_line, label in eval_loader:
output = model(play, player, player_line)
loss = criterion(output, label)
eval_loss += loss.item()
_, predicted = torch.max(output, 1)
total_correct += (predicted == label).sum().item()
total_samples += label.size(0)
eval_loss /= len(eval_loader)
accuracy = total_correct / total_samples
print(f'Epoch [{epoch + 1}/{num_epochs}] - Eval Loss: {eval_loss:.4f} - Eval Accuracy: {accuracy:.4f}')
只是没有使用 gpu,因为它是一个相对较大的模型,要在 cpu 上训练,我不想花几个小时等待它在 cpu 上训练。
答: 暂无答案
评论
"cuda:0" if torch.cuda.is_available() else "cpu"
cuda:0
cuda:0
cpu
cuda:0