def __init__(self, env, results_path, tok, episode_len=20): super(Seq2SeqAgent, self).__init__(env, results_path) self.tok = tok self.episode_len = episode_len self.feature_size = self.env.feature_size # Models self.glove_dim = 300 with open('img_features/objects/object_vocab.txt', 'r') as f_ov: self.obj_vocab = [k.strip() for k in f_ov.readlines()] glove_matrix = get_glove_matrix(self.obj_vocab, self.glove_dim) self.objencoder = ObjEncoder(glove_matrix.size(0), glove_matrix.size(1), glove_matrix).cuda() enc_hidden_size = args.rnn_dim//2 if args.bidir else args.rnn_dim self.encoder = model.EncoderLSTM(tok.vocab_size(), args.wemb, enc_hidden_size, padding_idx, args.dropout, bidirectional=args.bidir).cuda() self.decoder = model.AttnDecoderLSTM_Graph(args.aemb, args.rnn_dim, args.dropout, feature_size=self.feature_size + args.angle_feat_size).cuda() self.critic = model.Critic().cuda() self.models = (self.encoder, self.decoder, self.critic) # Optimizers self.encoder_optimizer = args.optimizer(self.encoder.parameters(), lr=args.lr) self.decoder_optimizer = args.optimizer(self.decoder.parameters(), lr=args.lr) self.critic_optimizer = args.optimizer(self.critic.parameters(), lr=args.lr) self.optimizers = (self.encoder_optimizer, self.decoder_optimizer, self.critic_optimizer) # Evaluations self.losses = [] self.criterion = nn.CrossEntropyLoss(ignore_index=args.ignoreid, size_average=False) # Logs sys.stdout.flush() self.logs = defaultdict(list)
def __init__(self, env, results_path, tok, episode_len=20): super(Seq2SeqAgent, self).__init__(env, results_path) self.tok = tok self.episode_len = episode_len self.feature_size = self.env.feature_size # Models enc_hidden_size = args.rnn_dim//2 if args.bidir else args.rnn_dim self.encoder = model.EncoderLSTM(tok.vocab_size(), args.wemb, enc_hidden_size, padding_idx, args.dropout, bidirectional=args.bidir).cuda() self.decoder = model.AttnDecoderLSTM(args.aemb, args.rnn_dim, args.dropout, feature_size=self.feature_size + args.angle_feat_size).cuda() self.critic = model.Critic().cuda() self.models = (self.encoder, self.decoder, self.critic) # Optimizers self.encoder_optimizer = args.optimizer(self.encoder.parameters(), lr=args.lr) self.decoder_optimizer = args.optimizer(self.decoder.parameters(), lr=args.lr) self.critic_optimizer = args.optimizer(self.critic.parameters(), lr=args.lr) self.optimizers = (self.encoder_optimizer, self.decoder_optimizer, self.critic_optimizer) # Evaluations self.losses = [] self.criterion = nn.CrossEntropyLoss(ignore_index=args.ignoreid, size_average=False) # Logs sys.stdout.flush() self.logs = defaultdict(list)
def __init__(self, env, results_path, tok, episode_len=20): super(Seq2PolicyAgent, self).__init__(env, results_path) self._iter = 0 self.tok = tok self.episode_len = episode_len self.feature_size = self.env.feature_size # Models enc_hidden_size = args.rnn_dim//2 if args.bidir else args.rnn_dim self.encoder = model.EncoderLSTM(tok.vocab_size(), args.wemb, enc_hidden_size, padding_idx, args.dropout, bidirectional=args.bidir).cuda() if args.original_decoder: self.decoder = model.AttnDecoderLSTM(args.aemb, args.rnn_dim, args.dropout, feature_size=self.feature_size + args.angle_feat_size).cuda() else: self.decoder = model.AttnPolicyLSTM(args.aemb, args.rnn_dim, args.dropout, feature_size=self.feature_size + args.angle_feat_size, latent_dim=args.vae_latent_dim).cuda() if args.fix_vae: print("fix the parameters in sub policy") for param in self.decoder.policy.parameters(): param.requires_grad = False self.critic = model.Critic().cuda() self.models = (self.encoder, self.decoder, self.critic) # Optimizers self.encoder_optimizer = args.optimizer(self.encoder.parameters(), lr=args.lr) self.decoder_optimizer = args.optimizer(self.decoder.parameters(), lr=args.lr) self.critic_optimizer = args.optimizer(self.critic.parameters(), lr=args.lr) self.optimizers = (self.encoder_optimizer, self.decoder_optimizer, self.critic_optimizer) # Evaluations self.losses = [] self.criterion = nn.CrossEntropyLoss(ignore_index=args.ignoreid, size_average=False) # Logs sys.stdout.flush() self.logs = defaultdict(list)
n_iters = 100000 MAX_LENGTH = 30 n_evaluations = 10 teacher_forcing_rate = 0.5 beam_size = 3 # data preparing lang, pairs = d.read_data() pairs = d.filterPairs(pairs, MAX_LENGTH) print("after filter, the number of pairs is %s." % len(pairs)) training_pairs = [ d.tensorsFromPair(lang, random.choice(pairs)) for i in range(n_iters) ] # build model encoder = model.EncoderLSTM(lang.n_words, hidden_size).to(model.device) # decoder = model.DecoderLSTM(lang.n_words, hidden_size).to(model.device) decoder = model.DecoderAttenLSTM( lang.n_words, hidden_size, max_length=MAX_LENGTH, dropout_p=0.1, ).to(model.device) start = time.time() plot_losses = [] print_loss_total = 0 # reset every print_every plot_loss_total = 0 # reset every plot_every encoder_optimizer = optim.SGD(encoder.parameters(), lr=learning_rate) decoder_optimizer = optim.SGD(decoder.parameters(), lr=learning_rate)