Can you change this code to use gpu? Instantiate the model, optimizer, and loss function…
- User
Can you change this code to use gpu?
Instantiate the model, optimizer, and loss function
model = TextClassificationModel() optimizer = optim.Adam(model.parameters(), lr=1e-5) criterion = nn.CrossEntropyLoss()
Train the model
for epoch in range(5): running_loss = 0.0 for inputs, attention_mask, labels in train_dataloader: optimizer.zero_grad() outputs = model(inputs, attention_mask) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() print(f"Epoch {epoch+1} Loss: {running_loss/len(train_dataloader)}")
Test the model on the test data
test_dataset = TextClassificationDataset(test_data, tokenizer) test_dataloader = DataLoader(test_dataset, batch_size=32, shuffle=False)
- Assistant
To use GPU in the code, you need to first move the model and its parameters to the GPU device, and then move the inputs, attention masks, and labels to the GPU device during training.