Some one explain me how to do this

username: Manoj_Reddy
cooked: <p>error</p>
<h1>
<a name="iterate-over-data-1" class="anchor" href="#iterate-over-data-1"></a>Iterate over data.</h1>
<p>—&gt; 22             for i, data in enumerate(trainloader, 0):<br>
23                 inputs, labels = data<br>
24                 inputs, labels = inputs.cuda(),labels.cuda()</p>
<p>this is the code<br>
def train_model(model, criterion, optimizer, scheduler, num_epochs=25):<br>
since = time.time()</p>
<pre><code>best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0

for epoch in range(num_epochs):
    print('Epoch {}/{}'.format(epoch, num_epochs - 1))
    print('-' * 10)

    # Each epoch has a training and validation phase
    for phase in ['train', 'val']:
        if phase == 'train':
            model.train()  # Set model to training mode
        else:
            model.eval()   # Set model to evaluate mode

        running_loss = 0.0
        running_corrects = 0

        # Iterate over data.
        for i, data in enumerate(trainloader, 0):
            inputs, labels = data
            inputs, labels = inputs.cuda(),labels.cuda()

           

            optimizer.zero_grad()
            outputs = model(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            # statistics
           # print statistics
            running_loss += loss.item() * inputs.size(0)
            out = torch.argmax(outputs.detach(),dim=1)
            assert out.shape==labels.shape
            running_acc += (labels==out).sum().item()
        print(f"Train loss {epoch+1}: {running_loss/len(trainset)},Train Acc:{running_acc*100/len(trainset)}%")

        correct = 0
        model.train(True)
        with torch.no_grad():
            for inputs,labels in valloader:
                out = model(inputs.cuda()).cpu()
                out = torch.argmax(out,dim=1)
                acc = (out==labels).sum().item()
                correct += acc
        print(f"Val accuracy:{correct*100/len(valset)}%")
        if correct&gt;best_val_acc:
            best_val_acc = correct
            best_val_model = deepcopy(model.state_dict())
        lr_scheduler.step()</code></pre>

username: Maxwell_Albert
cooked: <aside class="quote no-group" data-username="Manoj_Reddy" data-post="1" data-topic="149143">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/manoj_reddy/48/48188_2.png" class="avatar"> Manoj_Reddy:</div>
<blockquote>
<p>for i, data in enumerate(trainloader, 0):</p>
</blockquote>
</aside>
<p>for i, data in enumerate(trainloader):</p>

username: Manoj_Reddy
cooked: <p>~\AppData\Local\Temp/ipykernel_13884/1642471687.py in train_model(model, criterion, optimizer, scheduler, num_epochs)<br>
8         running_loss = 0.0<br>
9         running_acc = 0<br>
—&gt; 10         for i, data in enumerate(trainloader, 0):<br>
11             inputs, labels = data<br>
12             inputs, labels = inputs.cuda(),labels.cuda()</p>
<p>NameError: name ‘trainloader’ is not defined</p>
<aside class="quote no-group quote-modified" data-username="Maxwell_Albert" data-post="2" data-topic="149143" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/maxwell_albert/48/31003_2.png" class="avatar"> Maxwell_Albert:</div>
<blockquote>
<aside class="quote no-group" data-username="Manoj_Reddy" data-post="1" data-topic="149143">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/manoj_reddy/48/48188_2.png" class="avatar"> Manoj_Reddy:</div>
<blockquote>
<p>for i, data in enumerate(trainloader, 0):</p>
</blockquote>
</aside>
<p>for i, data in enumerate(trainloader):</p>
</blockquote>
</aside>
<p>showing in this way but I don’t know how to solve it I am trying</p>

username: Maxwell_Albert
cooked: <p>This is because you have not define the trainloader, trainloader is a generator of the data you want.<br>
I will give you a example of how to have a trainloader(CIFAR10)</p>
<pre><code class="lang-auto">  transform_train = transforms.Compose(
    [
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
    ]
    )
    transform_test = transforms.Compose(
        [
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
        ]
    )

    trainset = torchvision.datasets.CIFAR10(
        root="../data", train=True, download=True, transform=transform_train
    )
    # train_sampler = torch.utils.data.distributed.DistributedSampler(trainset)
    train_loader = torch.utils.data.DataLoader(
        trainset,
        batch_size=args.batches,
        shuffle=True,
        num_workers=12,
        drop_last=True,
        # sampler=train_sampler,
        pin_memory=True,
    )

    testset = torchvision.datasets.CIFAR10(
        root="../data", train=False, download=True, transform=transform_test
    )
    val_loader = torch.utils.data.DataLoader(
        testset,
        batch_size=args.batches,
        shuffle=False,
        num_workers=12,
        drop_last=True,
        pin_memory=True,
    )
</code></pre>
<p>There are trainset and trainloader, transet is the list of training data(not modified), trainloader is the generator of the dataset and it could also do some augumentation and transforms.</p>

username: Manoj_Reddy
cooked: <p>from <strong>future</strong> import print_function, division</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.optim as optim<br>
from torch.optim import lr_scheduler<br>
import torch.backends.cudnn as cudnn<br>
import numpy as np<br>
import torchvision<br>
from torchvision import datasets, models, transforms<br>
import torchvision.transforms as transforms<br>
import matplotlib.pyplot as plt<br>
import time<br>
import os<br>
import copy</p>
<p>cudnn.benchmark = True<br>
plt.ion()   # interactive mode</p>
<p>data_transforms = {<br>
‘train’: transforms.Compose([<br>
transforms.RandomResizedCrop(224),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
]),<br>
‘val’: transforms.Compose([<br>
transforms.Resize(256),<br>
transforms.CenterCrop(224),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
]),<br>
}</p>
<p>data_dir = ‘C:\my data’<br>
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),<br>
data_transforms[x])<br>
for x in [‘train’, ‘val’]}<br>
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=6,<br>
shuffle=True, num_workers=6)<br>
for x in [‘train’, ‘val’]}<br>
dataset_sizes = {x: len(image_datasets[x]) for x in [‘train’, ‘val’]}<br>
class_names = image_datasets[‘train’].classes</p>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “CPU”)</p>
<p>train_data = datasets.CIFAR10(<br>
root = “C:\my data”,<br>
train = True,<br>
download = True,<br>
transform = transform)</p>
<p>train_data, val_data = torch.utils.data.random_split(<br>
train_data, [int(len(train_data) * 0.8), int(len(train_data)*0.2)])</p>
<p>test_data = datasets.CIFAR10(<br>
root = “C:\my data”,<br>
train = False,<br>
download = True,<br>
transform = transform)</p>
<p>classes = test_data.classes<br>
dic_classes = {}<br>
for i in range(len(classes)):<br>
dic_classes[i] = classes[i]</p>
<p>print(dic_classes)</p>
<p>trainloader = torch.utils.data.DataLoader(<br>
train_data, batch_size=6, shuffle=True)<br>
valloader = torch.utils.data.DataLoader(<br>
val_data, batch_size=6, shuffle=True)<br>
testloader = torch.utils.data.DataLoader(<br>
test_data, batch_size=6, shuffle=False)</p>
<p>def imshow(inp, title=None):<br>
“”“Imshow for Tensor.”""<br>
inp = inp.numpy().transpose((1, 2, 0))<br>
mean = np.array([0.485, 0.456, 0.406])<br>
std = np.array([0.229, 0.224, 0.225])<br>
inp = std * inp + mean<br>
inp = np.clip(inp, 0, 1)<br>
plt.imshow(inp)<br>
if title is not None:<br>
plt.title(title)<br>
plt.pause(0.001)  # pause a bit so that plots are updated</p>
<h1>
<a name="get-a-batch-of-training-data-1" class="anchor" href="#get-a-batch-of-training-data-1"></a>Get a batch of training data</h1>
<p>inputs, classes = next(iter(dataloaders[‘train’]))</p>
<h1>
<a name="make-a-grid-from-batch-2" class="anchor" href="#make-a-grid-from-batch-2"></a>Make a grid from batch</h1>
<p>out = torchvision.utils.make_grid(inputs)</p>
<p>imshow(out, title=[class_names[x] for x in classes])</p>
<p>def train_model(model, criterion, optimizer, scheduler, num_epochs=25):<br>
since = time.time()</p>
<pre><code>best_model_wts = copy.deepcopy(model.state_dict())
best_acc = 0.0
for epoch in range(25):  
    model.train(True)
    running_loss = 0.0
    running_acc = 0
    for i, data in enumerate(trainloader):
        inputs, labels = data
        inputs, labels = inputs.cuda(),labels.cuda()

        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.item() * inputs.size(0)
        out = torch.argmax(outputs.detach(),dim=1)
        assert out.shape==labels.shape
        running_acc += (labels==out).sum().item()
    print(f"Train loss {epoch+1}: {running_loss/len(trainset)},Train Acc:{running_acc*100/len(trainset)}%")

