February 2018
Intermediate to advanced
262 pages
6h 59m
English
We discussed word embeddings briefly earlier. In this section, we create word embeddings as part of our network architecture and train the entire model to predict the sentiment of each review. At the end of the training, we will have a sentiment classifier model and also the word embeddings for the IMDB datasets. The following code demonstrates how to create a network architecture to predict the sentiment using word embeddings:
class EmbNet(nn.Module): def __init__(self,emb_size,hidden_size1,hidden_size2=400): super().__init__() self.embedding = nn.Embedding(emb_size,hidden_size1) self.fc = nn.Linear(hidden_size2,3) def forward(self,x): embeds = self.embedding(x).view(x.size(0),-1) out = self.fc(embeds) ...