Skip to content

Instantly share code, notes, and snippets.

@Lanme
Created November 7, 2018 06:22
Show Gist options
  • Save Lanme/cd237b3e5d24da30592c0cff71420cdb to your computer and use it in GitHub Desktop.
Save Lanme/cd237b3e5d24da30592c0cff71420cdb to your computer and use it in GitHub Desktop.
basic_knowledge
#生成文本
class RNNNumpy:
def __init__(self, word_dim=128, hidden_dim=100):
self.word_dim = word_dim
self.hidden_dim = hidden_dim
self.U = np.random.uniform(-np.sqrt(1./word_dim), np.sqrt(1./word_dim), (hidden_dim, word_dim))
self.V = np.random.uniform(-np.sqrt(1./hidden_dim), np.sqrt(1./hidden_dim), (word_dim, hidden_dim))
self.W = np.random.uniform(-np.sqrt(1./hidden_dim), np.sqrt(1./hidden_dim), (hidden_dim, hidden_dim))
def forward_propagation(self, x):
T = len(x)
s = np.zeros((T + 1, self.hidden_dim))
s[-1] = np.zeros(self.hidden_dim)
o = np.zeros((T, self.word_dim))
for t in np.arange(T):
s[t] = np.tanh(self.U[:,x[t]] + self.W.dot(s[t-1]))
o[t] = softmax(self.V.dot(s[t]))
return [o, s]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment