February 2018
Intermediate to advanced
262 pages
6h 59m
English
Let's look at the code and then walk through the code. You may be surprised at how similar the code looks:
class IMDBRnn(nn.Module): def __init__(self,vocab,hidden_size,n_cat,bs=1,nl=2): super().__init__() self.hidden_size = hidden_size self.bs = bs self.nl = nl self.e = nn.Embedding(n_vocab,hidden_size) self.rnn = nn.LSTM(hidden_size,hidden_size,nl) self.fc2 = nn.Linear(hidden_size,n_cat) self.softmax = nn.LogSoftmax(dim=-1) def forward(self,inp): bs = inp.size()[1] if bs != self.bs: self.bs = bs e_out = self.e(inp) h0 = c0 = Variable(e_out.data.new(*(self.nl,self.bs,self.hidden_size)).zero_()) rnn_o,_ = self.rnn(e_out,(h0,c0)) rnn_o = rnn_o[-1] fc = F.dropout(self.fc2(rnn_o),p=0.8) return self.softmax(fc)
The