How to use condition flow?
t3476
You probably get this error when you try to use the ByteTensor as a boolean: RuntimeError: bool value of non-empty torch.ByteTensor objects is ambiguous
alexis-jacq
Does that numpy bool create proper computing graph? If so, why that’s the case? I’m not sure why pytorch can create computing graph with mixing numpy arrays and pytorch variables.
t3476
Can you elaborate a bit more on what exactly you are trying to do?
smth
<SCODE>def forward(self, x):
    x = self.module1(x)
    if (x.data > 0).all():
        return self.module2(x)
    else:
        return self.module3(x)
<ECODE>
apaszke
Problem sovled. Thanks for help.
t3476
Hi, does the condition work on a sample by sample basis? or does the condition apply across all the samples in batch? I am confused about this. Can you please explain it?
rahul
I don’t think it works on a per sample basis. Can someone please provide a proper equivalent to tf.cond().
madmaxiwnl
ptrblck
Well, it’s something like this: <SCODE>out_tensor1, out_tensor2, out_tensor3 = tf.cond(some_condition,
                         lambda: some_tensor1, some_tensor2, some_tensor3,
                         lambda: some_tensor4, some_tensor5, some_tensor6)
<ECODE> Currently, I am using a workaround I found on another thread: <SCODE>def where(cond, f1, f2):
    return c * f1() + (1-c) * f2()
<ECODE> and finding all 3 out_tensors seperately. Though having an inbuilt operation in pytorch would be useful.
madmaxiwnl
I’m not sure, how the TF code works exactly, but if it’s just an assignment, wouldn’t this work: <SCODE>if some_condition:
    out_tensor1, out_tensor2, out_tensor3 = some_tensor1, some_tensor2, some_tensor3
else:
    out_tensor1, out_tensor2, out_tensor3 = some_tensor4, some_tensor5, some_tensor6
<ECODE>
ptrblck
<SCODE>some_condition = some_condition.type(torch.LongTensor)
out_tensor1 = some_condition * some_tensor1 + (1-some_condition) * some_tensor2
<ECODE> and, similarly for out_tensor 2 and 3. I couldn’t find a function which would perform the operation for all 3 out_tensors at once. Anyways it’s working now, thanks.
madmaxiwnl
Use
abediee
Could the tensor.unbind() function be used on .data, to work per sample basis?
wiiiktor