Ravindra BagaleCourses & study guides English Track your progress

50. Tiny Text Classifier with a Transformer Intro in PyTorch

Data Science

50.4 Train on Fictional Reviews

50.4 Fictional reviews वर train

Tiny classifier train आणि predict.

सुरू करण्याआधी

या page वर 50.2 मधले X आणि y tensors आणि 50.3 Mean-Embed Classifier मधला MeanEmbedClassifier class वापरला आहे. आधी 50.2 आणि 50.3 चा code त्याच notebook मध्ये run करा, नाहीतर Python "name is not defined" असा error देईल.

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

Why. Tiny classifier train करा आणि predict करा.

हे काय आहे?

What. 6-row toy वर BCEWithLogitsLoss + Adam, काही dozen epochs — API practice, Kaggle win नाही.

कसं करायचं?

How.

Fit आणि infer

  1. 50.2 मधून X, y build.
  2. 60 epochs train करा; दर 15 epochs नंतर loss print करा.
  3. Sigmoid probs; threshold 0.5.
import torch.optim as optim

torch.manual_seed(0)
model = MeanEmbedClassifier()
opt = optim.Adam(model.parameters(), lr=0.05)
crit = nn.BCEWithLogitsLoss()

for epoch in range(60):
    opt.zero_grad()
    logits = model(X)
    loss = crit(logits, y)
    loss.backward()
    opt.step()
    if epoch % 15 == 0:
        print(epoch, float(loss))

model.eval()
with torch.no_grad():
    probs = torch.sigmoid(model(X))
print(probs.round(decimals=3).view(-1))

Output / जे दिसायला हवं. Loss खाली; probs toy labels कडे. 6 rows = learning sandbox — Chapter 38 अजून लागू.