示例#1
0
def get_rnn_classifer(bptt,
                      max_seq,
                      n_class,
                      n_tok,
                      emb_sz,
                      n_hid,
                      n_layers,
                      pad_token,
                      layers,
                      drops,
                      bidir=False,
                      dropouth=0.3,
                      dropouti=0.5,
                      dropoute=0.1,
                      wdrop=0.5):
    rnn_enc = MultiBatchRNN(bptt,
                            max_seq,
                            n_tok,
                            emb_sz,
                            n_hid,
                            n_layers,
                            pad_token=pad_token,
                            bidir=bidir,
                            dropouth=dropouth,
                            dropouti=dropouti,
                            dropoute=dropoute,
                            wdrop=wdrop)

    s2s_dec = DecoderRNN()

    return SequentialRNN(rnn_enc, s2s_dec)
示例#2
0
        plot_loss_total += loss

        if iter % print_every == 0:
            print_loss_avg = print_loss_total / print_every
            print_loss_total = 0
            print('%s (Batch_no: %d %d%%) %.4f' % (timeSince(
                start, iter / 5), n_iters / 5, iter / 5 * 100, print_loss_avg))
            loss_all += print_loss_avg

            plot_loss_avg = plot_loss_total / plot_every
            plot_loss_total = 0


hidden_size = 2046
encoder1 = EncoderRNN(2046, hidden_size)
attn_decoder1 = DecoderRNN(hidden_size, caption_list.n_words)

if use_cuda:
    encoder1 = encoder1.cuda()
    attn_decoder1 = attn_decoder1.cuda()

# encoder1.load_state_dict(torch.load('encoder.pt'))
# attn_decoder1.load_state_dict(torch.load('decoder.pt'))


def evaluate(encoder, decoder, vid_ID, max_length=MAX_LENGTH):
    input_variable, empty = variableFromId(vid_ID)
    input_length = len(input_variable)
    encoder_hidden = encoder.initHidden()

    # encoder_outputs = Variable(torch.zeros(max_length, encoder.hidden_size))
示例#3
0
from torch import optim
import torch.nn.functional as F
from preprocess import get_dataset
from utils.transformer import *
import argparse
from seq2seq import EncoderRNN, DecoderRNN , Linear

device = 'cpu'
#***********************
#*******IMPORTANT*******
#***********************
#PLEASE NOTE THE VALUE  = 2 or 1 for input_size GIVEN BELOW IS JUST FOR TESTING PURPOSES. PLEASE DO NOT HARDCODE ANY VALUES IN YOUR seq2seq.py file

torch.manual_seed(0)
encoder  = EncoderRNN(hidden_size = 1, input_size = 1, batch_size = 1)
decoder  = DecoderRNN(hidden_size = 1, output_size = 2, batch_size = 1)
dense = Linear(bidirectional  = False, hidden_size_encoder = 1 , hidden_size_decoder = 1)
dense2 = Linear(bidirectional  = False, hidden_size_encoder = 2 , hidden_size_decoder = 1)

layers = [encoder, decoder , dense]

layers_individual = []


for layer in layers: 
	for name,module in layer.named_modules():
		if(name==''):
			continue
		else:
 			layers_individual.append([name,module])
  
示例#4
0
        output_words = evaluate(encoder, decoder, pair[0])
        output_sentence = ' '.join(output_words)
        print('<', output_sentence)
        score, b2, b3, b4 = BLEU_score(pair[1], output_sentence[:-6])
        print(score)
        print(b2)
        print(b3)
        print(b4)
        total += score
        print('')
    print('Avg. score is:')
    print(total / 50)


hidden_size = 2046
encoder1 = EncoderRNN(2046, hidden_size)
attn_decoder1 = DecoderRNN(hidden_size, caption_list.n_words)

if use_cuda:
    encoder1 = encoder1.cuda()
    attn_decoder1 = attn_decoder1.cuda()

encoder1.load_state_dict(torch.load('encoder.pt'))
attn_decoder1.load_state_dict(torch.load('decoder.pt'))

######################################################################
#

evaluateRandomly(encoder1, attn_decoder1)

print('Done')
vocab = build_vocab(cleaned_news, cleaned_summaries, min_freq=3)

# 生成 dataset 是DataTensor 格式
news_dataset = build_dataset(vocab, cleaned_news, config['max_len_news'], type='news')
summaries_dataset = build_dataset(vocab, cleaned_summaries, config['max_len_summaries'], type='summaries')
# 合并在一起
dataset = TensorDataset(news_dataset, summaries_dataset)

# 加载预训练的word2vec模型(使用搜狗新闻训练得到的word2vec),维度是300
pre_embeddings = get_pretrained_embedding(config['pretrained_vector_path'], vocab, vector_dim=300).to(device)

# 构建模型,选择隐状态和词向量维度相同,都是300
vocab_size = len(vocab)
# encoder 使用的是单层双向gru
encoder = EncoderRNN(vocab_size, 300, 300, n_layers=1, pre_embeddings=pre_embeddings)
# decoder 使用双层单项gru
decoder = DecoderRNN(vocab_size, 300, 300, n_layers=2, pre_embeddings=pre_embeddings)

# 迁移到cuda上,training 要用
encoder.to(device)
decoder.to(device)

# 训练模型
training(encoder, decoder, dataset, vocab, config['lr'], config['batch_size'], config['epochs'])






