File size: 4,523 Bytes
ebfc0f2
 
dad07d6
ebfc0f2
 
d3b16a7
 
ebfc0f2
 
 
 
 
 
 
 
 
 
4b1f2c1
d3b16a7
0a94565
 
ebfc0f2
 
 
561ce5a
ebfc0f2
 
 
561ce5a
ebfc0f2
 
 
4b1f2c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebfc0f2
4b1f2c1
ebfc0f2
 
 
 
 
 
 
 
 
 
 
 
d3b16a7
 
 
 
0a94565
 
 
d3b16a7
 
ebfc0f2
 
 
 
 
 
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
---
license: apache-2.0
base_model: bert-base-uncased
tags:
- generated_from_trainer
metrics:
- accuracy
model-index:
- name: bert-finetuned-sst2
  results: []
---

<!-- This model card has been generated automatically according to the information the Trainer had access to. You
should probably proofread and complete it, then remove this comment. -->

# bert-finetuned-sst2

This model is a fine-tuned version of [bert-base-uncased](https://huggingface.co/bert-base-uncased) on SST-2 dataset.
It achieves the following results on the evaluation set:
- Loss: 0.6081
- Accuracy: 0.64

## Model description

bert-finetuned-sst2 is based on the BERT base architecture, which includes 12 transformer layers, with an uncased vocabulary. This means the model does not differentiate between uppercase and lowercase letters, making it more versatile for text processing tasks. BERT has been pivotal in NLP for its deep understanding of language context and nuance, and this fine-tuned version carries those benefits into sentiment analysis. It was introduced by researchers at Google AI Language in a 2018 paper and has since become a staple for NLP tasks. This model is fine-tuned to classify sentences into positive or negative sentiments, making it ideal for analyzing customer feedback, social media sentiment, and other text where understanding sentiment is valuable.

## Intended uses & limitations

bert-finetuned-sst2 is intended for use in sentiment analysis applications across various domains such as social media monitoring, customer feedback analysis, and market research. It is optimized for English language text. While BERT's deep contextual understanding enables accurate sentiment classification, users should be aware of potential biases in the training data which could influence the model's outputs. This model may not perform as well on text from domains significantly different from the training data, such as highly technical documents or languages other than English.

## Training and evaluation data

[SST-2 dataset](https://huggingface.co/datasets/gimmaru/glue-sst2)
We randomly select 100 training data and 100 evaluation data.

## How to use

```
from datasets import load_dataset
from transformers import AutoTokenizer, DataCollatorWithPadding

raw_datasets = load_dataset("glue", "sst2")
checkpoint = "zhuchi76/bert-finetuned-sst2"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)


def tokenize_function(example):
    return tokenizer(example["sentence"], truncation=True)


tokenized_datasets = raw_datasets.map(tokenize_function, batched=True)

small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(100))
small_eval_dataset = tokenized_datasets["validation"].shuffle(seed=42).select(range(100))

data_collator = DataCollatorWithPadding(tokenizer=tokenizer)

from transformers import TrainingArguments

training_args = TrainingArguments(output_dir="bert-finetuned-sst2",
                                  evaluation_strategy="epoch",
                                  hub_model_id="zhuchi76/bert-finetuned-sst2")

from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)

from transformers import Trainer
trainer = Trainer(
    model,
    training_args,
    train_dataset=small_train_dataset, # if using cpu
    eval_dataset=small_eval_dataset, # if using cpu
    data_collator=data_collator,
    tokenizer=tokenizer,
    compute_metrics=compute_metrics,
)

# Evaluation
predictions = trainer.predict(small_eval_dataset)
print(predictions.predictions.shape, predictions.label_ids.shape)
preds = np.argmax(predictions.predictions, axis=-1)

import evaluate
metric = evaluate.load("glue", "sst2")
metric.compute(predictions=preds, references=predictions.label_ids)

```

### Training hyperparameters

The following hyperparameters were used during training:
- learning_rate: 5e-05
- train_batch_size: 8
- eval_batch_size: 8
- seed: 42
- optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
- lr_scheduler_type: linear
- num_epochs: 3.0

### Training results

| Training Loss | Epoch | Step | Validation Loss | Accuracy |
|:-------------:|:-----:|:----:|:---------------:|:--------:|
| No log        | 1.0   | 13   | 0.6714          | 0.57     |
| No log        | 2.0   | 26   | 0.6477          | 0.65     |
| No log        | 3.0   | 39   | 0.6081          | 0.64     |


### Framework versions

- Transformers 4.38.2
- Pytorch 2.2.1+cu121
- Datasets 2.18.0
- Tokenizers 0.15.2