pulkitchowdry commited on
Commit
e06a2f3
·
verified ·
1 Parent(s): d4fa62e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +65 -15
README.md CHANGED
@@ -8,7 +8,6 @@ datasets:
8
  language:
9
  - en
10
  pipeline_tag: text-classification
11
- library_name: transformers
12
  ---
13
  # Sample AG News Classifier
14
 
@@ -22,28 +21,79 @@ The model has been trained to classify news headlines into 4 categories: World,
22
  Note: Currently limited to above 4 categories only
23
 
24
  # How to use
25
- You can use the model using pipeline or AutoModelForMultimodalLM options offered by HuggingFace
26
 
 
 
27
  ```python
28
- # Use a pipeline as a high-level helper
29
- from transformers import pipeline
 
30
 
31
- pipe = pipeline("text-classification", model="pulkitchowdry/sample-agnews-classifer")
32
- text = "US-Iran sign a peace deal"
33
- pipe(text)
34
 
35
- # Load model directly
36
- from transformers import AutoTokenizer, AutoModel
 
 
 
 
 
37
 
38
- tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
39
- model = AutoModel.from_pretrained("pulkitchowdry/sample-agnews-classifer", dtype="auto")
40
 
41
- text = "US-Iran sign a peace deal"
42
- inputs = tokenizer(text, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
43
- prediction = model(inputs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  categories = ["World", "Sports", "Business", "Science/Technology"]
45
 
46
- print(categories[prediction])
 
47
  ```
48
 
49
  # Limitations and bias
 
8
  language:
9
  - en
10
  pipeline_tag: text-classification
 
11
  ---
12
  # Sample AG News Classifier
13
 
 
21
  Note: Currently limited to above 4 categories only
22
 
23
  # How to use
24
+ The model has been developed using PyTorch and since it is a custom neural network architecture, it can be used by performing the below steps and not using the Transformers library,
25
 
26
+ 1. Create your model class
27
+
28
  ```python
29
+ import torch
30
+ import torch.nn as nn
31
+ from huggingface_hub import PyTorchModelHubMixin
32
 
 
 
 
33
 
34
+ class TextClassifier(nn.Module, PyTorchModelHubMixin):
35
+ def __init__(self, vocab_size=30522, embed_dim=128, num_classes=4):
36
+ super().__init__()
37
+ self.embedding = nn.Embedding(vocab_size, embed_dim)
38
+ self.fc1 = nn.Linear(embed_dim, 128)
39
+ self.relu = nn.ReLU()
40
+ self.fc2 = nn.Linear(128, num_classes)
41
 
42
+ def forward(self, input_ids, attention_mask=None):
43
+ x = self.embedding(input_ids)
44
 
45
+ if attention_mask is not None:
46
+ mask = attention_mask.unsqueeze(-1).float()
47
+ x = x * mask
48
+ x = x.sum(dim=1) / mask.sum(dim=1).clamp(min=1e-9)
49
+ else:
50
+ x = x.mean(dim=1)
51
+
52
+ x = self.fc1(x)
53
+ x = self.relu(x)
54
+ x = self.fc2(x)
55
+ return x
56
+
57
+ model = TextClassifier.from_pretrained("pulkitchowdry/sample-agnews-classifer")
58
+ model.eval()
59
+
60
+ ```
61
+
62
+ 2. Setup Tokenizer
63
+
64
+ ```python
65
+
66
+ from transformers import AutoTokenizer
67
+
68
+ tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
69
+ text = "Messi scores a hatrick in the world cup"
70
+
71
+ inputs = tokenizer(
72
+ text,
73
+ return_tensors="pt",
74
+ padding="max_length",
75
+ truncation=True,
76
+ max_length=128
77
+ )
78
+
79
+ ```
80
+
81
+ 3. Run the prediction
82
+
83
+ ```python
84
+
85
+ with torch.no_grad():
86
+ logits = model(
87
+ inputs["input_ids"],
88
+ attention_mask=inputs["attention_mask"]
89
+ )
90
+ prediction = logits.argmax(dim=1).item()
91
+
92
+ print(f"{prediction}")
93
  categories = ["World", "Sports", "Business", "Science/Technology"]
94
 
95
+ print("Predicted class: ", categories[prediction])
96
+
97
  ```
98
 
99
  # Limitations and bias