Views
No views yet
| Component | Details |
|---|---|
| Feature extraction | TF-IDF (15000 features, unigrams + bigrams, sublinear_tf) |
| Input layer | 15000-dim TF-IDF vector |
| Hidden layer 1 | Linear(15000→512) + BatchNorm + LeakyReLU + Dropout(0.4) |
| Hidden layer 2 | Linear(512→256) + BatchNorm + LeakyReLU + Dropout(0.3) |
| Hidden layer 3 | Linear(256→64) + BatchNorm + LeakyReLU + Dropout(0.2) |
| Output layer | Linear(64→1) + Sigmoid |
| Task | Binary sentiment classification |
1import torch
2import torch.nn as nn
3import pickle, json, re
4from huggingface_hub import hf_hub_download
5
6class SentimentNN(nn.Module):
7 def __init__(self, input_dim):
8 super().__init__()
9 self.net = nn.Sequential(
10 nn.Linear(input_dim, 512), nn.BatchNorm1d(512), nn.LeakyReLU(0.1), nn.Dropout(0.4),
11 nn.Linear(512, 256), nn.BatchNorm1d(256), nn.LeakyReLU(0.1), nn.Dropout(0.3),
12 nn.Linear(256, 64), nn.BatchNorm1d(64), nn.LeakyReLU(0.1), nn.Dropout(0.2),
13 nn.Linear(64, 1), nn.Sigmoid(),
14 )
15 def forward(self, x):
16 return self.net(x)
17
18# Download artifacts
19repo = "enzoliao/imdb-sentiment-nn"
20vectorizer_path = hf_hub_download(repo_id=repo, filename="vectorizer.pkl")
21model_path = hf_hub_download(repo_id=repo, filename="model.pt")
22config_path = hf_hub_download(repo_id=repo, filename="config.json")
23
24with open(config_path) as f:
25 config = json.load(f)
26with open(vectorizer_path, "rb") as f:
27 vectorizer = pickle.load(f)
28
29model = SentimentNN(config["input_dim"])
30model.load_state_dict(torch.load(model_path, map_location="cpu"))
31model.eval()
32
33# Predict
34import numpy as np
35text = "This movie was absolutely fantastic!"
36text = re.sub(r"<[^>]+>", " ", text).strip().lower()
37vec = vectorizer.transform([text]).toarray().astype("float32")
38with torch.no_grad():
39 prob = model(torch.tensor(vec)).item()
40label = "POSITIVE" if prob >= 0.5 else "NEGATIVE"
41print(f"{label} ({prob:.4f})")imdb-sentiment-nn/
├── data/imdb_top_500.csv
├── train.py
├── predict.py
├── requirements.txt
├── README.md
└── .github/workflows/train-and-upload.yml