File size: 3,439 Bytes
81fb41a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0123efb
81fb41a
 
 
 
 
 
0123efb
 
 
5de38e4
 
 
 
2e14b76
5de38e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0123efb
81fb41a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
base_model: M-FAC/bert-tiny-finetuned-sst2
tags:
- generated_from_trainer
datasets:
- sst2
metrics:
- accuracy
model-index:
- name: results
  results:
  - task:
      name: Text Classification
      type: text-classification
    dataset:
      name: sst2
      type: sst2
      config: default
      split: validation
      args: default
    metrics:
    - name: Accuracy
      type: accuracy
      value: 0.8279816513761468
---

<!-- 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 Tiny for SST2

This model is a fine-tuned version of [M-FAC/bert-tiny-finetuned-sst2](https://huggingface.co/M-FAC/bert-tiny-finetuned-sst2) on the sst2 dataset.
It achieves the following results on the evaluation set:
- Loss: 0.4771
- Accuracy: 0.8280

## Usage Example

```python
from transformers import BertTokenizer, BertForSequenceClassification, TrainingArguments, Trainer, DataCollatorWithPadding
import datasets

model = BertForSequenceClassification.from_pretrained('VityaVitalich/bert-tiny-sst2')
tokenizer = BertTokenizer.from_pretrained('VityaVitalich/bert-tiny-sst2')

def create_data(tokenizer):
  train_set = datasets.load_dataset('sst2', split='train').remove_columns(['idx'])
  val_set = datasets.load_dataset('sst2', split='validation').remove_columns(['idx'])

  def tokenize_func(examples):
      return tokenizer(examples["sentence"], max_length=128, padding='max_length', truncation=True)

  encoded_dataset_train = train_set.map(tokenize_func, batched=True)
  encoded_dataset_test = val_set.map(tokenize_func, batched=True)
  data_collator = DataCollatorWithPadding(tokenizer)
  return encoded_dataset_train, encoded_dataset_test, data_collator

encoded_dataset_train, encoded_dataset_test, data_collator = create_data(tokenizer)

training_args = TrainingArguments(
    output_dir='./results',
    learning_rate=3e-5,
    per_device_train_batch_size=128,
    per_device_eval_batch_size=128,
    load_best_model_at_end=True,
    num_train_epochs=5,
    weight_decay=0.1,
    fp16=True,
    fp16_full_eval=True,
    evaluation_strategy="epoch",
    seed=42,
    save_strategy = "epoch",
    save_total_limit=5,
    logging_strategy="epoch",
    report_to="all",
)


trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=encoded_dataset_train,
    eval_dataset=encoded_dataset_test,
    data_collator=data_collator,
    compute_metrics=compute_metrics,
)

trainer.evaluate(encoded_dataset_test)
```


## Training procedure

### Training hyperparameters

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

### Training results

| Training Loss | Epoch | Step | Validation Loss | Accuracy |
|:-------------:|:-----:|:----:|:---------------:|:--------:|
| 0.2313        | 1.0   | 527  | 0.4771          | 0.8280   |
| 0.2057        | 2.0   | 1054 | 0.4937          | 0.8257   |
| 0.1949        | 3.0   | 1581 | 0.5121          | 0.8177   |
| 0.1904        | 4.0   | 2108 | 0.5100          | 0.8200   |
| 0.1879        | 5.0   | 2635 | 0.5137          | 0.8211   |


### Framework versions

- Transformers 4.34.0.dev0
- Pytorch 2.0.1+cu117
- Datasets 2.14.5
- Tokenizers 0.14.0