Klasifikasi emosi multi-label untuk ulasan berbahasa Indonesia.
Satu teks bisa punya beberapa emosi sekaligus — misalnya jengkel + kecewa.
Label (7): netral · jengkel · kecewa · frustrasi · marah · puas · harapan
#DILATIH MENGGUNAKAN 4600 DATA E COMMERS
Model ini bukan model dari nol. Ini hasil fine-tuning dari:
indobenchmark/indobert-base-p2IndoBERT Base Model (phase 2, uncased)
| Repo HuggingFace | https://huggingface.co/indobenchmark/indobert-base-p2 |
| Paper | IndoNLU — arXiv:2009.05387 |
| Arsitektur | BERT-base: 12 layer, 768 hidden, 12 head, 124.5M parameter |
| Data pretraining | Indo4B (23.43 GB teks berbahasa Indonesia) |
| Lisensi model dasar | MIT |
| Tokenizer | WordPiece, vocab 30.521 — identik dengan IndoBERT asli |
Seluruh kemampuan berbahasa Indonesia di model ini berasal dari IndoBERT. Kontribusi repo ini hanya bagian fine-tuning untuk tugas klasifikasi emosi (tambahan MLP head + data latih ulasan e-commerce).
Model ini tidak berafiliasi dengan IndoBenchmark / IndoNLU. Nama "IndoBERT" dan hasil pretraining-nya adalah milik para penulis aslinya.
Bryan Wilie*, Karissa Vincentio*, Genta Indra Winata*, Samuel Cahyawijaya*, Xiaohong Li, Zhi Yuan Lim, Sidik Soleman, Rahmad Mahendra, Pascale Fung, Syafri Bahar, Ayu Purwarianti
Kalau kamu memakai model ini, mohon sitasi IndoBERT juga, bukan hanya repo ini:
@inproceedings{wilie2020indonlu,
title = {IndoNLU: Benchmark and Resources for Evaluating Indonesian Natural Language Understanding},
author = {Bryan Wilie and Karissa Vincentio and Genta Indra Winata and Samuel Cahyawijaya and X. Li and Zhi Yuan Lim and S. Soleman and R. Mahendra and Pascale Fung and Syafri Bahar and A. Purwarianti},
booktitle = {Proceedings of the 1st Conference of the Asia-Pacific Chapter of the Association for Computational Linguistics and the 10th International Joint Conference on Natural Language Processing},
year = {2020}
}
@article{wilie2020indobert,
title = {IndoBERT: a Pretrained Language Model for Indonesian},
author = {Wilie, Bryan and Vincentio, Karissa and Winata, Genta Indra and Cahyawijaya, Samuel and Li, Xiaohong and Lim, Zhi Yuan and Soleman, Sidik and Mahendra, Rahmad and Fung, Pascale and Bahar, Syafri and Purwarianti, Ayu},
journal = {arXiv preprint arXiv:2009.05387},
year = {2020}
}
| Parameter | Nilai |
|---|---|
| Base model | indobenchmark/indobert-base-p2 |
| Pooling | Mean pooling atas token non-pad |
| Head | Linear(768→512) → GELU → 512→256 → GELU → 256→128 → GELU → 128→64 → GELU → 64→7 |
| Loss saat training | BCEWithLogitsLoss + pos_weight |
| Output | 7 raw logits → sigmoid → threshold |
MAX_LENGTH |
128 token |
| Prefix | '0=tidak ada. 1=ada. ulasan: ' |
| File bobot | run10mini_weights.pt (~500 MB) |
Model dilatih dengan prefix injection. Setiap teks saat training berbentuk:
'0=tidak ada. 1=ada. ulasan: ' + teks_asli
Prefix ini bagian dari kontrak model. Tanpa prefix, hasil prediksi melenceng. Selalu tambahkan prefix sebelum tokenisasi.
pip install torch transformers pandas openpyxl
Butuh transformers >= 4.40.
python predict.py "barang rusak, seller tidak respon sama sekali"
"barang rusak, seller tidak respon sama sekali"
→ jengkel, kecewa
netral 0.0635 (th=0.50) #
jengkel 0.8276 (th=0.50) ######################## ADA
kecewa 0.7415 (th=0.45) ###################### ADA
frustrasi 0.3050 (th=0.40) #########
marah 0.2173 (th=0.50) ######
puas 0.1032 (th=0.50) ###
harapan 0.0772 (th=0.45) ##
python predict.py --file data.xlsx
python predict.py --file data.csv --out hasil.xlsx --prob
teks (bisa diganti: --col nama_kolom)<nama_file>_LABELED.xlsx berisi 7 kolom emosi (0/1)--prob menambahkan kolom prob_<emosi>from predict import load_model, predict_batch, EMOTIONS
model, tokenizer, device = load_model()
texts = ["barang bagus, terima kasih", "penipu, barang tidak sesuai"]
labels, probs = predict_batch(model, tokenizer, texts, device)
for t, lbl, pr in zip(texts, labels, probs):
aktif = [EMOTIONS[i] for i, v in enumerate(lbl) if v == 1]
print(t, "->", aktif)
predict.py)Karena head-nya custom, model ini tidak bisa dipakai lewat
pipeline("text-classification") biasa. Definisi ulang kelasnya:
import torch, torch.nn as nn
from transformers import AutoModel, AutoTokenizer
PREFIX = "0=tidak ada. 1=ada. ulasan: "
MAX_LENGTH = 128
EMOTIONS = ['netral', 'jengkel', 'kecewa', 'frustrasi', 'marah', 'puas', 'harapan']
class EmotionClassifier(nn.Module):
def __init__(self, base_model="indobenchmark/indobert-base-p2", num_labels=7):
super().__init__()
self.bert = AutoModel.from_pretrained(base_model)
self.classifier = nn.Sequential(
nn.Linear(768, 512), nn.GELU(),
nn.Linear(512, 256), nn.GELU(),
nn.Linear(256, 128), nn.GELU(),
nn.Linear(128, 64), nn.GELU(),
nn.Linear(64, num_labels),
)
def forward(self, input_ids, attention_mask):
out = self.bert(input_ids=input_ids, attention_mask=attention_mask)
mask = attention_mask.unsqueeze(-1).expand(out.last_hidden_state.size()).float()
pooled = (out.last_hidden_state * mask).sum(1) / mask.sum(1).clamp(min=1e-9)
return self.classifier(pooled)
model = EmotionClassifier()
model.load_state_dict(torch.load("run10mini_weights.pt", map_location="cpu"))
model.eval()
tokenizer = AutoTokenizer.from_pretrained("indobenchmark/indobert-base-p2")
enc = tokenizer(PREFIX + "barang rusak, seller tidak respon",
max_length=MAX_LENGTH, padding="max_length",
truncation=True, return_tensors="pt")
with torch.no_grad():
probs = torch.sigmoid(model(enc["input_ids"], enc["attention_mask"]))[0]
for em, p in zip(EMOTIONS, probs):
print(f"{em:<10} {p.item():.4f}")
| Emosi | Threshold |
|---|---|
| netral | 0.50 |
| jengkel | 0.50 |
| kecewa | 0.45 |
| frustrasi | 0.40 |
| marah | 0.50 |
| puas | 0.50 |
| harapan | 0.45 |
Mau threshold seragam 0.5?
python predict.py --threshold 0.5 "teks ulasan"
Model ini cenderung konservatif — lebih memilih diam daripada salah klaim.
Untung untuk precision, tapi recall netral, marah, dan harapan relatif
rendah karena probabilitas pada sampel yang terlewat memang sangat kecil
(<0.15). Kasus seperti itu tidak bisa diperbaiki lewat threshold saja.
"semoga awet", "mudah-mudahan cepat sampai")
kadang terbaca netral. harapan adalah label terlemah.jengkel dan kecewa tumpang tindih secara definisi, jadi sering muncul
bersamaan.| File | Keterangan |
|---|---|
run10mini_weights.pt |
Bobot model, state_dict PyTorch (~500 MB) |
tokenizer.json |
Tokenizer WordPiece IndoBERT, vocab 30.521 |
tokenizer_config.json |
Konfigurasi tokenizer |
config.json |
Config arsitektur backbone BERT |
predict.py |
Script inference, jalan offline |
README.md |
Dokumen ini |
LICENSE |
Lisensi MIT |
Catatan:
config.jsonmemakaivocab_size: 50000(ukuran embedding matrix IndoBERT asli), sedangkanvocab.txtberisi 30.521 token. Ini normal — baris embedding di atas 30.520 tidak pernah dipakai tokenizer. Jangan diubah.
MIT — mengikuti lisensi model dasar
indobenchmark/indobert-base-p2
yang juga MIT.
Kalau kamu memakai atau memodifikasi model ini, mohon cantumkan kredit ke IndoBERT / IndoNLU seperti di bagian Model dasar di atas.