bytedeco/javacpp-presets

[PyTorch] How to check if a tensor is None

haifengl opened this issue · 5 comments

For example, I would like to check if a module's bias is present. module.bias().isNull() returns false for a None tensor though. Thanks.

You can use numel.

Thanks! The hack works. But I get the below exception when calling torch.zero_(bias) when the bias is NOT None. But a similar call works with the weight of same module.

                    torch.ones_(batchNorm2d.weight());
                    // RuntimeException: a leaf Variable that requires grad is being used in an in-place operation.
                    torch.zero_(batchNorm2d.bias());

I understand the exception from C++ and the exception is self-explaining. But wonder if anyone knows a work around. Thanks!

Try this:
batchNorm2d.bias().detach().zero_()

BTW, a tensor can be undefined too. I guess the equivalent of None in the C++ api is an undefined tensor, so instead of testing if numel is 0 you may have to test for defined().

Thanks a lot! Both defined() and detached() work well. I tried to find some method like undefined() :)