How should i reshape my data

Muhammad_Izaz: <p>i am using Resent for 1d data and its showing this error<br>
RuntimeError: size mismatch, m1: [16 x 51200], m2: [2048 x 2] at C:\w\1\s\tmp_conda_3.7_080832\conda\conda-bld\pytorch_1580544815892\work\aten\src\TH/generic/THTensorMath.cpp:41<br>
here</p>
<p>def forward(self, x):</p>
<pre><code>    x  = F.relu(self.bn1(self.conv1(x.float())))
    
    x  = self.layer1(x)
    x  = self.layer2(x)
    x  = self.layer3(x)
    x  = self.layer4(x)
    
    x  = F.avg_pool1d(x,4)

    x  = x.view(x.size(0),-1)
    x  = self.fc(x)
    
    return x</code></pre>

ptrblck: <p>The <code>view</code> operation itself is right.<br>
How did you define the <code>in_features</code> of <code>self.fc</code>, as they seem to create the mismatch?</p>

Muhammad_Izaz: <p>so, what should i do for the correction.<br>
this my Resnet block</p>
<p>class ResNet(torch.nn.Module):<br>
def <strong>init</strong>(self, block , num_layers, classes=2):<br>
super(ResNet,self).<strong>init</strong>()<br>
self.input_planes = 64<br>
self.conv1  = torch.nn.Conv1d(1, 64, kernel_size=3, stride=1, padding=1)<br>
self.bn1    = torch.nn.BatchNorm1d(64)<br>
self.layer1 = self._layer(block, 64, num_layers[0], stride=1)<br>
self.layer2 = self._layer(block, 128, num_layers[1], stride=1)<br>
self.layer3 = self._layer(block, 256, num_layers[2], stride=1)<br>
self.layer4 = self._layer(block, 512, num_layers[3], stride=1)<br>
self.averagePool = torch.nn.AvgPool1d(kernel_size = 4, stride = 1)<br>
self.fc     = torch.nn.Linear(512*block.expension, classes)</p>
<pre><code>def _layer(self, block, planes, num_layers, stride= 1):
    dim_change= None
    if stride != 1 or planes != self.input_planes*block.expension:
        dim_change = torch.nn.Sequential(torch.nn.Conv1d(self.input_planes, planes*block.expension, kernel_size=1, stride=stride),
                                        torch.nn.BatchNorm1d(planes*block.expension))
    netLayers = []
    netLayers.append(block(self.input_planes,planes, stride=stride, dim_change= dim_change))
    self.input_planes = planes * block.expension
    for i in range(1,num_layers):
        netLayers.append(block(self.input_planes,planes))
        self.input_planes = planes* block.expension
        
    return torch.nn.Sequential(*netLayers)</code></pre>

ptrblck: <p><code>512*block.expension</code> doesn’t match the incoming number of features, so you would either have to decrease the spatial size of the activation through conv/pool layers or set <code>in_features=51200</code> for <code>self.fc</code>.</p>

Muhammad_Izaz: <p>Could you please explain that what happen was the problem with my network and how it is removed.</p>

Muhammad_Izaz: <p>it has been running still now for three hours and did not even show a single output whats the problem</p>

ptrblck: <p>The problem is that the passed activation map contains more features than you’ve specified in the linear layer, which should accept this input.</p>
<aside class="quote no-group" data-username="Muhammad_Izaz" data-post="6" data-topic="77216" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/muhammad_izaz/48/16831_2.png" class="avatar"> Muhammad_Izaz:</div>
<blockquote>
<p>it has been running still now for three hours and did not even show a single output whats the problem</p>
</blockquote>
</aside>
<p>When should what kind of output be printed?<br>
If you are expecting an output in each iteration, your code might hang.<br>
In this case, could you set <code>num_workers=0</code> in your <code>DataLoader</code> and rerun the script?</p>

Muhammad_Izaz: <p>it is still not showing accuracy and loss</p>
<p>train_iterator = torch.utils.data.TensorDataset(x_train, y_train.reshape(-1).long())<br>
train_data = torch.utils.data.DataLoader(train_iterator, batch_size = 16, shuffle = True)<br>
for x, y in train_data:<br>
break<br>
print(x.shape, y.shape)<br>
criterion = torch.nn.CrossEntropyLoss()</p>
<p>optimizer = torch.optim.Adam(net.parameters(), lr=0.0001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0, amsgrad=False)<br>
epochs = 3<br>
batches = len(train_data)# number of batches in your dataset (where each batch contains 64 examples from your training data)<br>
losses=[]<br>
accuracy=[]<br>
for epoch in range(epochs):<br>
epoch_loss = 0.0<br>
epoch_accuracy = 0.0<br>
for features, labels in train_data:<br>
outputs = net(features)<br>
loss = criterion (outputs,labels)<br>
optimizer.zero_grad()<br>
loss.backward()<br>
optimizer.step()<br>
epoch_loss += loss.item() # here we are accumulating loss for each training batch.<br>
epoch_accuracy += (outputs.argmax(1) == labels).float().mean()</p>
<pre><code>losses.append(epoch_loss/batches)
accuracy.append(epoch_accuracy/batches)   
print(f'Epoch: {epoch} -&gt; Loss: {(epoch_loss/batches):.8f}, Accuracy: {(epoch_accuracy/batches):.8f}')
# dividing epoch_loss by number of batches in our train_data to get mean epoch_loss
# and printing epoch loss round of to 8 decimals.... same for epoch_accuracy</code></pre>

ptrblck: <p>Could you add a print statement in each iteration?</p>
<p>PS: You can post code snippets by wrapping them into three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"><br>
This would make is easier to debug the code, as I’m currently unsure about the indentation of the code.</p>