1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model.to(device) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=0.001) scaler = torch.cuda.amp.GradScaler()
for epoch in range(10): model.train() running_loss = 0.0 for inputs, labels in dataloader: inputs, labels = inputs.to(device), labels.to(device) optimizer.zero_grad() with torch.cuda.amp.autocast(): outputs = model(inputs) loss = criterion(outputs, labels) scaler.scale(loss).backward() scaler.step(optimizer) scaler.update() running_loss += loss.item() scheduler.step() model.eval() correct = 0 total = 0 with torch.no_grad(): for inputs, labels in val_dataloader: inputs, labels = inputs.to(device), labels.to(device) outputs = model(inputs) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print(f'Epoch {epoch+1}, Loss: {running_loss/len(dataloader):.4f}, ' f'Val Accuracy: {100*correct/total:.2f}%')
torch.save(model.state_dict(), 'model.pth') model.load_state_dict(torch.load('model.pth', map_location=device))
torch.save(model, 'full_model.pth') model = torch.load('full_model.pth', map_location=device)
|