RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'weight'

Khilan_Pandya: <p>my code is</p>
<pre><code class="lang-auto">classes=["not a face","face"]
path = "F:/project/Database/sample1.jpg"
b=cv2.imread(path)
q=torch.from_numpy(b)
print(q.shape)
d=np.transpose(q.numpy(), (2, 0, 1))
print(d.shape)
print(type(d))
w=torch.from_numpy(d)
w = w.unsqueeze(0)
w= w.double()
print(type(w))
print(w.shape)
print(type(w))

def vis_model(net):
    was_training = net.training
    net.eval()
    with torch.no_grad():
        outp=net(w)
        pred=torch.max(outp,1)
</code></pre>
<p>Basically I have made a model now I want to give a single image as an input to the model and get the prediction…</p>

Kushaj: <p>A fix would be to call <code>.double()</code> for convert to 64bit float.</p>

Khilan_Pandya: <p>I have done that still I am getting that error…</p>

Kushaj: <p>You should do that for your inputs and model also.</p>

imaluengo: <p>Which line triggers that error? It seems the error is in the model itself, which is the only source code not available in the example.</p>
<p>As <a class="mention" href="/u/kushaj">@Kushaj</a> stated, try converting parameters to float or your input image to double (either).</p>
<p>But without reproducible code there is only so much we can do to help…</p>

Khilan_Pandya: <p>Here is the code for model… And it would be really helpful if you give a little information on my error.</p>
<pre><code class="lang-auto">class CNN(nn.Module):
    
    def __init__(self, out_1=13, out_2=32):
        super(CNN, self).__init__()
        self.cnn1 = nn.Conv2d(in_channels=3, out_channels=out_1, kernel_size=3, padding=1)
        self.relu1 = nn.ReLU()
        self.maxpool1 = nn.MaxPool2d(kernel_size=2)
        self.cnn2 = nn.Conv2d(in_channels=out_1, out_channels=out_2, kernel_size=5, stride=1, padding=0)
        self.relu2 = nn.ReLU()
        self.maxpool2 = nn.MaxPool2d(kernel_size=2)
        self.fc1 = nn.Linear(out_2 * 23 * 23, 2)
    
    def forward(self, x):
        print(x.shape)
        out = self.cnn1(x)
        print(out.shape)
        out = self.relu1(out)
        out = self.maxpool1(out)
        out = self.cnn2(out)
        out = self.relu2(out)
        out = self.maxpool2(out)
        #print(out.shape)
        out = out.view(out.size(0), -1)
        out = self.fc1(out)
        return out
    
    def activations(self, x):
        z1 = self.cnn1(x)
        a1 = self.relu1(z1)
        out = self.maxpool1(a1)
        
        z2 = self.cnn2(out)
        a2 = self.relu2(z2)
        out = self.maxpool2(a2)
        out = out.view(out.size(0),-1)
        return z1, a1, z2, a2, out
</code></pre>
<p>If required this is the method by which I have trained the model…</p>
<pre><code class="lang-auto">n_epochs=3
loss_list=[]
accuracy_list=[]
N_test=len(validation_dataset)

def train_model(n_epochs):
    for epoch in range(n_epochs):
        for x, y in train_loader:
            optimizer.zero_grad()
            z = net(x)
            loss = criterion(z, y)
            loss.backward()
            optimizer.step()

        correct=0
        #perform a prediction on the validation  data  
        for x_test, y_test in validation_loader:
            z = net(x_test)
            _, yhat = torch.max(z.data, 1)
            correct += (yhat == y_test).sum().item()
        accuracy = correct / N_test
        accuracy_list.append(accuracy)
        loss_list.append(loss.data)
train_model(n_epochs)
</code></pre>
<p>Additional Information<br>
2 classes each having 624 images and shape 3x100x100</p>

imaluengo: <p>Which line exactly throws the runtimeerror?</p>

Khilan_Pandya: <p>This is the complete error message. When I call the function I get error.</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-13-1ad2836344b5&gt; in &lt;module&gt;()
----&gt; 1 vis_model(net)

&lt;ipython-input-12-d875cfa1fa86&gt; in vis_model(net)
     18     net.eval()
     19     with torch.no_grad():
---&gt; 20         outp=net(w)
     21         pred=torch.max(outp,1)
     22 

C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--&gt; 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

&lt;ipython-input-4-ce890d3bdcf7&gt; in forward(self, x)
     13     def forward(self, x):
     14         print(x.shape)
---&gt; 15         out = self.cnn1(x)
     16         print(out.shape)
     17         out = self.relu1(out)

C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    487             result = self._slow_forward(*input, **kwargs)
    488         else:
--&gt; 489             result = self.forward(*input, **kwargs)
    490         for hook in self._forward_hooks.values():
    491             hook_result = hook(self, input, result)