示例#6
0
    def build_model(self):
        class MLP(nn.Module):
            def __init__(self, dims):
                super(MLP, self).__init__()
                self.hidden = nn.ModuleList()
                for k in range(len(dims) - 1):
                    self.hidden.append(nn.Linear(dims[k], dims[k + 1]))

            def forward(self, x):
                for layer in self.hidden[:-1]:
                    x = F.relu(layer(x))
                output = self.hidden[-1](x.float())
                return output

        # A2V
        aud_input_MLP = MLP([self.feat_dim, self.hidden_dim, self.hidden_dim])
        phn_encoder = EncoderRNN(self.hidden_dim,
                                 self.seq_len,
                                 self.hidden_dim,
                                 input_dropout_p=self.dropout_rate,
                                 dropout_p=self.dropout_rate,
                                 n_layers=self.enc_num_layers,
                                 bidirectional=True,
                                 rnn_cell='gru',
                                 variable_lengths=True)
        spk_encoder = EncoderRNN(self.hidden_dim,
                                 self.seq_len,
                                 self.hidden_dim,
                                 input_dropout_p=self.dropout_rate,
                                 dropout_p=self.dropout_rate,
                                 n_layers=self.enc_num_layers,
                                 bidirectional=True,
                                 rnn_cell='gru',
                                 variable_lengths=True)
        aud_decoder = DecoderRNN(self.hidden_dim * 4,
                                 self.seq_len,
                                 self.hidden_dim * 4,
                                 n_layers=self.dec_num_layers,
                                 rnn_cell='gru',
                                 bidirectional=True,
                                 input_dropout_p=self.dropout_rate,
                                 dropout_p=self.dropout_rate)
        aud_output_MLP = MLP(
            [self.hidden_dim * 4, self.hidden_dim, self.feat_dim])

        # a2v = A2VwD(input_MLP, phn_encoder, spk_encoder, decoder, output_MLP, self.dec_num_layers)

        # T2V
        if self.unit_type == 'char':
            txt_feat_dim = 27
        else:
            txt_feat_dim = 60
        txt_input_MLP = MLP([txt_feat_dim, self.hidden_dim])
        txt_encoder = EncoderRNN(self.hidden_dim,
                                 self.seq_len,
                                 self.hidden_dim,
                                 n_layers=1,
                                 bidirectional=True,
                                 rnn_cell='gru',
                                 variable_lengths=True)
        txt_decoder = DecoderRNN(self.hidden_dim * 2,
                                 self.seq_len,
                                 self.hidden_dim * 2,
                                 n_layers=1,
                                 rnn_cell='gru',
                                 bidirectional=True)
        txt_output_MLP = MLP([self.hidden_dim * 2, txt_feat_dim])

        # t2v = A2V(txt_input_MLP, txt_encoder, txt_decoder, txt_output_MLP, 1)

        # size of discriminator input = num_directions * p_hidden_dim
        # discriminator = FCDiscriminator(2*self.hidden_dim, self.hidden_dim, self.D_num_layers)

        # the whole model
        if self.weight_x == 0.:
            if_x = False
        else:
            if_x = True
        self.model = Model(aud_input_MLP, phn_encoder, spk_encoder,
                           aud_decoder, aud_output_MLP, self.dec_num_layers,
                           txt_input_MLP, txt_encoder, txt_decoder,
                           txt_output_MLP, 1, if_x, self.neg_num)

        self.model.to(device)
示例#7
0
bidirectional = config.getboolean("bidirectional")
trainset, source_vocab, target_vocab = get_dataset(
    types="train",
    batch_size=int(config["batch_size"]),
    shuffle=True,
    num_workers=int(config["num_workers"]),
    pin_memory=False,
    drop_last=True)
encoder1 = EncoderRNN(int(config["hidden_size_encoder"]),
                      len(source_vocab) + 2,
                      int(config["batch_size"]),
                      num_layers=int(config["num_layer_encoder"]),
                      bidirectional=bidirectional).to(device)
bridge = Linear(bidirectional, int(config["hidden_size_encoder"]),
                int(config["hidden_size_decoder"])).to(device)
decoder1 = DecoderRNN(int(config["hidden_size_decoder"]),
                      len(target_vocab) + 2,
                      int(config["batch_size"]),
                      num_layers=int(config["num_layer_decoder"])).to(device)
trainIters(trainset,
           encoder1,
           decoder1,
           bridge,
           num_epochs=int(config["num_epoch"]),
           batch_size=int(config["batch_size"]),
           print_every=10,
           device=device)
torch.save(encoder1, "encoder.pt")
torch.save(decoder1, "decoder.pt")
torch.save(bridge, "bridge.pt")
示例#8
0
    seen_aux = 0
    
    for word in sent:
        if seen_aux:
            if word in ["do", "does", "don't", "doesn't"]:
                return word
        else:
            if word in ["do", "does", "don't", "doesn't"]:
                seen_aux = 1



# Where the actual running of the code happens
hidden_size = int(sys.argv[6]) # Default 128
encoder1 = EncoderRNN(input_lang.n_words, hidden_size, recurrent_unit)
decoder1 = DecoderRNN(hidden_size, output_lang.n_words, recurrent_unit, attn=attention, n_layers=1, dropout_p=0.1)

if use_cuda:
    encoder1 = encoder1.cuda()
    decoder1 = decoder1.cuda()


counter = 0
direcs_to_process = 1

lines = open(testFile, encoding='utf-8').read().strip().split('\n')
test_pairs = [[normalizeString(s) for s in l.split('\t')] for l in lines]

length_sorted_pairs_dict = {}
for i in range(30):
        length_sorted_pairs_dict[i] = []