Answers.Body: <p>No,
  you misunderstood the error.
  The</p>
  <pre><code>np.random.random(100)
  </code></pre>
  <p>generates 100 random number of type float64 (also double in your case).
  Change the type to float32 by adding the following line:</p>
  <pre><code>t = torch.tensor(np.random.random(100))
  t = t.astype(np.float32)
  ...
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-03T09:40:38.993Z
Answers.Text: Ok, see. your solution was in the right direction but in my version of Pytorch was not correct @Dave. I had to change t = torch.tensor(np.random.random(100)) to t = torch.tensor(np.random.random(100)).float() and now the types matches.
Answers.UserDisplayName: isg75
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-03T14:33:31.290Z
Answers.Text: Nice, that it worked. Then do not forget to leave a like at least!
Answers.UserDisplayName: Dave
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-31T20:14:34.500Z
Answers.LastActivityDate: 2024-05-31T20:14:34.500Z
Answers.UserDisplayName: Dave
Body: <p>I have a problem understanding how Pytorch and Pytorch-Lightning work. I'm trying to build a toy Autoencoder and it giving me the typical mat1 and mat2 error. My code is:</p>
  <pre><code>import torch
  from torch import Tensor
  from torch import nn
  import torch.nn.functional as F
  from torch.optim import AdamW
  from pytorch_lightning import LightningModule, Trainer
  import numpy as np
  
  class Encoder(nn.Module):
      def __init__(self):
          super().__init__()
          self.l1 = nn.Sequential(nn.Linear(100, 64), nn.ReLU(), nn.Linear(64, 32))
  
      def forward(self, x):
          return self.l1(x)
  
  class Decoder(nn.Module):
      def __init__(self):
          super().__init__()
          self.l1 = nn.Sequential(nn.Linear(32, 64), nn.ReLU(), nn.Linear(64, 100))
  
      def forward(self, x):
          return self.l1(x)
  
  
  encoder = Encoder()
  decoder = Decoder()
  
  t = torch.tensor(np.random.random(100))
  encoder.forward(t)
  </code></pre>
  <p>When I run the code I get the following error:</p>
  <pre><code>---------------------------------------------------------------------------
  RuntimeError                              Traceback (most recent call last)
  &lt;ipython-input-57-53b4b6e59a61&gt; in &lt;cell line: 6&gt;()
        4 t = torch.tensor(np.random.random(100))
        5 #t_new = t.reshape(-1,1)
  ----&gt; 6 encoder.forward(t)
        7 #x.view(x.size(0), -1)
  
  6 frames
  /usr/local/lib/python3.10/dist-packages/torch/nn/modules/linear.py in forward(self, input)
      114 
      115     def forward(self, input: Tensor) -&gt; Tensor:
  --&gt; 116         return F.linear(input, self.weight, self.bias)
      117 
      118     def extra_repr(self) -&gt; str:
  
  RuntimeError: mat1 and mat2 must have the same dtype, but got Double and Float
  </code></pre>
  <p>which I can't understand as I haven't set the datatypes. I expect to get a tensor full of 32 zeros (because I haven't put the values of my weights and biases).</p>
  <p>Thank you!</p>
  
Tags: <pytorch>
Title: Pytorch Encoder not working
UserDisplayName: isg75
ThreadText: # Pytorch Encoder not working
  
  (Asked by: username_0 on 2024-05-30)
  
  I have a problem understanding how Pytorch and Pytorch-Lightning work. I'm trying to build a toy Autoencoder and it giving me the typical mat1 and mat2 error. My code is:
  import torch
  from torch import Tensor
  from torch import nn
  import torch.nn.functional as F
  from torch.optim import AdamW
  from pytorch_lightning import LightningModule, Trainer
  import numpy as np
  
  class Encoder(nn.Module):
      def __init__(self):
          super().__init__()
          self.l1 = nn.Sequential(nn.Linear(100, 64), nn.ReLU(), nn.Linear(64, 32))
  
      def forward(self, x):
          return self.l1(x)
  
  class Decoder(nn.Module):
      def __init__(self):
          super().__init__()
          self.l1 = nn.Sequential(nn.Linear(32, 64), nn.ReLU(), nn.Linear(64, 100))
  
      def forward(self, x):
          return self.l1(x)
  
  
  encoder = Encoder()
  decoder = Decoder()
  
  t = torch.tensor(np.random.random(100))
  encoder.forward(t)
  
  When I run the code I get the following error:
  ---------------------------------------------------------------------------
  RuntimeError                              Traceback (most recent call last)
   in ()
        4 t = torch.tensor(np.random.random(100))
        5 #t_new = t.reshape(-1,1)
  ----> 6 encoder.forward(t)
        7 #x.view(x.size(0), -1)
  
  6 frames
  /usr/local/lib/python3.10/dist-packages/torch/nn/modules/linear.py in forward(self, input)
      114 
      115     def forward(self, input: Tensor) -> Tensor:
  --> 116         return F.linear(input, self.weight, self.bias)
      117 
      118     def extra_repr(self) -> str:
  
  RuntimeError: mat1 and mat2 must have the same dtype, but got Double and Float
  
  which I can't understand as I haven't set the datatypes. I expect to get a tensor full of 32 zeros (because I haven't put the values of my weights and biases).
  Thank you!
  
  **Tags:** pytorch
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-05-31. Score: 0 | Accepted answer
  
  No,
  you misunderstood the error.
  The
  np.random.random(100)
  
  generates 100 random number of type float64 (also double in your case).
  Change the type to float32 by adding the following line:
  t = torch.tensor(np.random.random(100))
  t = t.astype(np.float32)
  ...
  
  > **Comments:**
  > **username_0**: Ok, see. your solution was in the right direction but in my version of Pytorch was not correct @Dave. I had to change t = torch.tensor(np.random.random(100)) to t = torch.tensor(np.random.random(100)).float() and now the types matches.  
  > **username_1**: Nice, that it worked. Then do not forget to leave a like at least!  
  
  ---
  
  