TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not str

AP_M: <p>I have uploaded 2 text files in train_ldr and test_ldr data loaders. The data in data loaders is in tensor form.</p>
<pre><code class="lang-auto">model = Net()
optimizer = optim.Adam(model.parameters(), lr=0.001)
loss_func = nn.NLLLoss()

epochs = 20
loss_list = []

model.train()
for epoch in range(epochs):
    total_loss = []
    for batch_idx, (data, Target) in enumerate(train_ldr):
        optimizer.zero_grad()
        # Forward pass
        output = model(data)
        # Calculating loss
        loss = loss_func(output, target)
        # Backward pass
        loss.backward()
        # Optimize the weights
        optimizer.step()
        
        total_loss.append(loss.item())
    loss_list.append(sum(total_loss)/len(total_loss))
    print('Training [{:.0f}%]\tLoss: {:.4f}'.format(
        100. * (epoch + 1) / epochs, loss_list[-1]))
</code></pre>
<p>while execuiting, it gives the error:</p>
<pre><code class="lang-auto">TypeError                                 Traceback (most recent call last)
&lt;ipython-input-37-888ed4dc834d&gt; in &lt;module&gt;
     12         optimizer.zero_grad()
     13         # Forward pass
---&gt; 14         output = model(data)
     15         # Calculating loss
     16         loss = loss_func(output, target)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

&lt;ipython-input-35-6b9a402c220d&gt; in forward(self, x)
     10 
     11     def forward(self, x):
---&gt; 12         x = F.relu(self.conv1(x))
     13         x = F.max_pool2d(x, 2)
     14         x = F.relu(self.conv2(x))

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

~\anaconda3\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
    397 
    398     def forward(self, input: Tensor) -&gt; Tensor:
--&gt; 399         return self._conv_forward(input, self.weight, self.bias)
    400 
    401 class Conv3d(_ConvNd):

~\anaconda3\lib\site-packages\torch\nn\modules\conv.py in _conv_forward(self, input, weight, bias)
    393                             weight, bias, self.stride,
    394                             _pair(0), self.dilation, self.groups)
--&gt; 395         return F.conv2d(input, weight, bias, self.stride,
    396                         self.padding, self.dilation, self.groups)
    397 
</code></pre>
<p>TypeError: conv2d(): argument ‘input’ (position 1) must be Tensor, not str<br>
Please suggest…``</p>

SageAgastya: <p>Could you please post the code snippet where you are defining the whole model.<br>
Also, print a few tensors from dataloader?</p>

AP_M: <pre><code class="lang-auto">class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, kernel_size=5)
        self.conv2 = nn.Conv2d(6, 16, kernel_size=5)
        self.dropout = nn.Dropout2d()
        self.fc1 = nn.Linear(256, 64)
        self.fc2 = nn.Linear(64, 1)
        self.hybrid = Hybrid(qiskit.Aer.get_backend('qasm_simulator'), 100, np.pi / 2)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2)
        x = self.dropout(x)
        x = x.view(1, -1)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        x = self.hybrid(x)
        return torch.cat((x, 1 - x), -1)
</code></pre>

AP_M: <p>few tensors from training data loader-</p>
<pre><code class="lang-auto">Epoch = 0

Batch = 0
tensor([[939.4254, 948.3578,  81.3177,  12.9644,  12.9642,   6.2724,  64.3632,
           5.6000],
        [848.6209, 851.5731,  57.8863,  14.1427,  14.1425,   4.0930,  71.0401,
           8.1333],
        [898.1860, 907.0068,  84.4972,  16.3053,  16.3053,   5.1822,  67.4501,
          13.0667]])
tensor([1, 1, 1])

Batch = 1
tensor([[973.2529, 964.6500,  82.4052,  10.6442,  10.6436,   7.7418,  62.0951,
           2.0000],
        [925.6989, 937.1903, 147.6703,  14.7563,  14.7563,  10.0073,  66.5943,
           8.3333],
        [811.1076, 799.7836, 101.2528,   8.4278,   8.4277,  12.0142,  74.9993,
           1.1333]])
tensor([0, 0, 0])

Batch = 2
tensor([[933.5856, 893.8986, 130.0819,  16.3578,  16.3565,   7.9523,  65.4439,
          11.5333],
        [959.6946, 957.8956,  54.9045,  12.0154,  12.0153,   4.5695,  62.7270,
           3.2667],
        [923.2839, 617.7942, 517.5366,   9.9660,   9.9339,  51.9303,  81.3423,
           1.2000]])
tensor([0, 0, 0])
</code></pre>

Kapil_Rana: <p>The error would be in input data, probably.<br>
Can you please tell me here the “data” value whenever this error came (it would be easy to see in on which x error is coming)<br>
or is this coming for all x or data values.<br>
print the type of data and check.</p>

AP_M: <p>This error is not allowing to execute the code block. It is for all x values, I assume. A small sample of input file is:</p>
<pre><code class="lang-auto">885.1578446	853.76373	140.9727412	15.55450456	15.55337134	9.063145708	69.49995211	11.13333333	0
939.4253707	948.357865	81.31774203	12.96443946	12.96419467	6.272368526	64.36314993	5.6	1
898.1860468	907.00686	84.49723585	16.30527867	16.3052737	5.182201271	67.45006559	13.06666667	1
881.7578645	893.46003	90.37053724	15.72046849	15.72006806	5.748590592	68.8095618	11.8	0
</code></pre>
<p>The code is:</p>
<pre><code class="lang-auto">for epoch in range(2):
    print("\n==============================\n")
    print("Epoch = " + str(epoch))
    for (batch_idx, batch) in enumerate(train_ldr):
      print("\nBatch = " + str(batch_idx))
      X_train = batch['data']  
      Y_train = batch['Target']  
      print(X_train)
      print(Y_train)
</code></pre>
<p>A small sample output of data is</p>
<pre><code class="lang-auto">Epoch = 0

Batch = 0
tensor([[939.4254, 948.3578,  81.3177,  12.9644,  12.9642,   6.2724,  64.3632,
           5.6000],
        [848.6209, 851.5731,  57.8863,  14.1427,  14.1425,   4.0930,  71.0401,
           8.1333],
        [898.1860, 907.0068,  84.4972,  16.3053,  16.3053,   5.1822,  67.4501,
          13.0667]])
tensor([1, 1, 1])

Batch = 1
tensor([[973.2529, 964.6500,  82.4052,  10.6442,  10.6436,   7.7418,  62.0951,
           2.0000],
        [925.6989, 937.1903, 147.6703,  14.7563,  14.7563,  10.0073,  66.5943,
           8.3333],
        [811.1076, 799.7836, 101.2528,   8.4278,   8.4277,  12.0142,  74.9993,
           1.1333]])
tensor([0, 0, 0])
</code></pre>