Boolean tensors for input and output of cnn classifier

swapnilw: <h2>
<a name="hello-i-am-relatively-new-to-pytorch-i-want-to-train-a-convolutional-neural-network-regression-model-which-should-have-both-the-input-and-output-as-boolean-tensors-i-followed-the-classifier-example-on-pytorch-tutorials-httpspytorchorgtutorialsbeginnerblitzcifar10_tutorialhtml-in-the-forward-method-in-the-net-class-i-believe-the-input-x-has-to-be-of-type-tensorfloat32-because-when-i-define-x-as-type-tensorfloat32-the-code-runs-fine-but-when-i-change-the-type-of-x-to-tensorbool-which-is-the-correct-type-for-the-data-that-i-have-i-get-an-error-for-the-line-selfconv1-nnconv3d3-6-5-1" class="anchor" href="#hello-i-am-relatively-new-to-pytorch-i-want-to-train-a-convolutional-neural-network-regression-model-which-should-have-both-the-input-and-output-as-boolean-tensors-i-followed-the-classifier-example-on-pytorch-tutorials-httpspytorchorgtutorialsbeginnerblitzcifar10_tutorialhtml-in-the-forward-method-in-the-net-class-i-believe-the-input-x-has-to-be-of-type-tensorfloat32-because-when-i-define-x-as-type-tensorfloat32-the-code-runs-fine-but-when-i-change-the-type-of-x-to-tensorbool-which-is-the-correct-type-for-the-data-that-i-have-i-get-an-error-for-the-line-selfconv1-nnconv3d3-6-5-1"></a>Hello! I am relatively new to PyTorch. I want to train a convolutional neural network regression model, which should have both the input and output as boolean tensors. I followed the classifier example on PyTorch tutorials (<a href="https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">Training a Classifier — PyTorch Tutorials 1.9.0+cu102 documentation</a>). In the “forward” method in the “Net” class, I believe the input “x” has to be of type tensor.float32 because when I define x as type tensor.float32, the code runs fine, but when I change the type of x to tensor.bool (which is the correct type for the data that I have), I get an error for the line “self.conv1 = nn.Conv3d(3, 6, 5)”</h2>
<h2>
<a name="runtimeerror-addmm_impl_cpu_-not-implemented-for-bool-2" class="anchor" href="#runtimeerror-addmm_impl_cpu_-not-implemented-for-bool-2"></a>“RuntimeError: “addmm_impl_cpu_” not implemented for ‘Bool’”</h2>
<p>Can someone please help me with this error? Plus, if I want a boolean tensors as input and output of my network, can I just do it by passing “x” as a boolean tensor in the PyTorch’s classifier example?</p>

ptrblck: <p>You won’t be able to use the <code>bool</code> data type directly, as the parameters are defined as floating point types. Integer types (or <code>bool</code> in this case) could be used via quantization approaches, but I’m unsure if that’s really what you would like to achieve.<br>
Could you describe the use case a bit and in particular, why the <code>bool</code> input and output would be needed or if you could transform the inputs to <code>float32</code> and apply a threshold to get the desired target labels (assuming the <code>bool</code> output represents classes or a similar output)?</p>

swapnilw: <p>Thank for your answer, ptrblck! I am using the CNN for my work in Chemistry, and the use case goes as follows:<br>
For the I/O of the CNN, I use 3D tensors. These 3D tensors are the grid representations of the chemical molecules I want to train my network with. I initiate the 3D tensor with all zero values, and update a grid’s value to 1 if an atom (of the molecule) is present in the grid. The grid resolution is small enough such that there can only be maximum one atom in a particular grid. That is how I end up with a 3D tensor, that has values only 0 (grid in which no atom is present) and 1 (grid where 1 atom is present).</p>
<p>An analogy of this problem can be that, imagine if you have to train a CNN using a collection of black-and-white images. These images have pixel values either 0 or 1.</p>

ptrblck: <p>Thanks for the update. Your use case sounds indeed like using images with black and while pixel values, so you should be able to transform it to <code>float32</code> in the input.<br>
I’m currently still unsure how the output would be treated, i.e. are you using the <code>bool</code> output to classify each pixel location or to restore the original input? In that case also the output could be converted to <code>float32</code>.</p>