def forward(self, img_features, captions):
        """
        We can use teacher forcing during training. For reference, refer to
        https://www.deeplearningbook.org/contents/rnn.html
        """
        batch_size = img_features.size(0)

        h, c = self.get_init_lstm_state(img_features)
        # max_timespan = max([len(caption) for caption in captions]) - 1
        max_timespan = captions.size(1) - 1

        prev_words = torch.ones(batch_size, 1).long()
        if torch.cuda.is_available():
            prev_words = prev_words.cuda()

        if self.use_tf:
            embedding = self.embedding(captions) if self.training else self.embedding(prev_words)
        else:
            embedding = self.embedding(prev_words)

        # preds = torch.zeros(batch_size, max_timespan, self.vocab_size)
        # alphas = torch.zeros(batch_size, max_timespan, img_features.size(1))
        # if torch.cuda.is_available():
        #     preds = preds.cuda()
        #     alphas = alphas.cuda()

        preds = []
        alphas = []

        for t in range(max_timespan):
            context, alpha = self.attention(img_features, h)
            gate = self.sigmoid(self.f_beta(h))
            gated_context = gate * context

            if self.use_tf and self.training:
                lstm_input = torch.cat((embedding[:, t], gated_context), dim=1)
            else:
                embedding = embedding.squeeze(1) if embedding.dim() == 3 else embedding
                lstm_input = torch.cat((embedding, gated_context), dim=1)

            h, c = self.lstm(lstm_input, (h, c))
            output = self.deep_output(self.dropout(h))

            # preds[:, t] = output
            # alphas[:, t] = alpha

            preds.append(output.unsqueeze(1))
            alphas.append(alpha.unsqueeze(1))

            if not self.training or not self.use_tf:
                embedding = self.embedding(output.max(1)[1].reshape(batch_size, 1))

        preds = torch.cat(preds, 1)
        alphas = torch.cat(alphas, 1)

        return preds, alphas

        We can use teacher forcing during training. For reference, refer to
        https://www.deeplearningbook.org/contents/rnn.html
        
torchmm/models/captioning/nic.py