简体   繁体   English

Cnn model 使用 pytorch

[英]Cnn model using pytorch

I have images and labels.我有图像和标签。 I divided them into test and train sets.我将它们分为测试集和训练集。 (xtrain, ytrain, xtest, ytest). (xtrain,ytrain,xtest,ytest)。 x refers to images and y refers to label. x 指图像,y 指 label。 How to use these sets in the following train model如何在以下列车中使用这些套装 model

 **# 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)**
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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM