Example #1
0
def action_features():
    actions = []
    for a in range(3):
        a = one_hot(a)
        actions.append(a)
    actions = np.stack(actions)
    return actions
    def find_closest_word(self, word):

        word = torch.Tensor([word])

        print(word.size)

        # Embed unknown word
        embedding = self.enc_lin(
                self.enc_rnn(
                    self.emb_layer(utils.one_hot(word, self.vocab_sz)))[1])
        
        print("Encoded unknown word {} to an embedding of size {}".format(word, embedding.shape))

        known_index = (self.tree.query(wordembedding)[1])

        print("Unknown word now converted to index {}, which corresponds to word {}".format(known_index, self.vocab[known_index]))
    def forward(self, phrase, sim_phrase=None, train=False):
        """
        forward pass

        inputs :-

        phrase : given phrase , shape = (max sequence length, batch size)
        sim_phrase : (if train == True), shape = (max seq length, batch sz)
        train : if true teacher forcing is used to train the module

        outputs :-

        out : generated paraphrase, shape = (max sequence length, batch size, )
        enc_out : encoded generated paraphrase, shape=(batch size, enc_dim)
        enc_sim_phrase : encoded sim_phrase, shape=(batch size, enc_dim)

        """

        if sim_phrase is None:
            sim_phrase = phrase

        if train:

            # encode input phrase
            enc_phrase = self.enc_lin(
                self.enc_rnn(
                    self.emb_layer(utils.one_hot(phrase, self.vocab_sz)))[1])
            
            # generate similar phrase using teacher forcing
            emb_sim_phrase_gen = self.gen_emb(sim_phrase)
            out_rnn, _ = self.gen_rnn(
                torch.cat([enc_phrase, emb_sim_phrase_gen[:-1, :]], dim=0))
            out = self.gen_lin(out_rnn)

            # propagated from shared discriminator to calculate
            # pair-wise discriminator loss
            enc_sim_phrase = self.dis_lin(
                self.dis_rnn(
                    self.dis_emb_layer(utils.one_hot(sim_phrase,
                                                     self.vocab_sz)))[1])
            enc_out = self.dis_lin(
                self.dis_rnn(self.dis_emb_layer(torch.exp(out)))[1])

        else:

            # encode input phrase
            enc_phrase = self.enc_lin(
                self.enc_rnn(
                    self.emb_layer(utils.one_hot(phrase, self.vocab_sz)))[1])
            
            # generate similar phrase using teacher forcing
            words = []
            h = None
            for __ in range(self.max_seq_len):
                word, h = self.gen_rnn(enc_phrase, hx=h)
                word = self.gen_lin(word)
                words.append(word)
                word = torch.multinomial(torch.exp(word[0]), 1)
                word = word.t()
                enc_phrase = self.gen_emb(word)
            out = torch.cat(words, dim=0)

            # propagated from shared discriminator to calculate
            # pair-wise discriminator loss
            enc_sim_phrase = self.dis_lin(
                self.dis_rnn(
                    self.dis_emb_layer(utils.one_hot(sim_phrase,
                                                     self.vocab_sz)))[1])
            enc_out = self.dis_lin(
                self.dis_rnn(self.dis_emb_layer(torch.exp(out)))[1])

        enc_out.squeeze_(0)
        enc_sim_phrase.squeeze_(0)
        return out, enc_out, enc_sim_phrase
Example #4
0
def action_encoding(max_order_size):
    actions = []
    for a in list(range(max_order_size)):
        actions.append(one_hot(a, c=max_order_size))
    actions = np.stack(actions)
    return actions
Example #5
0
 def observation(self, observation):
     return one_hot(observation, self.env.unwrapped.max_stock)