Very simple but very strange type error

username: Core_Park
cooked: <p>I have encountered a very strange error with the datatypes. I am very new to PyTorch, I apologize if this is a simple mistake.</p>
<p>The minimal version of the error is:</p>
<p>import torch as torch<br>
class Net(torch.nn.Module):<br>
def <strong>init</strong>(self):<br>
super(Net, self).<strong>init</strong>()<br>
self.conv1 = torch.nn.Conv2d(in_channels=1,out_channels=16,kernel_size=5)<br>
self.pool = torch.nn.MaxPool2d(2, 2)<br>
self.conv2 = torch.nn.Conv2d(in_channels=16,out_channels=16,kernel_size=5)<br>
self.dense1 = torch.nn.Linear(16 * 145 * 95, 512)<br>
self.dense2 = torch.nn.Linear(512, 168)</p>
<pre><code>def forward(self, x):
    x = self.pool(torch.nn.functional.relu(self.conv1(x)))
    x = self.pool(torch.nn.functional.relu(self.conv2(x)))
    x = x.view(-1, 16 * 145 * 95)
    x = torch.nn.functional.relu(self.fc1(x))
    x = self.fc2(x)
    x = x.view(-1,2)
    return x
</code></pre>
<p>CNN=Net()<br>
c=torch.zeros((2,1,190,290),dtype=torch.double)<br>
CNN(c)</p>
<p>I have tried all different versions of: adding .float() to the network, adding .double() to the inputs, etc. They all didn’t change the error message.<br>
The full error message is displayed below.</p>
<p>Thank you so much in advance.</p>
<p>-Core Park</p>
<h1><a name="hyperparameters-batch_size-32-epochs-5-learning_rate-0001-1" class="anchor" href="#hyperparameters-batch_size-32-epochs-5-learning_rate-0001-1"></a>===== HYPERPARAMETERS =====<br>
batch_size= 32<br>
epochs= 5<br>
learning_rate= 0.001</h1>
<h2><a name="torchdoubletensor-2" class="anchor" href="#torchdoubletensor-2"></a>torch.DoubleTensor</h2>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
1 CNN = Net()<br>
----&gt; 2 trainNet(CNN, batch_size=32, n_epochs=5, learning_rate=0.001)</p>
<p> in trainNet(net, batch_size, n_epochs, learning_rate)<br>
38             <span class="hashtag-raw">#Forward</span> pass, backward pass, optimize<br>
39             print(inputs.type())<br>
—&gt; 40             outputs = net(inputs)<br>
41             loss_size = loss(outputs, labels)<br>
42             loss_size.backward()</p>
<p>/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
 → 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p> in forward(self, x)<br>
9<br>
10     def forward(self, x):<br>
—&gt; 11         x = self.pool(torch.nn.functional.relu(self.conv1(x)))<br>
12         x = self.pool(torch.nn.functional.relu(self.conv2(x)))<br>
13         x = x.view(-1, 16 * 145 * 95)</p>
<p>/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
 → 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p>/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/conv.py in forward(self, input)<br>
343<br>
344     def forward(self, input):<br>
 → 345         return self.conv2d_forward(input, self.weight)<br>
346<br>
347 class Conv3d(_ConvNd):</p>
<p>/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/conv.py in conv2d_forward(self, input, weight)<br>
340                             _pair(0), self.dilation, self.groups)<br>
341         return F.conv2d(input, weight, self.bias, self.stride,<br>
 → 342                         self.padding, self.dilation, self.groups)<br>
343<br>
344     def forward(self, input):</p>
<p>RuntimeError: Expected object of scalar type Double but got scalar type Float for argument <span class="hashtag-raw">#3</span> ‘mat1’ in call to <em>th_addmm</em></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/f/df7126d278d0c516a0931d1ca8379b21bb3f5a28.png" data-download-href="https://discuss.pytorch.org/uploads/default/df7126d278d0c516a0931d1ca8379b21bb3f5a28" title="err"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/f/df7126d278d0c516a0931d1ca8379b21bb3f5a28_2_451x500.png" alt="err" data-base62-sha1="vSETW3yKXoNJ3kdP85H4zi09qeA" width="451" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/f/df7126d278d0c516a0931d1ca8379b21bb3f5a28_2_451x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/f/df7126d278d0c516a0931d1ca8379b21bb3f5a28_2_676x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/f/df7126d278d0c516a0931d1ca8379b21bb3f5a28.png 2x" data-dominant-color="EEEFEF"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">err</span><span class="informations">729×808 112 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Core_Park
cooked: <p>I think this has nothing to do with the input data, the data types of the tensors in the network don;t match each other I guess?</p>

username: ptrblck
cooked: <p>PyTorch uses <code>float32</code> by default, while you are creating an input tensor with <code>float64</code> (<code>double</code>).<br>
If you don’t specify the <code>dtype</code> or set it to <code>torch.float</code>, the code should work.</p>

username: Core_Park
cooked: <p>Thank you very much, I indeed realize that.</p>
<p>Thank you.</p>