Gradient computation with index_select (for recursive neural networks)
Hi all, My cell looks something like this: <SCODE>import torch
import torch.nn.functional as F
import torch.nn as nn

class ReNNCell(nn.Module):
def __init__(self, dim):
    super(ReNNCell, self).__init__()
    self.dim = dim
    self.W = nn.Linear(dim*2, dim)
    self.W_score = nn.Linear(dim, 1)

def forward(self, inputs):
    assert(inputs.size()[0] == 1), 'we expect batch size = 1'
    rep = F.relu(self.W(inputs))
    score = F.relu(self.W_score(rep))
    return rep, score
<ECODE> <SCODE>tensors = [Variable(torch.randn(1,3)), Variable(torch.randn(1,3)), Variable(torch.randn(1,3)), Variable(torch.randn(1,3))]

cats = []
for i in range(1,4,2):
    cats.append(torch.cat([tensors[i-1], tensors[i]],1))

cell = ReNNCell(3,2)
outputs = [cell(c) for c in cats]
reps = [o[0] for o in outputs]
scores = [o[1] for o in outputs]
scores = torch.cat(scores, 1)
reps = torch.cat(reps, 0)
max_score, max_index = torch.max(scores, 1)
max_index = torch.squeeze(max_index, 1)
max_score = torch.squeeze(max_score, 1)
<ECODE> Based on this selection I will compute some dummy loss. <SCODE>crit = torch.nn.MSELoss()
loss = crit(reps.index_select(0,max_index), Variable(torch.ones(1,3)))
<ECODE> <SCODE>RuntimeError: could not compute gradients for some functions (Threshold, Threshold)
<ECODE> Could anybody point me in the right direction? Is this a bug or do I perhaps have to somehow  compute the gradients myself?
elPistolero
Hi,
albanD
Yes it does! So index_select somehow discards the Variable state (or is it squeeze)?
elPistolero
albanD
elPistolero