def __init__(self, encoder, decoder): super(Seq2Seq, self).__init__() self.encoder = encoder self.decoder = decoder if not hasattr(self, 'activations'): self.activations = neural.Activations()
def __init__(self, tgt_vocab, cell_size, embed_size, dropout, delimiters, rnn_cell='LSTM', num_layers=1, beam_size=1, extra_input_size=0, max_len=None, activations=None): super(RNNDecoder, self).__init__() self.monitor_activations = (activations is not None) if isinstance(activations, neural.Activations): self.activations = activations else: self.activations = neural.Activations() self.dec_embedding = th.nn.Embedding(tgt_vocab, embed_size) cell = CELLS[rnn_cell] self.decoder = cell(input_size=embed_size + extra_input_size, hidden_size=cell_size, num_layers=num_layers, dropout=dropout, batch_first=True) self.output = th.nn.Linear(cell_size, tgt_vocab) self.beam_predictor = BeamPredictor(self.decode, beam_size=beam_size, max_len=max_len, delimiters=delimiters) self.sampler = Sampler(self.decode, max_len=max_len, delimiters=delimiters)
def __init__(self, options, goal_vocab, resp_vocab, sel_vocab, delimiters, monitor_activations=True): super(Negotiator, self).__init__() self.monitor_activations = monitor_activations self.activations = neural.Activations() if monitor_activations: child_activations = self.activations else: child_activations = None self.h_init = th.nn.Linear(1, options.cell_size * options.num_layers, bias=False) self.c_init = th.nn.Linear(1, options.cell_size * options.num_layers, bias=False) self.context_encoder = seq2seq.RNNEncoder(src_vocab=goal_vocab, cell_size=options.cell_size, embed_size=options.embed_size, dropout=options.dropout, delimiters=delimiters[0], rnn_cell=options.rnn_cell, num_layers=options.num_layers, bidirectional=False, activations=child_activations) self.response_decoder = seq2seq.RNNDecoder(tgt_vocab=resp_vocab, cell_size=options.cell_size, embed_size=options.embed_size, dropout=options.dropout, delimiters=delimiters[1], rnn_cell=options.rnn_cell, num_layers=options.num_layers, beam_size=options.beam_size, extra_input_size=options.cell_size, max_len=options.max_length, activations=child_activations) self.response_encoder = seq2seq.RNNEncoder(src_vocab=resp_vocab, cell_size=options.cell_size, embed_size=options.embed_size, dropout=options.dropout, delimiters=delimiters[1], rnn_cell=options.rnn_cell, num_layers=options.num_layers, bidirectional=options.bidirectional, activations=child_activations) self.combined_layer = th.nn.Linear(options.cell_size * 2, options.cell_size, bias=False) self.selection_layer = th.nn.Linear(options.cell_size, sel_vocab, bias=False)
def __init__(self, src_vocab, cell_size, embed_size, dropout, delimiters, rnn_cell='LSTM', num_layers=1, bidirectional=False, attention=False, activations=None): super(RNNEncoder, self).__init__() self.monitor_activations = (activations is not None) if isinstance(activations, neural.Activations): self.activations = activations else: self.activations = neural.Activations() self.enc_embedding = th.nn.Embedding(src_vocab, embed_size) cell = CELLS[rnn_cell] self.use_c = (rnn_cell == 'LSTM') if cell_size % 2 != 0: raise ValueError( 'cell_size must be even for bidirectional encoder ' '(instead got {})'.format(cell_size)) self.num_directions = 2 if bidirectional else 1 self.num_layers = num_layers self.cell_size = cell_size self.cell = cell(input_size=embed_size, hidden_size=cell_size // self.num_directions, num_layers=num_layers, dropout=dropout, batch_first=True, bidirectional=bidirectional) self.h_init = th.nn.Linear(1, cell_size * num_layers, bias=False) self.c_init = th.nn.Linear(1, cell_size * num_layers, bias=False) self.use_attention = attention if attention: self.attention = Attention(cell_size, activations=activations, save_weights=True)
def __init__(self, src_vocab, tgt_vocab, cell_size, embed_size, dropout, delimiters, rnn_cell='LSTM', num_layers=1, beam_size=1, bidirectional=False, attention=False, max_len=None, monitor_activations=True): self.activations = neural.Activations() if monitor_activations: child_activations = self.activations else: child_activations = None encoder = RNNEncoder(src_vocab=src_vocab, cell_size=cell_size, embed_size=embed_size, dropout=dropout, num_layers=num_layers, rnn_cell=rnn_cell, bidirectional=bidirectional, attention=attention, delimiters=delimiters, activations=child_activations) decoder = RNNDecoder(tgt_vocab=tgt_vocab, cell_size=cell_size, embed_size=embed_size, dropout=dropout, num_layers=num_layers, rnn_cell=rnn_cell, delimiters=delimiters, beam_size=beam_size, max_len=max_len, activations=child_activations) super(RNN2RNN, self).__init__(encoder, decoder)
def decode(self, tgt_indices, enc_state, extra_inputs=None, monitor=False): if monitor: a = self.activations else: a = neural.Activations() prev_embed = self.dec_embedding(tgt_indices) if len(enc_state) == 1: enc_state = enc_state[0] if extra_inputs: input_embed = th.cat([prev_embed] + extra_inputs, 2) else: input_embed = prev_embed a.dec_out, dec_state = self.decoder(input_embed, enc_state) a.out = self.output(a.dec_out) log_softmax = th.nn.LogSoftmax(dim=2)(a.out) if not isinstance(dec_state, tuple): dec_state = (dec_state, ) return log_softmax, (a.dec_out, dec_state)
def __init__(self, repr_size, activations=None, save_weights=False): super(Attention, self).__init__() self.repr_size = repr_size self.save_weights = save_weights self.monitor_activations = (activations is not None) if isinstance(activations, neural.Activations): self.activations = activations else: self.activations = neural.Activations() self.hidden1 = th.nn.Linear(repr_size, repr_size) self.hidden2 = th.nn.Linear(repr_size, repr_size) self.target = th.nn.Linear(1, repr_size) self.output = th.nn.Linear(repr_size, repr_size) self.current_split = 'default' self.dump_file = None self.close_generator = None
def __init__(self, src_vocab, cell_size, embed_size, dropout, delimiters, rnn_cell='LSTM', num_layers=1, bidirectional='ignored', activations=None): super(ConvEncoder, self).__init__() self.monitor_activations = (activations is not None) if isinstance(activations, neural.Activations): self.activations = activations else: self.activations = neural.Activations() self.enc_embedding = th.nn.Embedding(src_vocab, cell_size) self.conv = th.nn.Conv1d(in_channels=cell_size, out_channels=cell_size, kernel_size=2) self.c_init = th.nn.Linear(1, cell_size * num_layers, bias=False) self.nonlinearity = th.nn.Tanh()