Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rasbt
GitHub Repository: rasbt/machine-learning-book
Path: blob/main/ch15/ch15_part1.py
1245 views
1
# coding: utf-8
2
3
4
import sys
5
from python_environment_check import check_packages
6
import torch
7
import torch.nn as nn
8
9
# # Machine Learning with PyTorch and Scikit-Learn
10
# # -- Code Examples
11
12
# ## Package version checks
13
14
# Add folder to path in order to load from the check_packages.py script:
15
16
17
18
sys.path.insert(0, '..')
19
20
21
# Check recommended package versions:
22
23
24
25
26
27
d = {
28
'torch': '1.8.0',
29
}
30
check_packages(d)
31
32
33
# # Chapter 15: Modeling Sequential Data Using Recurrent Neural Networks (Part 1/3)
34
35
# **Outline**
36
#
37
# - [Introducing sequential data](#Introducing-sequential-data)
38
# - [Modeling sequential data -- order matters](#Modeling-sequential-data----order-matters)
39
# - [Sequential data versus time series data](#Sequential-data-versus-time-series-data)
40
# - [Representing sequences](#Representing-sequences)
41
# - [The different categories of sequence modeling](#The-different-categories-of-sequence-modeling)
42
# - [RNNs for modeling sequences](#RNNs-for-modeling-sequences)
43
# - [Understanding the dataflow in RNNs](#Understanding-the-dataflow-in-RNNs)
44
# - [Computing activations in an RNN](#Computing-activations-in-an-RNN)
45
# - [Hidden recurrence versus output recurrence](#Hidden-recurrence-versus-output-recurrence)
46
# - [The challenges of learning long-range interactions](#The-challenges-of-learning-long-range-interactions)
47
# - [Long short-term memory cells](#Long-short-term-memory-cells)
48
49
50
51
52
53
# # Introducing sequential data
54
#
55
# ## Modeling sequential data⁠—order matters
56
#
57
# ## Representing sequences
58
#
59
#
60
61
62
63
64
65
# ## The different categories of sequence modeling
66
67
68
69
70
71
# # RNNs for modeling sequences
72
#
73
# ## Understanding the RNN looping mechanism
74
#
75
76
77
78
79
80
81
82
83
84
# ## Computing activations in an RNN
85
#
86
87
88
89
90
91
92
93
94
95
# ## Hidden-recurrence vs. output-recurrence
96
97
98
99
100
101
102
103
104
torch.manual_seed(1)
105
106
rnn_layer = nn.RNN(input_size=5, hidden_size=2, num_layers=1, batch_first=True)
107
108
w_xh = rnn_layer.weight_ih_l0
109
w_hh = rnn_layer.weight_hh_l0
110
b_xh = rnn_layer.bias_ih_l0
111
b_hh = rnn_layer.bias_hh_l0
112
113
print('W_xh shape:', w_xh.shape)
114
print('W_hh shape:', w_hh.shape)
115
print('b_xh shape:', b_xh.shape)
116
print('b_hh shape:', b_hh.shape)
117
118
119
120
121
x_seq = torch.tensor([[1.0]*5, [2.0]*5, [3.0]*5]).float()
122
123
## output of the simple RNN:
124
output, hn = rnn_layer(torch.reshape(x_seq, (1, 3, 5)))
125
126
## manually computing the output:
127
out_man = []
128
for t in range(3):
129
xt = torch.reshape(x_seq[t], (1, 5))
130
print(f'Time step {t} =>')
131
print(' Input :', xt.numpy())
132
133
ht = torch.matmul(xt, torch.transpose(w_xh, 0, 1)) + b_xh
134
print(' Hidden :', ht.detach().numpy())
135
136
if t>0:
137
prev_h = out_man[t-1]
138
else:
139
prev_h = torch.zeros((ht.shape))
140
141
ot = ht + torch.matmul(prev_h, torch.transpose(w_hh, 0, 1)) + b_hh
142
ot = torch.tanh(ot)
143
out_man.append(ot)
144
print(' Output (manual) :', ot.detach().numpy())
145
print(' RNN output :', output[:, t].detach().numpy())
146
print()
147
148
149
# ## The challenges of learning long-range interactions
150
#
151
152
153
154
155
156
#
157
# ## Long Short-Term Memory cells
158
159
160
161
162
163
#
164
# ---
165
166
#
167
#
168
# Readers may ignore the next cell.
169
#
170
171
172
173
174
175