correct = 0
model.train(False)
with torch.no_grad():
    for inputs,labels in valloader:
        out = model(inputs.cuda()).cpu()
        out = torch.argmax(out,dim=1)
        acc = (out==labels).sum().item()
        correct += acc
    print(f"Val accuracy:{correct*100/len(valset)}%")
if correct&gt;best_val_acc:
    best_val_acc = correct
    best_val_model = deepcopy(model.state_dict())
lr_scheduler.step()
</code></pre>
<p>def visualize_model(model, num_images=6):<br>
was_training = model.training<br>
model.eval()<br>
images_so_far = 0<br>
fig = plt.figure()</p>
<pre><code>with torch.no_grad():
    for i, (inputs, labels) in enumerate(dataloaders['val']):
        inputs = inputs.to(device)
        labels = labels.to(device)

        outputs = model(inputs)
        _, preds = torch.max(outputs, 1)

        for j in range(inputs.size()[0]):
            images_so_far += 1
            ax = plt.subplot(num_images//2, 2, images_so_far)
            ax.axis('off')
            ax.set_title(f'predicted: {class_names[preds[j]]}')
            imshow(inputs.cpu().data[j])

            if images_so_far == num_images:
                model.train(mode=was_training)
                return
    model.train(mode=was_training)
</code></pre>
<p>model_ft = models.resnet18(pretrained=True)<br>
num_ftrs = model_ft.fc.in_features</p>
<h1>
<a name="here-the-size-of-each-output-sample-is-set-to-2-3" class="anchor" href="#here-the-size-of-each-output-sample-is-set-to-2-3"></a>Here the size of each output sample is set to 2.</h1>
<h1>
<a name="alternatively-it-can-be-generalized-to-nnlinearnum_ftrs-lenclass_names-4" class="anchor" href="#alternatively-it-can-be-generalized-to-nnlinearnum_ftrs-lenclass_names-4"></a>Alternatively, it can be generalized to nn.Linear(num_ftrs, len(class_names)).</h1>
<p>model_ft.fc = nn.Linear(num_ftrs, 2)</p>
<p>model_ft = model_ft.to(device)</p>
<p>criterion = nn.CrossEntropyLoss()</p>
<h1>
<a name="observe-that-all-parameters-are-being-optimized-5" class="anchor" href="#observe-that-all-parameters-are-being-optimized-5"></a>Observe that all parameters are being optimized</h1>
<p>optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)</p>
<h1>
<a name="decay-lr-by-a-factor-of-01-every-7-epochs-6" class="anchor" href="#decay-lr-by-a-factor-of-01-every-7-epochs-6"></a>Decay LR by a factor of 0.1 every 7 epochs</h1>
<p>exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)<br>
model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler,<br>
num_epochs=25)<br>
The error is this:</p>
<hr>
<p>NameError                                 Traceback (most recent call last)<br>
~\AppData\Local\Temp/ipykernel_17208/2182967272.py in <br>
----&gt; 1 model_ft = train_model(model_ft, criterion, optimizer_ft, exp_lr_scheduler,<br>
2                        num_epochs=25)</p>
<p>~\AppData\Local\Temp/ipykernel_17208/3799990550.py in train_model(model, criterion, optimizer, scheduler, num_epochs)<br>
8         running_loss = 0.0<br>
9         running_acc = 0<br>
—&gt; 10         for i, data in enumerate(trainloader):<br>
11             inputs, labels = data<br>
12             inputs, labels = inputs.cuda(),labels.cuda()</p>
<p>NameError: name ‘trainloader’ is not defined</p>
<p>can you check this code and tell me what to change I am doing it in a number of ways Iam getting errors only if you rewrite it also fine please help me with this</p>
<p>thank you</p>