Skip to content

Commit fe89f03

Browse files
authored
Add/rnn (#188)
* index updated * added rnn md file * line removed
1 parent 77c20f3 commit fe89f03

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

docs/algorithms/deep-learning/neural-networks/index.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,13 @@
1212
</div>
1313
</a>
1414

15-
15+
<!-- recurrent neural network -->
16+
<a href="recurrent-neural-network" style="padding: 0 2px 0 16px; background-color: rgba(39, 39, 43, 0.4); border: 1px solid rgba(76, 76, 82, 0.4); border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); overflow: hidden; transition: transform 0.2s; display: flex; align-items: center; text-decoration: none; color: inherit;">
17+
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Recurrent_neural_network_unfold.svg/512px-Recurrent_neural_network_unfold.svg.png" alt="Recurrent Neural Network" style="width: 300px; height: 150px; object-fit: cover; border-radius: 10px;" />
18+
<div style="padding: 15px;">
19+
<h2 style="margin: 0; font-size: 20px;">Recurrent Neural Network</h2>
20+
<p style="font-size: 16px;">A deep learning model designed for sequential data processing.</p>
21+
<p style="font-size: 12px;">📅 2025-01-10 | ⏱️ 3 mins</p>
22+
</div>
23+
</a>
1624
</div>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# 🧪 Recurrent Neural Network (RNN)
2+
3+
<div align="center">
4+
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Recurrent_neural_network_unfold.svg/512px-Recurrent_neural_network_unfold.svg.png" />
5+
</div>
6+
7+
## 🎯 Objective
8+
Recurrent Neural Networks (RNNs) are a class of artificial neural networks designed to process sequential data. Unlike feedforward networks, RNNs have connections that allow information to persist, making them suitable for tasks such as speech recognition, text generation, and time-series forecasting.
9+
10+
## 📚 Prerequisites
11+
- Understanding of basic neural networks and deep learning
12+
- Knowledge of activation functions and backpropagation
13+
- Familiarity with sequence-based data processing
14+
- Libraries: NumPy, TensorFlow, PyTorch
15+
16+
---
17+
18+
## 🧬 Inputs
19+
- A sequence of data points such as text, speech signals, or time-series data.
20+
- Example: A sentence represented as a sequence of word embeddings for NLP tasks.
21+
22+
## 🎎 Outputs
23+
- Predicted sequence values or classifications.
24+
- Example: Next word prediction in a sentence or stock price forecasting.
25+
26+
---
27+
28+
## 🏩 RNN Architecture
29+
- RNNs maintain a **hidden state** that updates with each time step.
30+
- At each step, the hidden state is computed as:
31+
$$ h_t = f(W_h h_{t-1} + W_x x_t + b) $$
32+
- Variants of RNNs include **LSTMs (Long Short-Term Memory)** and **GRUs (Gated Recurrent Units)**, which help mitigate the vanishing gradient problem.
33+
34+
## 🏅 Training Process
35+
- The model is trained using **Backpropagation Through Time (BPTT)**.
36+
- Uses optimizers like **Adam** or **SGD**.
37+
- Typical hyperparameters:
38+
- Learning rate: 0.001
39+
- Batch size: 64
40+
- Epochs: 30
41+
- Loss function: Cross-entropy for classification tasks, MSE for regression tasks.
42+
43+
## 📊 Evaluation Metrics
44+
- Accuracy (for classification)
45+
- Perplexity (for language models)
46+
- Mean Squared Error (MSE) (for regression tasks)
47+
- BLEU Score (for sequence-to-sequence models)
48+
49+
---
50+
51+
## 💻 Code Implementation
52+
```python
53+
import numpy as np
54+
import torch
55+
import torch.nn as nn
56+
import torch.optim as optim
57+
58+
# Define RNN Model
59+
class RNN(nn.Module):
60+
def __init__(self, input_size, hidden_size, output_size):
61+
super(RNN, self).__init__()
62+
self.hidden_size = hidden_size
63+
self.rnn = nn.RNN(input_size, hidden_size, batch_first=True)
64+
self.fc = nn.Linear(hidden_size, output_size)
65+
66+
def forward(self, x, hidden):
67+
out, hidden = self.rnn(x, hidden)
68+
out = self.fc(out[:, -1, :])
69+
return out, hidden
70+
71+
# Model Training
72+
input_size = 10 # Number of input features
73+
hidden_size = 20 # Number of hidden neurons
74+
output_size = 1 # Output dimension
75+
76+
model = RNN(input_size, hidden_size, output_size)
77+
criterion = nn.MSELoss()
78+
optimizer = optim.Adam(model.parameters(), lr=0.001)
79+
80+
# Sample Training Loop
81+
for epoch in range(10):
82+
optimizer.zero_grad()
83+
inputs = torch.randn(32, 5, input_size) # (batch_size, seq_length, input_size)
84+
hidden = torch.zeros(1, 32, hidden_size) # Initial hidden state
85+
outputs, hidden = model(inputs, hidden)
86+
loss = criterion(outputs, torch.randn(32, output_size))
87+
loss.backward()
88+
optimizer.step()
89+
print(f"Epoch {epoch+1}, Loss: {loss.item()}")
90+
```
91+
92+
## 🔍 Understanding the Code
93+
- **Model Definition:**
94+
- The `RNN` class defines a simple recurrent neural network with an input layer, a recurrent layer, and a fully connected output layer.
95+
- **Forward Pass:**
96+
- Takes an input sequence, processes it through the RNN layer, and generates an output.
97+
- **Training Loop:**
98+
- Uses randomly generated data for demonstration.
99+
- Optimizes weights using the Adam optimizer and mean squared error loss.
100+
101+
---
102+
103+
## 🌟 Advantages
104+
- Effective for sequential data modeling.
105+
- Capable of handling variable-length inputs.
106+
- Works well for applications like text generation and speech recognition.
107+
108+
## ⚠️ Limitations
109+
- Struggles with long-range dependencies due to vanishing gradients.
110+
- Training can be slow due to sequential computations.
111+
- Alternatives like **LSTMs and GRUs** are preferred for longer sequences.
112+
113+
## 🚀 Applications
114+
### Natural Language Processing (NLP)
115+
- Text prediction
116+
- Sentiment analysis
117+
- Machine translation
118+
119+
### Time-Series Forecasting
120+
- Stock price prediction
121+
- Weather forecasting
122+
- Healthcare monitoring (e.g., ECG signals)

0 commit comments

Comments
 (0)