-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedforward.py
More file actions
45 lines (38 loc) · 1.2 KB
/
feedforward.py
File metadata and controls
45 lines (38 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# feedforward.py
import numpy as np
class FeedForwardNetwork:
def __init__(self, d_model, d_ff):
"""
Two fully connected layers with ReLU in between.
d_model: input and output dimension
d_ff: hidden dimension (larger)
"""
self.W1 = np.random.randn(d_model, d_ff) * np.sqrt(2. / d_model)
self.b1 = np.zeros((d_ff,))
self.W2 = np.random.randn(d_ff, d_model) * np.sqrt(2. / d_ff)
self.b2 = np.zeros((d_model,))
def __call__(self, x):
"""
Forward pass.
Args:
x: (batch_size, seq_len, d_model)
Returns:
output: (batch_size, seq_len, d_model)
"""
# Linear layer 1 + ReLU
x = np.matmul(x, self.W1) + self.b1
x = np.maximum(0, x) # ReLU activation
# Linear layer 2
x = np.matmul(x, self.W2) + self.b2
return x
#testing feedforward
if __name__ == "__main__":
np.random.seed(0)
batch_size = 2
seq_len = 4
d_model = 8
d_ff = 32
x = np.random.randn(batch_size, seq_len, d_model)
ffn = FeedForwardNetwork(d_model, d_ff)
output = ffn(x)
print("FeedForward Output shape:", output.shape) # Should be (2, 4, 8)