C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
    318     def forward(self, input):
    319         return F.conv2d(input, self.weight, self.bias, self.stride,
--&gt; 320                         self.padding, self.dilation, self.groups)
    321 
    322 

RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #2 'weight'
</code></pre>

imaluengo: <p>Can you run, before you enter the training loop:</p>
<p><code>net = net.float()</code></p>
<p>It will transform the model parameters to float.</p>
<p>And then in your training loop:</p>
<p><code>z = net(x.float())</code></p>
<p>That should proceed without error.</p>
<p>PS: replace <code>.float()</code> by <code>.double()</code> if you wish to have network + data in double precision format.</p>

Khilan_Pandya: <p>This worked… Thanks</p>

Yummy_Chen: <p>I got the same error:</p>
<blockquote>
<p>RuntimeError: Expected object of scalar type Double but got scalar type Float for argument <span class="hashtag-raw">#2</span> ‘weight’</p>
</blockquote>
<p>so I’m wondering why that error would happended?<br>
and why it still wrong when I have converted the dataset to double?</p>
<p>why I must convert the model to double?</p>

ptrblck: <p>Both, the data and model parameters, should have the same <code>dtype</code>.<br>
If you’ve converted your data to <code>double</code>, you would have to do the same for your model.</p>

HamedH25: <p>Hi I have a same issue, however I couldn’t fix it. would you please support? Error is coming from last line.</p>
<pre><code>np_data = genfromtxt('Top10_data.csv', delimiter=',', dtype='complex', skip_header=0)
inputs_T = np_data[:, 0:20].real
targets_T = np_data[:, 20:22].real

inputs = torch.from_numpy(inputs_T)
targets = torch.from_numpy(targets_T)


train_ds = TensorDataset(inputs, targets)



batch_size = 500
train_dl = DataLoader(train_ds, batch_size, shuffle=True)


class SimpleNet(nn.Module):
   # Initialize the layers
   def __init__(self):
      super().__init__()
      self.linear1 = nn.Linear(20, 20)
      self.act1 = nn.ReLU()  # Activation function
      self.linear2 = nn.Linear(20, 2)

# Perform the computation
def forward(self, x):
    x = self.linear1(x)
    x = self.act1(x)
    x = self.linear2(x)
    return x


model = SimpleNet()

opt = torch.optim.SGD(model.parameters(), 1e-5)

loss_fn = F.mse_loss


def fit(num_epochs, model, loss_fn, opt):
   for epoch in range(num_epochs):
       for xb, yb in train_dl:
          # Generate predictions
          xb = Variable(xb.float(), requires_grad=False)
          yb = Variable(yb.float(), requires_grad=False)
          pred = model(xb)
          loss = loss_fn(pred, yb)
          # Perform gradient descent
          loss.backward()
          opt.step()
          opt.zero_grad()
     print('Training loss: ', loss_fn(model(inputs), targets))


fit(100, model, loss_fn, opt)</code></pre>

ptrblck: <p>numpy uses float64 as their default type, so call <code>float()</code> on these tensors before passing them to the <code>TensorDataset</code>:</p>
<pre><code class="lang-python">inputs = torch.from_numpy(inputs_T),float()
targets = torch.from_numpy(targets_T).float()
</code></pre>
<p>(or cast them using numpy’s <code>astype</code> before).</p>

HamedH25: <p>Thank you so much. It works. Great !</p>

Beyazit_Bestami_Yuks: <aside class="quote no-group" data-username="ptrblck" data-post="14" data-topic="38961">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>torch.from_numpy(inputs_T),float()</p>
</blockquote>
</aside>
<p>That’s worked. Thank you…</p>

mckit: <p>This worked for me as well!! Thanks</p>

111502: <p>Thanks, That’s work… I have the same problem</p>

Chin_GM: <p>Dear all,</p>
<p>I have similar issue and wish to get some help from you guys.<br>
class VehicleDataset(Dataset):</p>
<pre><code>def __init__(self, small_sequences):
    self.sequences = small_sequences

def __len__(self):
    return len(self.sequences)

def __getitem__(self, idx):
    sequence, cluster_label = self.sequences[idx]

    return dict(
        sequence=torch.transpose(torch.Tensor(sequence.to_numpy()), 0, 1),
        cluster_label=torch.tensor(cluster_label).long()
    )
</code></pre>
<p>class VehicleDataModule(pl.LightningDataModule):<br>
def <strong>init</strong>(self, train_sequences, test_sequences, batch_size=8):<br>
super().<strong>init</strong>()<br>
self.train_sequences = train_sequences<br>
self.test_sequences = test_sequences<br>
self.batch_size = batch_size</p>
<pre><code>def setup(self):
    self.train_dataset = VehicleDataset(self.train_sequences)
    self.test_dataset = VehicleDataset(self.test_sequences)

def train_dataloader(self):
    return DataLoader(
        self.train_dataset,
        batch_size=self.batch_size,
        shuffle=True,
        num_workers=2
    )

