TypeError: Cannot convert torch.dtype to numpy.dtype

Dka1: <p>I am trying to run my model on the GPU.<br>
Here is the what I have so far:</p>
<pre><code class="lang-auto">from torch.utils.data import DataLoader
import torch.optim as optim
import numpy as np

from torch import nn
import torch
from packages.ann import ANN
from packages.cnn import CNN


class MODEL(nn.Module):
    def __init__(self, input_dim, input_length, output_dim, kernel_size=16):
        super(self, MODEL).__init__()
        self.ann = ANN(input_dim, output_dim ,kernel_size, dropout = 0.2)
        self.cnn = CNN(input_dim, output_dim ,kernel_size, dropout = 0.2)
    
    def forward(self, x):
        ann_output = self.ann(x)[:, -1]
        cnn_output = self.cnn(x)[:, -1]
        return torch.cat((ann_output, cnn_output), dim=1)
    

# set env variable for gpus
os.environ["CUDA_VISIBLE_DEVICES"]="0"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    

# Call the model
model = MODEL(input_dim, input_length, output_dim, kernel_size)
model = model.to(device, dtype=torch.float)

optimizer = optim.Adam(model.parameters(), lr=0.005, weight_decay=0.85)

triplet_loss = nn.TripletMarginLoss(margin=1)
model.train()


#`train_data` are of type numpy

loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
epochs = 10

for epoch in range():
    running_loss = []
    for step, (anchor, positive, negative) in enumerate(loader):
        
        anchor = anchor.to(device)
        positive = positive.to(device)
        negative = negative.to(device)
        
        print(type(anchor)) # &lt;class 'torch.Tensor'&gt;
        
        optimizer.zero_grad()
        anchor_output = model(anchor) # The error occurs here. 
        positive_output = model(positive)
        negative_output = model(negative)

        loss = triplet_loss(anchor_output, positive_output, negative_output)
        loss.backward()
        optimizer.step()

        running_loss.append(loss.cpu().detach().numpy())
</code></pre>
<p>I get the following error in the second for loop, on line <code>anchor_output = model(anchor)</code></p>
<pre><code class="lang-auto">TypeError: Cannot convert torch.dtype to numpy.dtype
</code></pre>

AlphaBetaGamma96: <p>Could you share what <code>ANN</code> and <code>CNN</code> are ? Because it seems the error occurs within those objects.</p>
<p>Also, you’ve defined both <code>ANN</code> and <code>CNN</code> to <code>self.ann</code>, so you’re not using <code>ANN</code> at all which might be the error?</p>
<aside class="quote no-group quote-modified" data-username="Dka1" data-post="1" data-topic="153488">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/77aa72/48.png" class="avatar"> Dka1:</div>
<blockquote>
<pre><code class="lang-auto">        self.ann = ANN(input_dim, output_dim ,kernel_size, dropout = 0.2)
        self.ann = CNN(input_dim, output_dim ,kernel_size, dropout = 0.2)
</code></pre>
</blockquote>
</aside>
<p>Secondly, you’ve also defined both <code>ann_output</code> and <code>cnn_output</code> to <code>self.ann(x)[:,-1]</code> so you’re not using <code>self.cnn</code></p>
<aside class="quote no-group quote-modified" data-username="Dka1" data-post="1" data-topic="153488">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/77aa72/48.png" class="avatar"> Dka1:</div>
<blockquote>
<pre><code class="lang-auto">        ann_output = self.ann(x)[:, -1]
        cnn_output = self.ann(x)[:, -1]
</code></pre>
</blockquote>
</aside>
<p>Could these be the source of the problem, perhaps?</p>

Dka1: <p><a class="mention" href="/u/alphabetagamma96">@AlphaBetaGamma96</a>  thank you for your reply. I have updated the class names accordingly.</p>

AlphaBetaGamma96: <p>Can you share what <code>ANN</code> and <code>CNN</code> are? Because the error seems to emerge from them.</p>