def tester(loc, z, zt, k=3, neval=500, evaldev=True): """ Trainer function for multimodal log-bilinear models loc: location of model to evaluate k: the beam width to use for inference neval: Number of images to evaluate evaldev: True if evaluating on dev set, False for test set """ prog = {} prog['_neval'] = neval prog['_evaldev'] = evaldev print 'Loading model...' net = lm_tools.load_model(loc) print 'Evaluating...' bleu = lm_tools.compute_bleu(net, z['word_dict'], z['index_dict'], zt['IM'], prog, k=k) print 'BLEU score = {}'.format(bleu[-1])
def train(self, X, indX, XY, V, indV, VY, IM, VIM, count_dict, word_dict, embed_map, prog): """ Trains the LBL """ self.start = self.seed self.init_params(embed_map, count_dict) self.step = 0 inds = np.arange(len(X)) numbatches = len(inds) / self.batchsize tic = time.time() bleu = [0.0] * 4 best = 0.0 scores = '/'.join([str(b) for b in bleu]) patience = 10 count = 0 done = False # Main loop lm_tools.display_phase(1) for epoch in range(self.maxepoch): if done: break self.epoch = epoch prng = npr.RandomState(self.seed + epoch + 1) prng.shuffle(inds) for minibatch in range(numbatches): batchX = X[inds[minibatch::numbatches]] batchY = XY[inds[minibatch::numbatches]].toarray() batchindX = indX[inds[minibatch::numbatches]].astype( int).flatten() batchindX = np.floor(batchindX / 5).astype(int) batchIm = IM[batchindX] loss_val = self.compute_obj(self.params, batchX, batchIm, batchY) self.backward(self.params, batchX, batchIm, batchY) self.update_params(batchX) if np.isnan(loss_val): print 'NaNs... breaking out' done = True break # Print out progress if np.mod(minibatch * self.batchsize, prog['_details']) == 0 and minibatch > 0: print "epoch/pts: %04d/%05d, cross-entropy loss: %.2f, time: %.2f" % ( epoch + 1, minibatch * self.batchsize, loss_val, (time.time() - tic) / 60) if np.mod(minibatch * self.batchsize, prog['_samples']) == 0 and minibatch > 0: print "best: %s" % (scores) print '\nSamples:' # lm_tools.generate_and_show(self, word_dict, count_dict, VIM, k=3) VIM_example = VIM[prog['_val_example_idx'], :] VIM_file_list = prog['_val_example_file'] lm_tools.generate_and_show_html(self, word_dict, count_dict, VIM_example, VIM_file_list, show=prog['_show_browser'], k=3) print ' ' if np.mod(minibatch * self.batchsize, prog['_update']) == 0 and minibatch > 0: self.update_hyperparams() self.step += 1 print "learning rate: %.4f, momentum: %.4f" % (self.eta_t, self.p_t) # Compute BLEU if np.mod(minibatch * self.batchsize, prog['_bleu']) == 0 and minibatch > 0: bleu = lm_tools.compute_bleu(self, word_dict, count_dict, VIM, prog, k=3) if bleu[-1] >= best: count = 0 best = bleu[-1] scores = '/'.join([str(b) for b in bleu]) print 'bleu score = {}'.format(bleu[-1]) lm_tools.save_model(self, self.loc) else: count += 1 if count == patience: done = True break self.update_hyperparams() self.step += 1 return best