BERT-based Emotion Recognition in Dialog Systems

Model Overview

BERT (Bidirectional Encoder Representations from Transformers) is a language model developed by Google that uses Transformer encoder architecture with bidirectional context processing. Unlike traditional recurrent networks, BERT processes input sequences in both directions simultaneously, enabling comprehensive contextual understanding.

The model's key innovations lie in its pretraining methodology, which employs:

  • Masked Language Modeling: Randomly masks 15% of tokens (80% replaced with [MASK], 10% substituted with random tokens, 10% unchanged)
  • Next Sentence Prediction: Determines whether two sentences are consecutive, enabilng better understanding of sentence relationships

After pretraining, BERT preserves embedding tables and transformer weights (12 layers for base, 24 for large variants). These pretrained models can be fine-tuned for various downstream tasks including text classification, similarity assessment, and reading comprehension.

Emotion Detection Task

Emotion detection in dialog systems classifies user utterances in to positive, negative, or neutral categories. This capability enhances conversational quality in chatbots and customer service applications while reducing manual review costs.

Implementation Example

import mindspore
from mindspore.dataset import GeneratorDataset
from mindspore import nn, ops

class EmotionDataset:
    def __init__(self, data_path):
        self.labels = []
        self.texts = []
        self._load_data(data_path)
    
    def _load_data(self, path):
        with open(path, 'r', encoding='utf-8') as file:
            lines = file.readlines()[1:]  # Skip header
            for line in lines:
                if line.strip():
                    label, text = line.strip().split('\t')
                    self.labels.append(int(label))
                    self.texts.append(text)
    
    def __len__(self):
        return len(self.labels)
    
    def __getitem__(self, idx):
        return self.labels[idx], self.texts[idx]

# Download dataset
!wget https://example.com/emotion_dataset.tar.gz
!tar -xzf emotion_dataset.tar.gz

Data Processing

from mindnlp.transformers import BertTokenizer

def prepare_dataset(data_source, tokenizer, seq_length=64, batch_size=32):
    dataset = GeneratorDataset(data_source, ['label', 'text'], shuffle=True)
    
    def tokenize_function(text):
        encoded = tokenizer(text, max_length=seq_length, 
                          padding='max_length', truncation=True)
        return encoded['input_ids'], encoded['attention_mask']
    
    dataset = dataset.map(tokenize_function, input_columns='text',
                         output_columns=['input_ids', 'attention_mask'])
    dataset = dataset.batch(batch_size)
    return dataset

tokenizer = BertTokenizer.from_pretrained('bert-base-chinese')
processed_data = prepare_dataset(EmotionDataset('emotion_data.txt'), tokenizer)

Model Configuration

from mindnlp.transformers import BertForSequenceClassification

model = BertForSequenceClassification.from_pretrained(
    'bert-base-chinese', 
    num_labels=3  # Positive, negative, neutral
)

optimizer = nn.Adam(model.trainable_params(), learning_rate=2e-5)
loss_fn = nn.CrossEntropyLoss()

Training Setup

from mindnlp.engine import Trainer, AccuracyMetric

trainer = Trainer(
    model=model,
    loss_fn=loss_fn,
    optimizer=optimizer,
    metrics={'accuracy': AccuracyMetric()},
    train_dataset=processed_data,
    eval_dataset=validation_data
)

trainer.run(epochs=5)

Evaluation

results = trainer.evaluate(test_dataset)
print(f"Test accuracy: {results['accuracy']:.3f}")

Inference Example

def predict_emotion(text, model, tokenizer):
    inputs = tokenizer(text, return_tensors='ms', 
                      max_length=64, padding=True)
    outputs = model(**inputs)
    prediction = ops.argmax(outputs.logits, axis=-1)
    return prediction.item()

sample_text = "我很高兴和你聊天"
prediction = predict_emotion(sample_text, model, tokenizer)
print(f"Predicted emotion: {prediction}")

Tags: BERT Transformer Emotion Detection Text Classification mindspore

Posted on Thu, 03 Sep 2026 16:11:08 +0000 by mattheww