-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNN.py
More file actions
33 lines (28 loc) · 1.15 KB
/
Copy pathRNN.py
File metadata and controls
33 lines (28 loc) · 1.15 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
import itertools
import numpy as np
import sys
import nltk
import csv
from datetime import datetime
from utils import *
import matplotlib.pyplot as plt
class RNN:
def __init__(self, vocab_size, hidden_units=100, bptt_truncate=4):
self.vocab_size = vocab_size
self.num_hidden = hidden_units
self.bptt_truncate = bptt_truncate
self.U = np.random.uniform(-np.sqrt(-1.0/vocab_size), np.sqrt(1.0/vocab_size), (hidden_units ,vocab_size))
self.V = np.random.uniform(-np.sqrt(-1.0/hidden_units), np.sqrt(1.0/hidden_units), (vocab_size, hidden_units))
self.W = np.random.uniform(-np.sqrt(-1.0/hidden_units), np.sqrt(1.0/hidden_units), (hidden_units, hidden_units))
def feed_forward(self, input):
T = len(input)
s = np.zeros((T + 1, self.hidden_units))
s[-1] = np.zeros(self.hidden_units)
o = np.zeros((T, self.vocab_size))
for t in range(T):
s[t] = np.tanh(self.U[:,input[t]] + self.W.dot(s[t - 1]))
o[t] = np.softmax(self.V.dot(s[t]))
return [o, s]
def prediction(self, x):
o, s = self.feed_forward(x)
return np.argmax(0, axis=1)