suayptalha commited on
Commit
e969f55
·
verified ·
1 Parent(s): 4fc72c8

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +101 -1
README.md CHANGED
@@ -12,4 +12,104 @@ tags:
12
  - sentiment_analysis
13
  - custom_code
14
  - hf_integration
15
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  - sentiment_analysis
13
  - custom_code
14
  - hf_integration
15
+ ---
16
+
17
+ # MinGRU Sentiment Analysis
18
+
19
+ ![minGRU](minGRU.jpg)
20
+
21
+ First Hugging Face integration of minGRU models from the paper "[**Were RNNs All We Needed?**](https://arxiv.org/abs/2410.01201)".
22
+
23
+ This model uses BERT-Base-Uncased tokenizer.
24
+
25
+ For modeling and configuration codes: [**minGRU-hf**](https://github.com/suayptalha/minGRU-hf/tree/main)
26
+
27
+ # Example Usage:
28
+ ```py
29
+ from transformers import AutoModelForSequenceClassification
30
+ model = AutoModelForSequenceClassification.from_pretrained(
31
+ "suayptalha/minGRU-Sentiment-Analysis",
32
+ trust_remote_code = True
33
+ ).to("cuda")
34
+
35
+ tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
36
+
37
+ text = "The movie was absolutely wonderful, I loved it!"
38
+
39
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=128).to(device)
40
+
41
+ with torch.no_grad():
42
+ outputs = model(**inputs)
43
+ logits = outputs.logits
44
+ prediction = torch.argmax(logits, dim=-1).item()
45
+
46
+ sentiment = "positive" if prediction == 1 else "negative"
47
+ print(f"Text: {text}")
48
+ print(f"Predicted sentiment: {sentiment}")
49
+ ```
50
+
51
+ > Text: The movie was absolutely wonderful, I loved it!
52
+ > Predicted sentiment: positive
53
+
54
+ # Training:
55
+
56
+ Training code:
57
+
58
+ ```py
59
+ from torch.optim import AdamW
60
+ from torch.nn import CrossEntropyLoss
61
+ import matplotlib.pyplot as plt
62
+ from tqdm import tqdm
63
+
64
+ optimizer = AdamW(model.parameters(), lr=5e-5)
65
+ criterion = CrossEntropyLoss()
66
+
67
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
68
+ model.to(device)
69
+
70
+ num_epochs = 5
71
+ loss_values = []
72
+
73
+ for epoch in range(num_epochs):
74
+ model.train()
75
+ epoch_loss = 0
76
+ progress_bar = tqdm(train_dataloader, desc=f"Epoch {epoch + 1}")
77
+
78
+ for batch in progress_bar:
79
+ input_ids = batch["input_ids"].to(device)
80
+ labels = batch["label"].to(device)
81
+
82
+ optimizer.zero_grad()
83
+ outputs = model(input_ids=input_ids, labels=labels)
84
+ loss = outputs.loss
85
+ loss.backward()
86
+ optimizer.step()
87
+
88
+ epoch_loss += loss.item()
89
+ progress_bar.set_postfix(loss=epoch_loss / len(progress_bar))
90
+
91
+ avg_loss = epoch_loss / len(progress_bar)
92
+ loss_values.append(avg_loss)
93
+
94
+ # Loss Graph
95
+ plt.figure(figsize=(10, 6))
96
+ plt.plot(range(1, num_epochs + 1), loss_values, marker='o', label='Training Loss')
97
+ plt.xlabel("Epoch")
98
+ plt.ylabel("Loss")
99
+ plt.title("Training Loss Over Epochs")
100
+ plt.legend()
101
+ plt.grid(True)
102
+ plt.show()
103
+ ```
104
+
105
+ You can use this code snippet for fine-tuning!
106
+
107
+ # Loss Graph:
108
+
109
+ ![Loss Graph](loss.png)
110
+
111
+ # Credits:
112
+
113
+ https://arxiv.org/abs/2410.01201
114
+
115
+ I am thankful to Leo Feng, Frederick Tung, Mohamed Osama Ahmed, Yoshua Bengio and Hossein Hajimirsadeghi for their papers.