Branching in nets


I would like to train a combination of nets, namely apply different nets to the input depending on the classification given by the classifier.
For example the forward could look like (simplified for clarity sake)

def forward(self,x):
  label = classifier(x)
  if label==1:
    x = othernet(x)
  return x,label

however I get the following error “RuntimeError: bool value of Variable objects containing non-empty torch.cuda.ByteTensor is ambiguous”

From what I understand, label is a batchsize x 1 tensor so that the “if” statement is ambiguous. How can I make it apply “othernet” to only the samples that have label 1? (and still have autograd work, of course)

A solution would be to use select_index, but is there something simpler?

Thanks for your help

if[0] == 1

Thanks for the quick reply, however I get a similar error:
"RuntimeError: bool value of non-empty torch.cuda.ByteTensor objects is ambiguous"
Any other idea?

your label's shape must be 2D. In this case you will have to do a for-loop. But do you understand what I suggested (instead of just trying out the code as-is)?

I precisely wanted to avoid a for loop, hoping for a matlab style shortcut.
anyway problem solved.