使用pytorch的CNN模型

时间:2020-07-10 11:17:16

标签: python image model pytorch cnn

我有图像和标签。我将它们分为测试和训练集。 (xtrain, 火车 xtest, ytest)。 x表示图像,y表示标签。 如何在以下火车模型中使用这些集合

 **# Train the model
  total_step = len(train_loader)
 for epoch in range(num_epochs):
   for i, (images, labels) in enumerate(train_loader):
    images = images.to(device)
    labels = labels.to(device)
    
    # Forward pass
    outputs = model(images)
    loss = criterion(outputs, labels)
    
    # Backward and optimize
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    if (i+1) % 100 == 0:
        print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}' 
               .format(epoch+1, num_epochs, i+1, total_step, loss.item()))
 # Test the model
 model.eval()  # eval mode (batchnorm uses moving mean/variance instead of mini-batch                           
                       mean/variance)**

1 个答案:

答案 0 :(得分:0)

from torch.utils.data import Dataset, DataLoader
training_set = Dataset(xtrain, ytrain)
test_set = Dataset(xtest, ytest)
params = {'batch_size': 64,
        'shuffle': True}
train_loader = DataLoader(training_set, **params)