|
--- |
|
tags: |
|
- pytorch_model_hub_mixin |
|
- model_hub_mixin |
|
--- |
|
# nvidia/domain-classifier |
|
|
|
This repository contains the code for the domain classifier model. |
|
|
|
# How to use in transformers |
|
To use the Domain classifier, use the following code: |
|
|
|
```python3 |
|
|
|
import torch |
|
from torch import nn |
|
from transformers import AutoModel, AutoTokenizer, AutoConfig |
|
from huggingface_hub import PyTorchModelHubMixin |
|
|
|
class CustomModel(nn.Module, PyTorchModelHubMixin): |
|
def __init__(self, config): |
|
super(CustomModel, self).__init__() |
|
self.model = AutoModel.from_pretrained(config['base_model']) |
|
self.dropout = nn.Dropout(config['fc_dropout']) |
|
self.fc = nn.Linear(self.model.config.hidden_size, len(config['id2label'])) |
|
|
|
def forward(self, input_ids, attention_mask): |
|
features = self.model(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state |
|
dropped = self.dropout(features) |
|
outputs = self.fc(dropped) |
|
return torch.softmax(outputs[:, 0, :], dim=1) |
|
|
|
# Setup configuration and model |
|
config = AutoConfig.from_pretrained("nvidia/domain-classifier") |
|
tokenizer = AutoTokenizer.from_pretrained("nvidia/domain-classifier") |
|
model = CustomModel.from_pretrained("nvidia/domain-classifier") |
|
|
|
# Prepare and process inputs |
|
text_samples = ["Sports is a popular domain", "Politics is a popular domain"] |
|
inputs = tokenizer(text_samples, return_tensors="pt", padding="longest", truncation=True) |
|
outputs = model(inputs['input_ids'], inputs['attention_mask']) |
|
|
|
# Predict and display results |
|
predicted_classes = torch.argmax(outputs, dim=1) |
|
predicted_domains = [config.id2label[class_idx.item()] for class_idx in predicted_classes.cpu().numpy()] |
|
print(predicted_domains) |
|
# ['Sports', 'News'] |
|
``` |