Repository navigation
Expand file tree
/
Copy pathtoy_gru.cpp
More file actions
58 lines (45 loc) · 1.68 KB
/
Copy pathtoy_gru.cpp
File metadata and controls
58 lines (45 loc) · 1.68 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
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
struct GRU {
// Weights and biases for gates and update
vector<vector<double>> Wz, Wr, Wh;
vector<double> bz, br, bh;
// Hidden state and output
vector<double> h_prev, h_t;
GRU(int input_size, int hidden_size) {
// Initialize weights and biases with appropriate dimensions
Wz = random_matrix(hidden_size, input_size + hidden_size);
Wr = random_matrix(hidden_size, input_size + hidden_size);
Wh = random_matrix(hidden_size, input_size + hidden_size);
bz = random_vector(hidden_size);
br = random_vector(hidden_size);
bh = random_vector(hidden_size);
// Initialize hidden state
h_prev = random_vector(hidden_size);
}
vector<double> forward(vector<double> x_t) {
// Combine input and previous hidden state
vector<double> z = concatenate(x_t, h_prev);
// Update gate
vector<double> zt = sigmoid(dot(Wz, z) + bz);
// Reset gate
vector<double> rt = sigmoid(dot(Wr, z) + br);
// Candidate activation
vector<double> ht_hat = tanh(dot(Wh, concatenate(x_t, multiply(rt, h_prev))) + bh);
// Final hidden state
h_t = multiply(zt, h_prev) + multiply((1 - zt), ht_hat);
return h_t; // Return hidden state as output
}
private:
// Helper functions for matrix operations and activations
// ... (implement dot product, sigmoid, tanh, etc.)
};
int main() {
// Example usage
GRU gru(5, 10); // Input size 5, hidden size 10
vector<double> input = {1, 2, 3, 4, 5};
vector<double> output = gru.forward(input);
// Process output...
}