2
2
3
3
# RNN example "abba" detector
4
4
#
5
- # see this for a more complex example:
5
+ # another simple example:
6
+ # https://github.com/pytorch/examples/tree/master/time_sequence_prediction
7
+ # a more complex example:
6
8
# http://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html
7
9
#
8
10
# E. Culurciello, April 2017
27
29
num_symbols = 2 # a,b
28
30
data_size = 256
29
31
seq_len = 4 # abba sequence to be detected only!
32
+ num_layers = 3
30
33
rdata = np .random .randint (0 , num_symbols , data_size ) # 0=1, 1=b, for example
31
34
32
35
# turn it into 1-hot encoding:
49
52
50
53
51
54
# create model:
52
- model = nn .RNN (num_symbols , num_symbols , 1 ) # see: http://pytorch.org/docs/nn.html#rnn
55
+ model = nn .RNN (num_symbols , num_symbols , num_layers ) # see: http://pytorch.org/docs/nn.html#rnn
53
56
criterion = nn .MSELoss ()
54
57
optimizer = optim .Adam (model .parameters (), lr = 0.005 )
55
58
65
68
# print('model test:', output,hn)
66
69
67
70
71
+ def repackage_hidden (h ):
72
+ """Wraps hidden states in new Variables, to detach them from their history."""
73
+ if type (h ) == Variable :
74
+ return Variable (h .data )
75
+ else :
76
+ return tuple (repackage_hidden (v ) for v in h )
77
+
78
+
68
79
num_epochs = 4
69
80
70
81
71
82
def train ():
72
83
model .train ()
73
- hidden = Variable (torch .zeros (1 , 1 , num_symbols ))
84
+ hidden = Variable (torch .zeros (num_layers , 1 , num_symbols ))
74
85
75
86
for epoch in range (num_epochs ): # loop over the dataset multiple times
76
-
77
87
running_loss = 0.0
78
88
for i in range (0 , data_size - seq_len , seq_len ):
79
89
# get inputs:
@@ -83,12 +93,16 @@ def train():
83
93
# wrap them in Variable
84
94
inputs , labels = Variable (inputs ), Variable (labels )
85
95
96
+ # Starting each batch, we detach the hidden state from how it was previously produced.
97
+ # If we didn't, the model would try backpropagating all the way to start of the dataset.
98
+ hidden = repackage_hidden (hidden )
99
+
86
100
# forward, backward, optimize
87
- optimizer .zero_grad ()
101
+ model .zero_grad ()
88
102
output , hidden = model (inputs , hidden )
89
103
90
104
loss = criterion (output , labels )
91
- loss .backward (retain_variables = True )
105
+ loss .backward ()
92
106
optimizer .step ()
93
107
94
108
# print info / statistics:
@@ -105,19 +119,14 @@ def train():
105
119
106
120
def test ():
107
121
model .eval ()
108
- hidden = Variable (torch .zeros (1 , 1 , num_symbols ))
122
+ hidden = Variable (torch .zeros (num_layers , 1 , num_symbols ))
109
123
for i in range (0 , data_size - seq_len , seq_len ):
110
-
111
124
inputs = torch .from_numpy ( data [i :i + seq_len ,:]).view (seq_len , 1 , num_symbols ).float ()
112
125
labels = torch .from_numpy (label [i :i + seq_len ,:]).view (seq_len , 1 , num_symbols ).float ()
113
-
114
- inputs = Variable (inputs )
115
-
126
+ inputs = Variable (inputs )
116
127
output , hidden = model (inputs , hidden )
117
-
118
128
print ('in:' , data [i :i + seq_len ,0 ], 'label:' , label [i :i + seq_len ,1 ], 'out:' , output .data .numpy ())
119
- if label [i ,1 ]> 0 :
120
- print ('RIGHT\n \n ' )
129
+
121
130
122
131
# train model:
123
132
print ('\n TRAINING ---' )
0 commit comments