Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
deeplearningzerotoall
GitHub Repository: deeplearningzerotoall/PyTorch
Path: blob/master/RNN/7-packed_sequence.py
618 views
1
import torch
2
import numpy as np
3
from torch.nn.utils.rnn import pad_sequence, pack_sequence, pack_padded_sequence, pad_packed_sequence
4
5
# Random word from random word generator
6
data = ['hello world',
7
'midnight',
8
'calculation',
9
'path',
10
'short circuit']
11
12
# Make dictionary
13
char_set = ['<pad>'] + list(set(char for seq in data for char in seq)) # Get all characters and include pad token
14
char2idx = {char: idx for idx, char in enumerate(char_set)} # Constuct character to index dictionary
15
print('char_set:', char_set)
16
print('char_set length:', len(char_set))
17
18
# Convert character to index and
19
# Make list of tensors
20
X = [torch.LongTensor([char2idx[char] for char in seq]) for seq in data]
21
22
# Check converted result
23
for sequence in X:
24
print(sequence)
25
26
# Make length tensor (will be used later in 'pack_padded_sequence' function)
27
lengths = [len(seq) for seq in X]
28
print('lengths:', lengths)
29
30
# Make a Tensor of shape (Batch x Maximum_Sequence_Length)
31
padded_sequence = pad_sequence(X, batch_first=True) # X is now padded sequence
32
print(padded_sequence)
33
print(padded_sequence.shape)
34
35
# Sort by descending lengths
36
sorted_idx = sorted(range(len(lengths)), key=lengths.__getitem__, reverse=True)
37
sorted_X = [X[idx] for idx in sorted_idx]
38
39
# Check converted result
40
for sequence in sorted_X:
41
print(sequence)
42
43
packed_sequence = pack_sequence(sorted_X)
44
print(packed_sequence)
45
46
# one-hot embedding using PaddedSequence
47
eye = torch.eye(len(char_set)) # Identity matrix of shape (len(char_set), len(char_set))
48
embedded_tensor = eye[padded_sequence]
49
print(embedded_tensor.shape) # shape: (Batch_size, max_sequence_length, number_of_input_tokens)
50
51
# one-hot embedding using PackedSequence
52
embedded_packed_seq = pack_sequence([eye[X[idx]] for idx in sorted_idx])
53
print(embedded_packed_seq.data.shape)
54
55
# declare RNN
56
rnn = torch.nn.RNN(input_size=len(char_set), hidden_size=30, batch_first=True)
57
58
# Try out PaddedSequence
59
rnn_output, hidden = rnn(embedded_tensor)
60
print(rnn_output.shape) # shape: (batch_size, max_seq_length, hidden_size)
61
print(hidden.shape) # shape: (num_layers * num_directions, batch_size, hidden_size)
62
63
# Try out PackedSequence
64
rnn_output, hidden = rnn(embedded_packed_seq)
65
print(rnn_output.data.shape)
66
print(hidden.data.shape)
67
68
# Try out pad_packed_sequence
69
unpacked_sequence, seq_lengths = pad_packed_sequence(embedded_packed_seq, batch_first=True)
70
print(unpacked_sequence.shape)
71
print(seq_lengths)
72
73
# Construct embedded_padded_sequence
74
embedded_padded_sequence = eye[pad_sequence(sorted_X, batch_first=True)]
75
print(embedded_padded_sequence.shape)
76
77
# Try out pack_padded_sequence
78
sorted_lengths = sorted(lengths, reverse=True)
79
new_packed_sequence = pack_padded_sequence(embedded_padded_sequence, sorted_lengths, batch_first=True)
80
print(new_packed_sequence.data.shape)
81
print(new_packed_sequence.batch_sizes)
82
83
84
85