Error while running Encoder – “TypeError: conv2d() received an invalid combination of arguments”

qaqbanxian: <p>import torch<br>
from torch import nn</p>
<p>class MseDirectionLoss(nn.Module):<br>
def <strong>init</strong>(self, lamda):<br>
super(MseDirectionLoss, self).<strong>init</strong>()<br>
self.lamda = lamda<br>
self.criterion = nn.MSELoss()<br>
self.similarity_loss = torch.nn.CosineSimilarity()</p>
<pre><code>def forward(self, output_pred, output_real):
    y_pred_0, y_pred_1, y_pred_2, y_pred_3 = output_pred[3], output_pred[6], output_pred[9], output_pred[12]
    y_0, y_1, y_2, y_3 = output_real[3], output_real[6], output_real[9], output_real[12]

    # different terms of loss
    abs_loss_0 = self.criterion(y_pred_0, y_0)
    loss_0 = torch.mean(1 - self.similarity_loss(y_pred_0.view(y_pred_0.shape[0], -1), y_0.view(y_0.shape[0], -1)))
    abs_loss_1 = self.criterion(y_pred_1, y_1)
    loss_1 = torch.mean(1 - self.similarity_loss(y_pred_1.view(y_pred_1.shape[0], -1), y_1.view(y_1.shape[0], -1)))
    abs_loss_2 = self.criterion(y_pred_2, y_2)
    loss_2 = torch.mean(1 - self.similarity_loss(y_pred_2.view(y_pred_2.shape[0], -1), y_2.view(y_2.shape[0], -1)))
    abs_loss_3 = self.criterion(y_pred_3, y_3)
    loss_3 = torch.mean(1 - self.similarity_loss(y_pred_3.view(y_pred_3.shape[0], -1), y_3.view(y_3.shape[0], -1)))

    total_loss = loss_0 + loss_1 + loss_2 + loss_3 + self.lamda * (
            abs_loss_0 + abs_loss_1 + abs_loss_2 + abs_loss_3)

    return total_loss
</code></pre>
<p>class DirectionOnlyLoss(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(DirectionOnlyLoss, self).<strong>init</strong>()<br>
self.similarity_loss = torch.nn.CosineSimilarity()</p>
<pre><code>def forward(self, output_pred, output_real):
    y_pred_0, y_pred_1, y_pred_2, y_pred_3 = output_pred[3], output_pred[6], output_pred[9], output_pred[12]
    y_0, y_1, y_2, y_3 = output_real[3], output_real[6], output_real[9], output_real[12]

    loss_0 = torch.mean(1 - self.similarity_loss(y_pred_0.view(y_pred_0.shape[0], -1), y_0.view(y_0.shape[0], -1)))
    loss_1 = torch.mean(1 - self.similarity_loss(y_pred_1.view(y_pred_1.shape[0], -1), y_1.view(y_1.shape[0], -1)))
    loss_2 = torch.mean(1 - self.similarity_loss(y_pred_2.view(y_pred_2.shape[0], -1), y_2.view(y_2.shape[0], -1)))
    loss_3 = torch.mean(1 - self.similarity_loss(y_pred_3.view(y_pred_3.shape[0], -1), y_3.view(y_3.shape[0], -1)))

    total_loss = loss_0 + loss_1 + loss_2 + loss_3

    return total_loss
</code></pre>
<p>error is：<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/9/a90aa615dcc24c13f5c5e8d46d0eeb9aa91ea836.png" data-download-href="https://discuss.pytorch.org/uploads/default/a90aa615dcc24c13f5c5e8d46d0eeb9aa91ea836" title="snipaste_20230323_091207"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/9/a90aa615dcc24c13f5c5e8d46d0eeb9aa91ea836_2_690x356.png" alt="snipaste_20230323_091207" data-base62-sha1="o7pvspTxqCeKqwnckuW5e3BwLau" width="690" height="356" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/9/a90aa615dcc24c13f5c5e8d46d0eeb9aa91ea836_2_690x356.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/9/a90aa615dcc24c13f5c5e8d46d0eeb9aa91ea836_2_1035x534.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/a/9/a90aa615dcc24c13f5c5e8d46d0eeb9aa91ea836_2_1380x712.png 2x" data-dominant-color="444346"><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">snipaste_20230323_091207</span><span class="informations">1718×888 122 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>

ptrblck: <p>Based on the error message it seems you are passing a <code>str</code> to your model instead of a tensor, so make sure the input to the <code>forward</code> method is a valid tensor.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```.</p>

qaqbanxian: <p>Thank you for your reply，but  Is this question related to internet speed?<br>
I’m a newbie thanks。</p>

ptrblck: <p>No, my answer and your question are unrelated to internet speed.<br>
The error you are getting is caused by a wrong input to your neural network. Here is a small example:</p>
<pre><code class="lang-python">model = nn.Conv2d(3, 3, 3)

input = "this is a string which is not a valid input"
out = model(input)
# TypeError: conv2d() received an invalid combination of arguments - got (str, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of:
#  * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
#       didn't match because some of the arguments have invalid types: (!str!, !Parameter!, !Parameter!, !tuple of (int, int)!, !tuple of (int, int)!, !tuple of (int, int)!, int)
#  * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)
#       didn't match because some of the arguments have invalid types: (!str!, !Parameter!, !Parameter!, !tuple of (int, int)!, !tuple of (int, int)!, !tuple of (int, int)!, int)

# works
input = torch.randn(1, 3, 24, 24)
out = model(input)
</code></pre>