Ravindra BagaleCourses & study guides English Track your progress

48. Small CNN Image Project in PyTorch

Data Science

48.3 Train a Small CNN

48.3 Small CNN train करा

TinyCNN idea reuse — CPU वर काही epochs.

हे का शिकायचं?

Why. TinyCNN idea reuse करा आणि CPU वर काही epochs train करा.

हे काय आहे?

What. Synthetic set वर CrossEntropyLoss + Adam.

कसं करायचं?

How.

थोडं train करा

  1. TinyCNN define करा (Chapter 47).
  2. Train batch 128 बनावा.
  3. 15–30 epochs; दर काही steps loss/acc print.
import torch
import torch.nn as nn
import torch.optim as optim

class TinyCNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 8, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
            nn.Conv2d(8, 16, 3, padding=1), nn.ReLU(), nn.MaxPool2d(2),
        )
        self.head = nn.Linear(16 * 7 * 7, 2)
    def forward(self, x):
        x = self.features(x)
        return self.head(x.view(x.size(0), -1))

def make_batch(n=128):
    x = torch.rand(n, 1, 28, 28) * 0.2
    y = torch.randint(0, 2, (n,))
    for i in range(n):
        if y[i] == 0: x[i, 0, :, :14] += 0.8
        else: x[i, 0, :, 14:] += 0.8
    return x, y

torch.manual_seed(0)
model = TinyCNN()
opt = optim.Adam(model.parameters(), lr=0.01)
crit = nn.CrossEntropyLoss()
X, y = make_batch()
for epoch in range(20):
    opt.zero_grad()
    logits = model(X)
    loss = crit(logits, y)
    loss.backward()
    opt.step()
    if epoch % 5 == 0:
        acc = (logits.argmax(1) == y).float().mean()
        print(epoch, float(loss), float(acc))

Output / जे दिसायला हवं. या easy synthetic pattern वर accuracy वर जाण्याकडे.