WRIME Multi-Emotion Intensity Model (weight-space edited, variant B)
日本語感情強度予測モデル。8種類の感情強度を同時に回帰で予測する。
MuneK/wrime-emotion-modernbert-model に対して以下の2点を適用したもの:
- tokenizer 不具合の修正(これが単体で最も影響が大きい)
- ヘッド重みの 16 パラメータ編集によるクラス間 logit スケールの校正
エンコーダ(ModernBERT-ja 310M, 310M パラメータ)は一切変更していない。
予測する8種類の感情
joy(喜び), sadness(悲しみ), anticipation(期待), surprise(驚き), anger(怒り), fear(恐れ), disgust(嫌悪), trust(信頼)
出力は 0-1 スケール(WRIME の 0-3 スケールを理論最大値 3.0 で正規化)。
性能
WRIME ver2 公式 test split(2,500件)、ラベルは Avg. Readers_*、
dominant emotion = np.argmax(labels)。
| 指標 | 元モデル | 本モデル (B) |
|---|---|---|
| Dominant Emotion Accuracy | 0.8044 | 0.8408 |
| weighted F1 | 0.8091 | 0.8455 |
| macro F1 | 0.7504 | 0.7814 |
| MCC (mean, one-vs-rest) | 0.7307 | 0.7643 |
| MSE | 0.00724 | 0.00768 |
| MAE | 0.0438 | 0.0450 |
回帰性能(MSE / MAE)はほぼ据え置きで、分類性能のみを改善している。
クラス別(test)
| emotion | n | precision | recall | F1 | 元モデルの F1 |
|---|---|---|---|---|---|
| joy | 895 | 0.9448 | 0.8413 | 0.8901 | 0.8395 |
| sadness | 440 | 0.9057 | 0.7636 | 0.8286 | 0.8000 |
| anticipation | 535 | 0.8715 | 0.8617 | 0.8665 | 0.8285 |
| surprise | 368 | 0.7788 | 0.8804 | 0.8265 | 0.8105 |
| anger | 20 | 1.0000 | 0.7000 | 0.8235 | 0.8000 |
| fear | 164 | 0.5747 | 0.9146 | 0.7059 | 0.6773 |
| disgust | 69 | 0.5686 | 0.8406 | 0.6784 | 0.6591 |
| trust | 9 | 0.6000 | 0.6667 | 0.6316 | 0.5882 |
8クラスすべてで F1 が改善。anger(n=20), trust(n=9)はサンプル数が少なく 数値の信頼区間が広い点に注意。
使用方法
import torch, torch.nn as nn
from huggingface_hub import hf_hub_download
from transformers import AutoModel, AutoTokenizer
REPO = "MuneK/wrime-emotion-modernbert-calibrated"
class MultiEmotionIntensityClassifier(nn.Module):
def __init__(self, model_name="sbintuitions/modernbert-ja-310m",
num_emotions=8, dropout=0.3):
super().__init__()
self.bert = AutoModel.from_pretrained(model_name)
self.dropout = nn.Dropout(dropout)
self.emotion_regressors = nn.Linear(self.bert.config.hidden_size, num_emotions)
self.sigmoid = nn.Sigmoid()
def forward(self, input_ids, attention_mask):
out = self.bert(input_ids=input_ids, attention_mask=attention_mask)
pooled = out.last_hidden_state[:, 0]
return self.sigmoid(self.emotion_regressors(self.dropout(pooled)))
model = MultiEmotionIntensityClassifier()
weights = hf_hub_download(REPO, "pytorch_model.bin")
model.load_state_dict(torch.load(weights, weights_only=True)) # strict=True
model.eval()
tokenizer = AutoTokenizer.from_pretrained(REPO)
EMOTIONS = ["joy", "sadness", "anticipation", "surprise",
"anger", "fear", "disgust", "trust"]
def analyze(text):
enc = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
with torch.no_grad():
scores = model(enc["input_ids"], enc["attention_mask"])[0]
return dict(zip(EMOTIONS, scores.tolist()))
print(analyze("今日は友達と遊んで楽しかったです"))
# {'joy': 0.771, 'sadness': 0.014, ..., 'trust': 0.064}
AutoTokenizer.from_pretrained はリポジトリ ID をそのまま受け取るが、
torch.load はローカルファイルを要求するため、重みは hf_hub_download で
取得する必要がある(torch.load("<repo_id>/pytorch_model.bin") は
FileNotFoundError になる)。
ローカルにクローン済みの場合は REPO をそのディレクトリのパスに置き換え、
hf_hub_download(REPO, "pytorch_model.bin") を
f"{REPO}/pytorch_model.bin" にすればよい。
動作確認: 上記の手順で Hub から読み込み、WRIME ver2 の test split 2,500件で accuracy 0.8408 / macro F1 0.7814 / MSE 0.00768 を再現済み。
変更点の詳細
1. tokenizer 不具合の修正
元リポジトリでは AutoTokenizer.from_pretrained(...) が LlamaTokenizer(slow)として
読み込まれ、日本語が1文字ずつに分割されていた。
"今日は友達と遊んで楽しかったです"
修正前 : ['今','日','は','友','達','と','遊','ん','で','楽','し','か','っ','た','で','す'] 16トークン
修正後 : ['今日は','友達と','遊んで','楽しかったです'] 4トークン
この状態での test accuracy は 0.5612(macro F1 0.4335)だった。
真因: tokenizer 関連の4ファイル(tokenizer.json, tokenizer.model,
special_tokens_map.json, tokenizer_config.json)はベースモデルとバイト単位で同一で
壊れていない。問題は config.json の model_type が "multi_emotion_intensity" という
transformers に未知の値であるため、AutoTokenizer が model_type からの解決に失敗し、
tokenizer_config.json の "tokenizer_class": "LlamaTokenizer" にフォールバックすること。
ベースモデル sbintuitions/modernbert-ja-310m は model_type: "modernbert" なので
この経路に入らず、同じファイルで正しく動く。
修正: tokenizer_config.json の tokenizer_class を
"PreTrainedTokenizerFast" に変更(TokenizersBackend として読まれ tokenizer.json を使う)。
config.json の model_type を "modernbert" にしても同じ効果が得られる。
2. ヘッド重みの編集
emotion_regressors(768→8 の線形層)のみを編集:
W[k] <- a_k * W[k]
b[k] <- a_k * b[k] + c_k
| emotion | a_k | c_k |
|---|---|---|
| joy | 1.00 | +0.340 |
| sadness | 1.00 | +0.115 |
| anticipation | 1.06 | −0.285 |
| surprise | 1.00 | −0.010 |
| anger | 1.00 | −0.110 |
| fear | 1.00 | −0.135 |
| disgust | 1.00 | −0.085 |
| trust | 0.96 | −0.185 |
- 編集した自由度: 16 / 314,618,120 パラメータ
- 勾配降下は使用せず、dev split(2,500件)上の座標上昇法のみ
- test split は最終測定にのみ使用
- MSE が元モデルの 1.10 倍を超えない制約下で探索
動機: 元モデルは感情ごとの logit スケールが不均一だった (joy: 平均 −2.50 / sd 2.42 に対し anger: 平均 −5.05 / sd 0.91)。 argmax は sigmoid の単調性より logit の argmax と等価なので、 平均が低くスプレッドの小さい anger / trust は構造的に argmax を取りにくく、 逆に fear / disgust が過剰発火していた(元モデルの fear: precision 0.54 / recall 0.91)。 この編集はそのスケール不整合だけを補正する。
注意: a_k > 0 なので各感情内では単調変換であり、
同一感情内の順位は元モデルと完全に同一(test の Spearman 相関は小数6桁まで一致)。
変わるのは感情間の比較(argmax)と絶対閾値の位置のみ。
他の変種について
MSE 許容量を変えた変種も作成している。より積極的な設定では accuracy 0.8736 まで上がるが、fear / disgust の絶対スコアを強く押し下げるため 固定閾値での個別感情検出が悪化する(閾値 0.5 での fear F1 が 0.355 → 0.147)。 本モデル(B)は分類性能と絶対スコアの両立を優先した設定。
よくある間違い: モデルクラスの取り違え
このチェックポイントのヘッドは 8 出力で、キー名は emotion_regressors です。
emotion_regressors.weight (8, 768)
emotion_regressors.bias (8,)
1 出力・キー名 classifier のクラス(例: nn.Linear(hidden_size, 1) を
self.classifier に持つもの)では読み込めません。
strict=True -> 失敗
Missing key(s): "classifier.weight", "classifier.bias"
Unexpected key(s): "emotion_regressors.weight", "emotion_regressors.bias"
strict=False -> classifier.weight/bias がランダム初期化のまま残り、
出力はランダムな射影になる(例外は出ない)
load_state_dict は必ず strict=True(既定値)で呼ぶこと。
strict=False を使うとヘッドの取り違えが無言で通ってしまう。
なお BertForSequenceClassification 形式のモデル(同作者の BERT-large 系
wrime モデルなど)はキー名が classifier で、さらに bert.pooler(tanh)を
経由するため forward も異なる。本モデルとは互換性がない。
制限事項
- 日本語テキストのみ対応
trust(test n=9),anger(test n=20)はサンプル数が少なく評価の信頼区間が広い- 固定閾値で個別感情を検出する用途では、閾値を再調整することを推奨
- 感情ラベルは WRIME の読み手3名の平均であり、書き手本人の主観とは異なる
ライセンス
- ModernBERT-ja: MIT License
- WRIME: CC BY-NC-ND 4.0(非営利・改変禁止)
引用
@misc{wrime-emotion-model,
title={WRIME Multi-Emotion Intensity Model},
author={Muneaki Kanno},
year={2025},
url={https://huggingface.co/MuneK/wrime-emotion-modernbert-model}
}
@inproceedings{kajiwara2021wrime,
title={WRIME: A New Dataset for Emotional Intensity Estimation with Subjective and Objective Annotations},
author={Tomoyuki Kajiwara and Chenhui Chu and Noriko Takemura and Yuta Nakashima and Hajime Nagahara},
booktitle={Proceedings of the 2021 Conference of the North American Chapter of the Association for Computational Linguistics (NAACL)},
pages={2095--2104},
year={2021}
}
@misc{modernbert-ja,
author={Tsukagoshi, Hayato and Li, Shengzhe and Fukuchi, Akihiko and Shibata, Tomohide},
title={{ModernBERT-Ja}},
url={https://huggingface.co/collections/sbintuitions/modernbert-ja-67b68fe891132877cf67aa0a},
year={2025}
}
重み編集の手法は Weight-Space Programming を参考にした。