๐ŸŽ“ A1Learner
๐Ÿ”

PyTorch The Review Words Dataset

A synthetic dataset is data a program makes up under fixed rules. This dataset builds tiny movie reviews from three small word lists.

Three word lists

A review draws its feeling words from one side only. Filler words carry no feeling at all.

POSITIVE = ["great", "lovely", "bright", "fun", "warm"]
NEGATIVE = ["awful", "dull", "cold", "boring", "grim"]
FILLER = ["the", "a", "movie", "story", "scene", "really", "very"]
WORDS = ["<pad>"] + POSITIVE + NEGATIVE + FILLER

Words become numbers

Each word gets one fixed number from the vocabulary. Models read numbers, not letters.

ids = [WORD_ID[w] for w in ["great", "the", "movie"]]
print(ids)

One review from the generator

The generator picks a side first. It then picks words from that side and shuffles them in with filler.

gen = torch.Generator().manual_seed(7)
words, label = make_review(gen)
print(words, label)

Example

import torch

POSITIVE = ["great", "lovely", "bright", "fun", "warm"]
NEGATIVE = ["awful", "dull", "cold", "boring", "grim"]
FILLER = ["the", "a", "movie", "story", "scene", "really", "very"]
WORDS = ["<pad>"] + POSITIVE + NEGATIVE + FILLER


def make_review(gen):
    label = int(torch.randint(0, 2, (1,), generator=gen))
    side = POSITIVE if label == 1 else NEGATIVE
    n_sent = int(torch.randint(2, 4, (1,), generator=gen))
    n_fill = int(torch.randint(1, 4, (1,), generator=gen))
    picks = torch.randperm(len(side), generator=gen)[:n_sent]
    words = [side[i] for i in picks.tolist()]
    picks = torch.randint(0, len(FILLER), (n_fill,), generator=gen)
    words += [FILLER[i] for i in picks.tolist()]
    order = torch.randperm(len(words), generator=gen)
    return [words[i] for i in order.tolist()], label


gen = torch.Generator().manual_seed(7)
reviews = [make_review(gen) for _ in range(200)]
train = reviews[:160]
held_out = reviews[160:]

names = {1: "positive", 0: "negative"}
print("Vocabulary size:", len(WORDS))
print("Train reviews:", len(train))
print("Held-out reviews:", len(held_out))
print("First held-out reviews:")
for words, label in held_out[:5]:
    print(" ".join(words), "->", names[label])

Key points

  • Positive words: great, lovely, bright, fun, warm
  • Negative words: awful, dull, cold, boring, grim
  • Filler words: the, a, movie, story, scene, really, very
  • Label 1 means positive and label 0 means negative
  • A review mixes two or three sentiment words with one to three filler words
  • A seeded generator makes the same reviews on every run
  • Held-out reviews: the last 40 reviews, kept aside and never trained on
  • The dataset is synthetic, so its rules are known exactly

In this example, the three word lists hold five positive words, five negative words, and seven filler words. The WORDS list adds a pad entry in front, so the vocabulary has 18 entries. The make_review function rolls a seeded generator for the label first. It then picks two or three words from the matching side. It adds one to three filler words and shuffles the whole list. The main code builds 200 reviews with seed 7. The first 160 form the training set. The last 40 stay held out for honest checks later. The printout shows the first five held-out reviews with their labels. Every review reads as one sentiment side plus neutral filler. The next lesson trains a classifier on this data.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Python Playground

Exercise

Why does this dataset use a seeded generator?