Question: Write the code to build an RNN architecture in the following class. import torch.nn as nn class RNN (nn. Module): def __init__(self, vocab_size, embedding_dim,
Write the code to build an RNN architecture in the following class. import torch.nn as nn class RNN (nn. Module): def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim, n_layers, bidirectional, dropout, pad_idx): super().__init__() #TO-DO #1. Initialize Embedding Layer #2. Initialize RNN layer #3. Initialize a fully connected layer with Linear transformation #4. Initialize Dropout def forward(self, text, text_lengths): #text= [sent_len, batch_size] #TO-DO #1. Apply embedding layer that matches each word to its vector and apply dropout. Dim [sent len, batch_size, es # 2. Run the RNN along the sentences of length sent_len. #output [sent len, batch size, hid dim num directio #3. Get last forward (hidden[-1,:,:]) hidden layer and apply dropout return self.fc(hidden)
Step by Step Solution
3.48 Rating (148 Votes )
There are 3 Steps involved in it
To create an RNN architecture using PyTorch you can define the RNN class as follows python import torch import torchnn as nn class RNNnnModule def ini... View full answer
Get step-by-step solutions from verified subject matter experts
