Update README.md
Browse files
README.md
CHANGED
|
@@ -2,8 +2,62 @@
|
|
| 2 |
tags:
|
| 3 |
- model_hub_mixin
|
| 4 |
- pytorch_model_hub_mixin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
---
|
| 6 |
|
| 7 |
This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration:
|
| 8 |
- Library: [More Information Needed]
|
| 9 |
-
- Docs: [More Information Needed]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
tags:
|
| 3 |
- model_hub_mixin
|
| 4 |
- pytorch_model_hub_mixin
|
| 5 |
+
language:
|
| 6 |
+
- en
|
| 7 |
+
metrics:
|
| 8 |
+
- accuracy
|
| 9 |
+
base_model:
|
| 10 |
+
- google-bert/bert-base-uncased
|
| 11 |
+
pipeline_tag: text-classification
|
| 12 |
---
|
| 13 |
|
| 14 |
This model has been pushed to the Hub using the [PytorchModelHubMixin](https://huggingface.co/docs/huggingface_hub/package_reference/mixins#huggingface_hub.PyTorchModelHubMixin) integration:
|
| 15 |
- Library: [More Information Needed]
|
| 16 |
+
- Docs: [More Information Needed]
|
| 17 |
+
|
| 18 |
+
The model class looks like the following:
|
| 19 |
+
```python
|
| 20 |
+
from transformers import BertModel
|
| 21 |
+
|
| 22 |
+
class BertClassifier(nn.Module, PyTorchModelHubMixin):
|
| 23 |
+
def __init__(self, dataset: str, num_classes, dropout=0.5):
|
| 24 |
+
|
| 25 |
+
super(BertClassifier, self).__init__()
|
| 26 |
+
|
| 27 |
+
self.model_name = "bert-base-uncased"
|
| 28 |
+
print(f"Loading BERT model {self.model_name} for {dataset} dataset...")
|
| 29 |
+
|
| 30 |
+
self.bert = BertModel.from_pretrained(self.model_name)
|
| 31 |
+
self.dropout = nn.Dropout(dropout)
|
| 32 |
+
self.linear = nn.Linear(768, num_classes) # in features, out features = number of classes
|
| 33 |
+
self.relu = nn.ReLU()
|
| 34 |
+
|
| 35 |
+
def forward(self, input_ids, attention_mask):
|
| 36 |
+
|
| 37 |
+
_, pooled_output = self.bert(input_ids=input_ids, attention_mask=attention_mask, return_dict=False)
|
| 38 |
+
dropout_output = self.dropout(pooled_output)
|
| 39 |
+
linear_output = self.linear(dropout_output)
|
| 40 |
+
final_layer = self.relu(linear_output)
|
| 41 |
+
|
| 42 |
+
return final_layer
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
The H&M dataset has 89 classes. Loading in the model looks like this:
|
| 46 |
+
```python
|
| 47 |
+
model = BertClassifier.from_pretrained("CDL-RecSys/BERT-uncased-hm-category-classifier")
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
Before classifying the input sequence, the phrase needs to be processed with the `BertTokenizer`:
|
| 51 |
+
```python
|
| 52 |
+
from transformers import BertModel, BertTokenizer
|
| 53 |
+
|
| 54 |
+
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
|
| 55 |
+
input = "Short, sleeveless dress in an airy cottonweave that is open at the back with a tie."
|
| 56 |
+
|
| 57 |
+
texts = self.tokenizer(batch, padding='max_length', max_length = 512, truncation=True,return_tensors="pt")
|
| 58 |
+
input_ids = texts["input_ids"]
|
| 59 |
+
attention_mask = texts["attention_mask"]
|
| 60 |
+
|
| 61 |
+
output = model(input_ids, attention_mask)
|
| 62 |
+
class = output.argmax(dim=1)
|
| 63 |
+
```
|