Text Classification
Transformers
PyTorch
Safetensors
German
bert
feature-extraction
text-embeddings-inference
Instructions to use hplisiecki/word2affect_german with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use hplisiecki/word2affect_german with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="hplisiecki/word2affect_german")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModel tokenizer = AutoTokenizer.from_pretrained("hplisiecki/word2affect_german") model = AutoModel.from_pretrained("hplisiecki/word2affect_german", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download model_script.py from hplisiecki/word2affect_german: direct link, hf CLI and curl.
- Browser
- Download file 1.77 kB
-
https://huggingface.co/hplisiecki/word2affect_german/resolve/main/model_script.py
- Command line
-
hf download hf://hplisiecki/word2affect_german/model_script.py
-
curl -L -o model_script.py https://huggingface.co/hplisiecki/word2affect_german/resolve/main/model_script.py
1.77 kB
| import torch | |
| from torch import nn | |
| from transformers import AutoModel | |
| class CustomModel(torch.nn.Module): | |
| def __init__(self, model_path, dropout=0.1, hidden_dim=768): | |
| super().__init__() | |
| self.metric_names = ['valence', 'arousal', 'imageability'] | |
| self.dropout_rate = dropout | |
| self.hidden_dim = hidden_dim | |
| self.bert = AutoModel.from_pretrained(model_path) | |
| for name in self.metric_names: | |
| setattr(self, name, nn.Linear(hidden_dim, 1)) | |
| setattr(self, 'l_1_' + name, nn.Linear(hidden_dim, hidden_dim)) | |
| self.layer_norm = nn.LayerNorm(self.hidden_dim) | |
| self.relu = nn.ReLU() | |
| self.dropout = nn.Dropout(self.dropout_rate) | |
| self.sigmoid = nn.Sigmoid() | |
| def save_pretrained(self, save_directory): | |
| self.bert.save_pretrained(save_directory) | |
| torch.save(self.state_dict(), f'{save_directory}/pytorch_model.bin') | |
| def from_pretrained(cls, model_dir, dropout=0.2, hidden_dim=768): | |
| model = cls(model_dir, dropout, hidden_dim) | |
| state_dict = torch.load(f'{model_dir}/pytorch_model.bin', map_location=torch.device('cpu')) | |
| model.load_state_dict(state_dict) | |
| return model | |
| def forward(self, *args): | |
| _, x = self.bert(*args, return_dict=False) | |
| output = self.rate_embedding(x) | |
| return output | |
| def rate_embedding(self, x): | |
| output_ratings = [] | |
| for name in self.metric_names: | |
| first_layer = self.relu(self.dropout(self.layer_norm(getattr(self, 'l_1_' + name)(x) + x))) | |
| second_layer = self.sigmoid(getattr(self, name)(first_layer)) | |
| output_ratings.append(second_layer) | |
| return output_ratings |