Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
deeplearningzerotoall
GitHub Repository: deeplearningzerotoall/PyTorch
Path: blob/master/RNN/1-basics.py
618 views
1
import torch
2
import numpy as np
3
4
# Random seed to make results deterministic and reproducible
5
torch.manual_seed(0)
6
7
# declare dimension
8
input_size = 4
9
hidden_size = 2
10
11
# singleton example
12
# shape : (1, 1, 4)
13
# input_data_np = np.array([[[1, 0, 0, 0]]])
14
15
# sequential example
16
# shape : (3, 5, 4)
17
h = [1, 0, 0, 0]
18
e = [0, 1, 0, 0]
19
l = [0, 0, 1, 0]
20
o = [0, 0, 0, 1]
21
input_data_np = np.array([[h, e, l, l, o], [e, o, l, l, l], [l, l, e, e, l]], dtype=np.float32)
22
23
# transform as torch tensor
24
input_data = torch.Tensor(input_data_np)
25
26
# declare RNN
27
rnn = torch.nn.RNN(input_size, hidden_size)
28
29
# check output
30
outputs, _status = rnn(input_data)
31
print(outputs)
32
print(outputs.size())
33
34