I have a question about shape of outputs of RNN in pytorch: these are from…
- User
I have a question about shape of outputs of RNN in pytorch:
these are from documentation, my question is about batch_first: torch.nn.RNN(input_size, hidden_size, num_layers=1, nonlinearity='tanh', bias=True, batch_first=False, dropout=0.0, bidirectional=False, device=None, dtype=None)
batch_first – If True, then the input and output tensors are provided as (batch, seq, feature) instead of (seq, batch, feature). Note that this does not apply to hidden or cell states. See the Inputs/Outputs sections below for details. Default: False
where:
N
batch size L
sequence length D
2 if bidirectional=True otherwise 1 H i n
input_size H o u t
hidden_size N= L= D= H in
H out
batch size sequence length 2 if bidirectional=True otherwise 1 input_size hidden_size
Outputs: output, h_n output: tensor of shape ( L , D ∗ H o u t ) (L,D∗H out ) for unbatched input, ( L , N , D ∗ H o u t ) (L,N,D∗H out ) when batch_first=False or ( N , L , D ∗ H o u t ) (N,L,D∗H out ) when batch_first=True containing the output features (h_t) from the last layer of the RNN, for each t. If a torch.nn.utils.rnn.PackedSequence has been given as the input, the output will also be a packed sequence.
h_n: tensor of shape ( D ∗ num_layers , H o u t ) (D∗num_layers,H out ) for unbatched input or ( D ∗ num_layers , N , H o u t ) (D∗num_layers,N,H out ) containing the final hidden state for each element in the batch.
this code is from a book:
D = 64 vocab_size = len(all_letters) hidden_nodes = 256 classes = len(dataset.label_names) first_rnn = nn.Sequential( nn.Embedding(vocab_size, D)#, (B, T) -> (B, T, D) nn.RNN(D, hidden_nodes, batch_first=True)#, (B, T, D) -> ( (B,T,D) , (S, B, D) ) LastTimeStep()#, The tanh activation is built into the RNN #object, so we don’t need to do it here. #We take the RNN output and reduce #it to one item, (B, D). nn.Linear(hidden_nodes, classes)#, (B, D) -> (B, classes) )
class LastTimeStep(nn.Module): '''''' A class for extracting the hidden activations of the last time step following the output of a PyTorch RNN module. '''''' def init(self, rnn_layers=1, bidirectional=False): super(LastTimeStep, self).init() self.rnn_layers = rnn_layers if bidirectional: self.num_directions = 2 else: self.num_directions = 1 def forward(self, input): rnn_output = input[0] #Result is either a tuple (out,ht) #or a tuple (out, (ht, ct)) last_step = input[1]# This is ht unless it’s a tuple, in which case if(type(last_step) == tuple): #it’s the first item in the tuple. last_step = last_step[0] batch_size = last_step.shape[1] #Per the docs, shape is #‘(num_layers * num_directions, last_step = last_step.view(self.rnn_layers, batch, hidden_size)’ #Reshapes so everything is separate self.num_directions, batch_size, -1) last_step = last_step[self.rnn_layers-1] #We want the last layer’s results. Reorders so the batch comes first last_step = last_step.permute(1, 0, 2) return last_step.reshape(batch_size, -1)
- Assistant