You can find the answer to this question by reading any work that requires teacher forcing on github. It may not be the most efficient solution but it works.

  
      github.com
  
  
    poojahira/image-captioning-with-attention/blob/master/models.py
import torch
from torch import nn
import torchvision

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


class Encoder(nn.Module):
    """
    Encoder.
    """

    def __init__(self, encoded_image_size=14):
        super(Encoder, self).__init__()
        self.enc_image_size = encoded_image_size

        resnet = torchvision.models.resnet101(pretrained=True)  # pretrained ImageNet ResNet-101

        # Remove linear and pool layers (since we're not doing classification)
        modules = list(resnet.children())[:-2]


  This file has been truncated. show original

  
  
    
    
  
  


Here is one example, just read the forward function for DecoderWithAttention class.