LSTMCell and Teacher Forcing

username: anandaltekar
cooked: <p>Hi,<br>
I’m fairly new to PyTorch and I’m trying to design an 18 node LSTM using LSTMCell with Teacher Forcing. I have quite a few difficulties.</p>
<p>Here’s my model:</p>
<pre><code>class tryLSTM(nn.moduleList):
    def __init__(self, input_size, hidden_size, batch_size):
        super(tryLSTM, self).__init__()

       self.input_size = input_size
       self.hidden_size = hidden_size
       self.batch_size = batch_size

       self.lstm0 = nn.LSTMCell(input_size, hidden_size, bias=True)
       self.lstm1 = nn.LSTMCell(input_size, hidden_size, bias=True)
       self.lstm2 = nn.LSTMCell(input_size, hidden_size, bias=True) 
       .........
       self.lstm17 = nn.LSTMCell(input_size, hidden_size, bias=True)
   def init_hidden(self):
       # initialize the hidden state and the cell state to zeros
       hidden = torch.zeros(self.batch_size, self.hidden_size)
       cell = torch.zeros(self.batch_size, self.hidden_size)
           return hidden, cell
   def forward(self, x, hc):
       out = []
       h_0, c_0 = hc

       h_1, c_1 = self.lstm1(x[0], h_0, c_0)
       out[0] = h_1

       h_2, c_2 = self.lstm2(x[1], h_1, c_1)
       out[1] = h_2
       ......
       h_17, c_17 = self.lstm17(x[16], h_16, c_16)
       out[16] = h_17

model = tryLSTM(input_size=128, hidden_size=128, batch_size=18)

if gpu: model.cuda()

optimizer = optim.Adam(model.parameters(), lr=0.0001)

criterion = nn.BCELoss(weight=None, reduction='mean')
</code></pre>
<p>here’s the training loop:</p>
<pre><code>def train(epoch):
model.train()
# initialize hidden and cell state
hc = model.init_hidden()
for batch_idx, (data, target) in enumerate(train_loader):
    # Zero out the gradients
    optimizer.zero_grad()
    target = data[1:]
    print(target.size())
    # Put data on GPU
    if gpu:
        data = data.cuda()
        target = target.cuda()
    # Get outputs of LSTM
    output = model(data, hc)
    print(output.size)
    # Calculate loss
    loss = criterion(output, target)
    # Calculate gradients
    loss.backward()
    # Update model parameters
    optimizer.step()
    train_loss.append(loss.item())
</code></pre>
<p><strong>Q.1</strong>I’m getting the following error:<br>
TypeError: forward() takes from 2 to 3 positional arguments but 4 were given</p>
<p><strong>Q.2</strong><br>
I’m not sure if this is the correct way to build what I want.<br>
My mini batch is X(18,3,128,128) These are 18 images. What I want to achieve is as follows:<br>
The 1st cells input is x[0] and output h_1 should be similar to x[1].<br>
2nd cells input is x[1] and h_1 and output h_2 should be similar to x[2]<br>
and so on.<br>
I believe the forward pass is run once for each image in the mini batch. So for a mini batch containing 18 images the forward defined above will run 18 times? That is not desired at all. What I want to do is run it once per mini batch but I need to pass in all 18 images as I’m using teacher forcing.<br>
What am I doing wrong? Is there a better way to build this architecture?</p>
<p>Please help, Thanks!</p>

username: anandaltekar
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> <a class="mention" href="/u/smth">@smth</a> <a class="mention" href="/u/alband">@albanD</a> <a class="mention" href="/u/simonw">@SimonW</a> <a class="mention" href="/u/apaszke">@apaszke</a> <a class="mention" href="/u/tom">@tom</a></p>