Esempio n. 1
0
    def evaluate_model(self, batch_size=1000):
        """
        self: CaptioningSolver object
        Prints unigram BLEU score averaged over 1000 training and val examples.
        """
        BLEUscores = {}
        for split in ['train', 'val']:
            minibatch = sample_coco_minibatch(self.data,
                                              split=split,
                                              batch_size=batch_size)
            gt_captions, features, urls = minibatch
            gt_captions = decode_captions(gt_captions,
                                          self.data['idx_to_word'])

            sample_captions = self.model.sample(features)
            sample_captions = decode_captions(sample_captions,
                                              self.data['idx_to_word'])

            total_score = 0.0
            for gt_caption, sample_caption, url in zip(gt_captions,
                                                       sample_captions, urls):
                total_score += self.BLEU_score(gt_caption, sample_caption)

            BLEUscores[split] = total_score / len(sample_captions)

        for split in BLEUscores:
            print('Average BLEU score for %s: %f' % (split, BLEUscores[split]))
    def _step(self):
        """
        Make a single gradient update. This is called by train() and should not
        be called manually.
        """
        # Make a minibatch of training data
        minibatch = sample_coco_minibatch(self.data,
                      batch_size=self.batch_size,
                      split='train')
        captions, features, urls = minibatch

        # Compute loss and gradient
        loss, grads = self.model.loss(features, captions)
        self.loss_history.append(loss)

        # Perform a parameter update
        for p, w in self.model.params.items():
            dw = grads[p]
            config = self.optim_configs[p]
            next_w, next_config = self.update_rule(w, dw, config)
            self.model.params[p] = next_w
            self.optim_configs[p] = next_config