File size: 4,556 Bytes
3c83e5a
 
6e33e62
 
 
 
 
 
 
 
 
 
 
 
3c83e5a
 
6e33e62
3c83e5a
6e33e62
3c83e5a
6e33e62
 
 
 
3c83e5a
00f634f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e33e62
3c83e5a
6e33e62
 
 
 
 
 
 
 
3c83e5a
6e33e62
3c83e5a
6e33e62
3c83e5a
6e33e62
3c83e5a
6e33e62
 
 
 
3c83e5a
6e33e62
3c83e5a
6e33e62
 
 
 
 
 
 
 
 
 
3c83e5a
6e33e62
3c83e5a
6e33e62
 
 
 
 
 
 
 
 
 
 
3c83e5a
 
6e33e62
3c83e5a
6e33e62
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
---
library_name: transformers
tags:
- generated_from_trainer
metrics:
- f1_score
model-index:
- name: results
  results: []
license: apache-2.0
language:
- th
base_model:
- distilbert/distilbert-base-uncased
---

# Model: Fine-Tuned Transformer

This model is a fine-tuned version of the Transformer architecture using a custom-trained BPE tokenizer and a DistilBERT-like configuration. It has been fine-tuned on a specific dataset with a sequence length of 512 tokens for a classification task involving 3 labels.

### Key Evaluation Metrics:
- **Loss**: 0.3656
- **F1 Micro**: 0.8763
- **Validation Set Size**: 7608 samples

## Usage
```python
from transformers import DistilBertForSequenceClassification, PreTrainedTokenizerFast
import torch

# Load the tokenizer and model
tokenizers = PreTrainedTokenizerFast.from_pretrained("FlukeTJ/distilbert-base-thai-sentiment")
models = DistilBertForSequenceClassification.from_pretrained("FlukeTJ/distilbert-base-thai-sentiment")

# Set device (GPU if available, else CPU)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
models = models.to(device)

def predict_sentiment(text):
    # Tokenize the input text without token_type_ids
    inputs = tokenizers(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
    inputs.pop("token_type_ids", None)  # Remove token_type_ids if present
    
    inputs = {k: v.to(device) for k, v in inputs.items()}
    
    # Make prediction
    with torch.no_grad():
        outputs = models(**inputs)
    
    # Get probabilities
    probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
    
    # Get the predicted class 
    predicted_class = torch.argmax(probabilities, dim=1).item()
    
    # Map class to sentiment
    sentiment_map = {1: "Neutral", 0: "Positive", 2: "Negative"}
    predicted_sentiment = sentiment_map[predicted_class]
    
    # Get the confidence score
    confidence = probabilities[0][predicted_class].item()
    
    return predicted_sentiment, confidence

# Example usage
texts = [
    "สุดยอดดด"
]

for text in texts:
    sentiment, confidence = predict_sentiment(text)
    print(f"Text: {text}")
    print(f"Predicted Sentiment: {sentiment}")
    print(f"Confidence: {confidence:.2f}")

# =============================
# Result    
# Text: สุดยอดดด
# Predicted Sentiment: Positive
# Confidence: 0.96
# =============================
```

## Model Description

This model is based on a **DistilBERT** architecture with the following configuration:
- **Sequence Length**: 512 tokens
- **Number of Layers**: 6 transformer layers
- **Number of Attention Heads**: 8
- **Vocabulary Size**: 20,000 (custom Byte Pair Encoding tokenizer)
- **Max Position Embeddings**: 512
- **Pad Token ID**: Defined by the custom tokenizer
- **Number of Labels**: 3 (for multi-class classification)

The tokenizer used for this model is a custom Byte Pair Encoding (BPE) tokenizer trained on the combined training and test datasets.

## Tokenizer

A custom tokenizer was built using **Byte Pair Encoding (BPE)** with a vocabulary size of 20,000. The tokenizer was trained on both the training and test sets to capture a wide range of token patterns.

## Training and Evaluation Data
- Training Set Size: 43,112 samples
- Validation Set Size: 7,608 samples
The model was trained and evaluated on a dataset that has not been publicly released. It was trained for a multi-class classification task with 3 possible labels. 

### Training hyperparameters

The following hyperparameters were used during training:
- learning_rate: 1e-05
- train_batch_size: 88
- eval_batch_size: 128
- seed: 42
- optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
- lr_scheduler_type: linear
- lr_scheduler_warmup_steps: 500
- num_epochs: 10
- mixed_precision_training: Native AMP

## Training Results

| Training Loss | Step | Validation Loss | F1 Micro |
|:-------------:|:----:|:---------------:|:--------:|
| 0.8035        | 500  | 0.5608          | 0.7821   |
| 0.4855        | 1000 | 0.4392          | 0.8266   |
| 0.3769        | 1500 | 0.3930          | 0.8433   |
| 0.3159        | 2000 | 0.3589          | 0.8675   |
| 0.279         | 2500 | 0.3552          | 0.8697   |
| 0.2463        | 3000 | 0.3812          | 0.8699   |
| 0.226         | 3500 | 0.3619          | 0.8690   |
| 0.2072        | 4000 | 0.3548          | 0.8754   |
| 0.1926        | 4500 | 0.3656          | 0.8763   |


### Framework versions

- Transformers 4.44.2
- Pytorch 2.4.0
- Datasets 3.0.0
- Tokenizers 0.19.1