Runtime error while do training

ibrahim_khan: <p>class segmodel(nn.Module):<br>
def <strong>init</strong>(self, depth=3,num_classes=22):<br>
super().<strong>init</strong>()<br>
self.pt = MLPin()<br>
self.encod1 = encoder1()<br>
self.encod2 = encoder()<br>
self.encod3 = encoder()<br>
self.encod4 = encoder()<br>
self.decod1 = decoder()<br>
self.decod2 = decoder()<br>
self.decod3 = decoder()<br>
self.decod4 = decoder()<br>
self.conv1 = nn.Conv1d(1, 64, 1)<br>
self.bn1 = nn.BatchNorm1d(64)<br>
self.conv2 = nn.Conv1d(64, 128, 1)<br>
self.bn2 = nn.BatchNorm1d(128)<br>
self.drop1 = nn.Dropout(0.2)<br>
self.conv3 = nn.Conv1d(128, num_classes, 1)</p>
<pre><code>def forward(self, x):
    x1 = self.pt(x)
    x2 = self.encod1(x)
    x3 = self.decod1(x2)

    x4 = self.encod2(x3)
    x5 = self.decod2(x4)

    x6 = self.encod3(x5)
    x6 = x4 +x6
    x7 = self.decod3(x6)
    x7 = x3 +x7
    x8 = self.encod4(x7)
    x8 = x2+ x8
    x9 = self.decod4(x8)

    x10 = x9+x1
    x10 = torch.reshape(x10,(batch_size,num_point*Number_of_Slices,1))

    x10 = x10.permute(0, 2, 1)


    x10 = F.relu(self.bn1(self.conv1(x10)), inplace=True)
    print(x10.shape)


    x10 = self.drop1(F.relu(self.bn2(self.conv2(x10)), inplace=True))
    print(x10.shape)
    x10 = self.conv3(x10)
    print(x10.shape)


    x10 = F.softmax(x10, dim=1)

    x10 = x10.permute(0, 2, 1)

    # x10= x10.contiguous().view(-1, 22)
    # x10 = torch.reshape(x10,(batch_size*Number_of_Slices*num_point,22))


    return x10
</code></pre>
<p>class decoder(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.mpl1 = MLP2()<br>
self.mpl2 = MLP2()<br>
self.mpl3 = MLP2()<br>
self.sft = nn.Softmax(dim=2)<br>
self.conv1 = nn.Conv1d(batch_size*Number_of_Slices,Number_of_Slices,1)<br>
# self.adaptive1 = nn.AdaptiveAvgPool1d(1)<br>
# self.adaptive2 = nn.AdaptiveAvgPool1d(2)</p>
<pre><code>def forward(self, x):
    out1 = self.mpl1(x)
    out1 =out1.mean(dim=(1) )

    out2 = self.mpl2(x)
    out2 = out2.mean(dim=(2))

    f1 = torch.reshape(out1, (batch_size * num_point, 1))
    f2 = torch.reshape(out2, (batch_size * Number_of_Slices, 1))
    f1 = torch.squeeze(f1)
    f2 = torch.squeeze(f2)
    f3 = torch.outer(f1, f2)
    f4 = torch.reshape(f3, (batch_size, num_point, batch_size * Number_of_Slices))
    f4 = torch.permute(f4, (0, 2, 1))

    out3 = self.sft(f4)
    out3 = self.conv1(out3)
    out4 = self.mpl3(x)
    out5 = out3 + out4
    out = out5 + x
    #
    # feature_vector1 = feature_maps.mean(dim=(1))  # or dim=(2, 3)
    # feature_vector2 = feature_maps.mean(dim=(2))
    # f1 = torch.reshape(feature_vector1, (16384, 1))
    # f2 = torch.reshape(feature_vector2, (288, 1))
    # f1 = torch.squeeze(f1)
    # f2 = torch.squeeze(f2)
    #
    # print(feature_vector1.shape)
    # print(feature_vector2.shape)
    #
    # f3 = torch.outer(f1, f2)
    # f4 = torch.reshape(f3, (8, 2048, 288))
    # f4 = torch.permute(f4, (0, 2, 1))


    return out
</code></pre>
<p>I got this error. Does reshaping creates problem. Please help me out in this.<br>
torch.autograd.backward(self, gradient, retain_graph, create_graph, inputs=inputs)<br>
File “/home/cyz/anaconda3/envs/ibrahim/lib/python3.8/site-packages/torch/autograd/<strong>init</strong>.py”, line 154, in backward<br>
Variable._execution_engine.run_backward(<br>
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn</p>

ptrblck: <p>Double post from <a href="https://discuss.pytorch.org/t/i-got-the-following-issue-kindly-help/135212/2">here</a>.</p>

ibrahim_khan: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> Could you see the issue in the code please? I have been trying but could not succeed yet.</p>
<p>Thank you.</p>

ptrblck: <p>Please post a minimal, executable code snippet as your current one uses undefined modules.<br>
You can post code snippets by wrapping them into three backticks ```.</p>