-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathsimple_rnn_test.py
86 lines (63 loc) · 1.59 KB
/
simple_rnn_test.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# https://deeplearningcourses.com/c/deep-learning-advanced-nlp
from __future__ import print_function, division
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future
from keras.models import Model
from keras.layers import Input, LSTM, GRU
import numpy as np
import matplotlib.pyplot as plt
try:
import keras.backend as K
if len(K.tensorflow_backend._get_available_gpus()) > 0:
from keras.layers import CuDNNLSTM as LSTM
from keras.layers import CuDNNGRU as GRU
except:
pass
T = 8
D = 2
M = 3
X = np.random.randn(1, T, D)
def lstm1():
input_ = Input(shape=(T, D))
rnn = LSTM(M, return_state=True)
x = rnn(input_)
model = Model(inputs=input_, outputs=x)
o, h, c = model.predict(X)
print("o:", o)
print("h:", h)
print("c:", c)
def lstm2():
input_ = Input(shape=(T, D))
rnn = LSTM(M, return_state=True, return_sequences=True)
# rnn = GRU(M, return_state=True)
x = rnn(input_)
model = Model(inputs=input_, outputs=x)
o, h, c = model.predict(X)
print("o:", o)
print("h:", h)
print("c:", c)
def gru1():
input_ = Input(shape=(T, D))
rnn = GRU(M, return_state=True)
x = rnn(input_)
model = Model(inputs=input_, outputs=x)
o, h = model.predict(X)
print("o:", o)
print("h:", h)
def gru2():
input_ = Input(shape=(T, D))
rnn = GRU(M, return_state=True, return_sequences=True)
x = rnn(input_)
model = Model(inputs=input_, outputs=x)
o, h = model.predict(X)
print("o:", o)
print("h:", h)
print("lstm1:")
lstm1()
print("lstm2:")
lstm2()
print("gru1:")
gru1()
print("gru2:")
gru2()