def val_dataloader(self):
    return DataLoader(
        self.test_dataset,
        batch_size=1,
        shuffle=False,
        num_workers=1
    )

def test_dataloader(self):
    return DataLoader(
        self.test_dataset,
        batch_size=1,
        shuffle=False,
        num_workers=1
    )
</code></pre>
<p>N_EPOCHS = 50<br>
BATCH_SIZE = 64</p>
<p>class TCNModel(nn.Module):<br>
def <strong>init</strong>(self, num_inputs, n_classes, num_channels, kernel_size=3, dropout=0.3):<br>
super(TCNModel, self).<strong>init</strong>()<br>
self.tcn = TemporalConvNet(<br>
num_inputs, num_channels, kernel_size=kernel_size, dropout=dropout<br>
)<br>
self.linear = nn.Linear(num_channels[-1], n_classes)</p>
<pre><code>def forward(self, x):
    y1 = self.tcn(x)
    out = self.linear(y1[:, :, -1])
    return out
</code></pre>
<h1><a name="define-lightning-module-1" class="anchor" href="#define-lightning-module-1"></a>Define Lightning Module</h1>
<p>class Classification(pl.LightningModule):</p>
<pre><code>def __init__(self, num_inputs: int, n_classes: int, num_channels):
    super().__init__()
    self.model = TCNModel(num_inputs, n_classes, num_channels)
    self.criterion = nn.CrossEntropyLoss()

def forward(self, x, cluster_labels=None):
    output = self.model(x)
    loss = 0
    if cluster_labels is not None:
        loss = self.criterion(output, cluster_labels)
    return loss, output

def training_step(self, batch, batch_idx):
    sequences = batch["sequence"]
    cluster_labels = batch["cluster_label"]

    loss, outputs = self(sequences, cluster_labels)
    predictions = torch.argmax(outputs, dim=1)
    step_accuracy = accuracy(predictions, cluster_labels)
    self.log("train_loss", loss, prog_bar=True, logger=True)
    self.log("train_accuracy", step_accuracy, prog_bar=True, logger=True)
    # in training step
    self.logger.experiment.add_scalars("losses", {"train_loss": loss}, global_step=self.current_epoch)
    return {"loss": loss, "accuracy": step_accuracy}

def validation_step(self, batch, batch_idx):
    sequences = batch["sequence"]
    cluster_labels = batch["cluster_label"]

    loss, outputs = self(sequences, cluster_labels)
    predictions = torch.argmax(outputs, dim=1)
    step_accuracy = accuracy(predictions, cluster_labels)
    self.log("val_loss", loss, prog_bar=True, logger=True)
    self.log("val_accuracy", step_accuracy, prog_bar=True, logger=True)
    # in validation step
    self.logger.experiment.add_scalars("losses", {"val_loss": loss}, global_step=self.current_epoch)
    return {"loss": loss, "accuracy": step_accuracy}

def test_step(self, batch, batch_idx):
    sequences = batch["sequence"]
    cluster_labels = batch["cluster_label"]

    loss, outputs = self(sequences, cluster_labels)
    predictions = torch.argmax(outputs, dim=1)
    step_accuracy = accuracy(predictions, cluster_labels)
    self.log("test_loss", loss, prog_bar=True, logger=True)
    self.log("test_accuracy", step_accuracy, prog_bar=True, logger=True)
    return {"loss": loss, "accuracy": step_accuracy}

def configure_optimizers(self):
    return optim.Adam(self.model.parameters(), lr=0.001)
</code></pre>
<p>model = Classification(num_inputs = 3, n_classes = 4, num_channels=[128]*4)</p>
<p>The error happened in step accuracy<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/2/8200884f6d2213a2a234c387a8388fd11ea5f080.png" data-download-href="https://discuss.pytorch.org/uploads/default/8200884f6d2213a2a234c387a8388fd11ea5f080" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/2/8200884f6d2213a2a234c387a8388fd11ea5f080_2_690x147.png" alt="image" data-base62-sha1="iy3d8Kip6Wvzih7JNuKjVMRB0HK" width="690" height="147" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/2/8200884f6d2213a2a234c387a8388fd11ea5f080_2_690x147.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/2/8200884f6d2213a2a234c387a8388fd11ea5f080_2_1035x220.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/2/8200884f6d2213a2a234c387a8388fd11ea5f080.png 2x" data-dominant-color="303337"><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">image</span><span class="informations">1347×288 46.6 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>
<p>This error only happen when I set the default datatype at the beginning of the script.<br>
torch.set_default_dtype(torch.float64)</p>
<p>thanks</p>

ptrblck: <p>The error is raised in the <code>torchmetrics</code> module, which seems to rely on <code>float32</code> being the default type.<br>
If you want to keep setting the default type to <code>float64</code> you might need to explicitly cast the tensors to the expected type before passing them to <code>torchmetrics</code>.</p>