def __init__(self, dir_st, vocab, save=False, dropout=0.25, fixed_emb=False): super(DropUniSkip, self).__init__(dir_st, vocab, save, dropout, fixed_emb) # Modules self.seq_drop_x = SequentialDropout(p=self.dropout) self.seq_drop_h = SequentialDropout(p=self.dropout)
def set_dropout(self, dropout): self.dropout = dropout self.drop_ir = SequentialDropout(p=dropout) self.drop_ii = SequentialDropout(p=dropout) self.drop_in = SequentialDropout(p=dropout) self.drop_hr = SequentialDropout(p=dropout) self.drop_hi = SequentialDropout(p=dropout) self.drop_hn = SequentialDropout(p=dropout)
class BayesianGRUCell(AbstractGRUCell): def __init__(self, input_size, hidden_size, bias_ih=True, bias_hh=False, dropout=0.25): super(BayesianGRUCell, self).__init__(input_size, hidden_size, bias_ih, bias_hh) self.set_dropout(dropout) def set_dropout(self, dropout): self.dropout = dropout self.drop_ir = SequentialDropout(p=dropout) self.drop_ii = SequentialDropout(p=dropout) self.drop_in = SequentialDropout(p=dropout) self.drop_hr = SequentialDropout(p=dropout) self.drop_hi = SequentialDropout(p=dropout) self.drop_hn = SequentialDropout(p=dropout) def end_of_sequence(self): self.drop_ir.end_of_sequence() self.drop_ii.end_of_sequence() self.drop_in.end_of_sequence() self.drop_hr.end_of_sequence() self.drop_hi.end_of_sequence() self.drop_hn.end_of_sequence() def forward(self, x, hx=None): if hx is None: hx = Variable(x.data.new().resize_( (x.size(0), self.hidden_size)).fill_(0)) x_ir = self.drop_ir(x) x_ii = self.drop_ii(x) x_in = self.drop_in(x) x_hr = self.drop_hr(hx) x_hi = self.drop_hi(hx) x_hn = self.drop_hn(hx) r = F.sigmoid(self.weight_ir(x_ir) + self.weight_hr(x_hr)) i = F.sigmoid(self.weight_ii(x_ii) + self.weight_hi(x_hi)) n = F.tanh(self.weight_in(x_in) + r * self.weight_hn(x_hn)) hx = (1 - i) * n + i * hx return hx