RuntimeError: The size of tensor a (64) must match the size of tensor b (384) at non-singleton dimension 1

I implemented this cord. But error occurred about the size of tensor .
Where should I fix it and what does it mean this error ?
Could someone teach me? please

error message

Traceback (most recent call last):
  File "C:/Users/name/Desktop/myo-python-1.0.4/bindsnet-master/bindsnet/nextrsnn.py", line 88, in <module>
    network.run(inputs=inputs, time=time)
  File "C:\Python36\lib\site-packages\bindsnet\network\network.py", line 357, in run
    self.layers[l].forward(x=current_inputs[l])
  File "C:\Python36\lib\site-packages\bindsnet\network\nodes.py", line 220, in forward
    super().forward(x)
  File "C:\Python36\lib\site-packages\bindsnet\network\nodes.py", line 108, in forward
    self.summed += x.float()
RuntimeError: The size of tensor a (64) must match the size of tensor b (384) at non-singleton dimension 1

cord

import torch
import torch.nn as nn
import numpy as np
from bindsnet.network import Network
from bindsnet.network.nodes import Input, LIFNodes
from bindsnet.network.topology import Connection
from bindsnet.network.monitors import Monitor

# define model 
class LogisticRegression(nn.Module):
    def __init__(self, input_size, num_classes):
        super(LogisticRegression, self).__init__()
        self.linear = nn.Linear(input_size, num_classes)
        self.dropout = nn.Dropout(0.5)

    def forward(self, x):
        output = self.linear(x)
        return output

# building network 
input_size = 64
num_classes = 6
time = 300      

network = Network(dt=1.0)
_BATCH_SIZE = 300
# building 5 layers
inpt = Input(n=64, sum_input=True)
middle = LIFNodes(n=40, trace=True, sum_input=True)
center = LIFNodes(n=40, trace=True, sum_input=True)
final = LIFNodes(n=40, trace=True, sum_input=True)
out = LIFNodes(n=6, sum_input=True)

# connecting each layers
inpt_middle = Connection(source=inpt, target=middle, wmin=0, wmax=1e-1)
middle_center = Connection(source=middle, target=center, wmin=0, wmax=1e-1)
center_final = Connection(source=center, target=final, wmin=0, wmax=1e-1)
final_out = Connection(source=final, target=out, wmin=0, wmax=1e-1)

# connecting all layers to network 
network.add_layer(inpt, name='A')
network.add_layer(middle, name='B')
network.add_layer(center, name='C')
network.add_layer(final,  name='D')
network.add_layer(out, name='E')

forward_connection = Connection(source=inpt, target=middle, w=0.05 + 0.1*torch.randn(inpt.n, middle.n))
network.add_connection(connection=forward_connection, source="A", target="B")
forward_connection = Connection(source=middle, target=center, w=0.05 + 0.1*torch.randn(middle.n, center.n))
network.add_connection(connection=forward_connection, source="B", target="C")
forward_connection = Connection(source=center, target=final, w=0.05 + 0.1*torch.randn(center.n, final.n))
network.add_connection(connection=forward_connection, source="C", target="D")
forward_connection = Connection(source=final, target=out, w=0.05 + 0.1*torch.randn(final.n, out.n))
network.add_connection(connection=forward_connection, source="D", target="E")
recurrent_connection = Connection(source=out, target=out, w=0.025*(torch.eye(out.n)-1),)
network.add_connection(connection=recurrent_connection, source="E", target="E")

# define of monitor as layers
inpt_monitor = Monitor(obj=inpt, state_vars=("s", "v"), time=500,)
middle_monitor = Monitor(obj=inpt, state_vars=("s", "v"), time=500,)
center_monitor = Monitor(obj=inpt, state_vars=("s", "v"), time=500,)
final_monitor = Monitor(obj=inpt, state_vars=("s", "v"), time=500,)
out_monitor = Monitor(obj=inpt, state_vars=("s", "v"), time=500,)

# connecting monitor to network
network.add_monitor(monitor=inpt_monitor, name="A")
network.add_monitor(monitor=middle_monitor, name="B")
network.add_monitor(monitor=center_monitor, name="C")
network.add_monitor(monitor=final_monitor, name="D")
network.add_monitor(monitor=out_monitor, name="E")

for l in network.layers:
    m = Monitor(network.layers[l], state_vars=['s'], time=time)
    network.add_monitor(m, name=l)

# loading data x = shape[1,64] y = [1,6]
npzfile = np.load("C:/Users/name/Desktop/myo-python-1.0.4/myo-armband-nn-master/data/train_set.npz")
x = npzfile['x']
y = npzfile['y']

# converting to tensor
x = torch.from_numpy(x).float()
y = torch.from_numpy(y).float()

training_pairs = []
for i, (x, y) in enumerate(zip(x.view(-1, 64), y)):
    inputs = {'A': x.repeat(time, 6), 'E_b': torch.ones(time, 1)}
    network.run(inputs=inputs, time=time)
    training_pairs.append([network.monitors['E'].get('s').sum(-1), y])
    network.reset_state_variables()

    if (i + 1) % 50 == 0: print('Train progress: (%d / 500)' % (i + 1))
    if (i + 1) == 500: print(); break

model = LogisticRegression(input_size, num_classes); criterion = nn.CrossEntropyLoss()   # m2恫ē›ø当
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
# training for spikes and label
for epoch in range(6):
    for i, (s, y) in enumerate(training_pairs):
        optimizer.zero_grad(); output = model(s.float())
        loss = criterion(output.unsqueeze(0),  y.unsqueeze(0).long())
        loss.backward(); optimizer.step()

test_pairs = []
for i, (x, y) in enumerate(zip(x.view(-1, 64), y)):
    network.run(inpts={'A': x}, time=time)
    test_pairs.append([network.monitors['E'].get('s').sum(-1), y])
    network.reset_state_variables()

    if (i + 1) % 50 == 0: print('Test progress: (%d / 500)' % (i + 1))
    if (i + 1) == 500: print(); break

correct, total = 0, 0
for s, y in test_pairs:
    output = model(s); _, predicted = torch.max(output.data.unsueeze(0), 1)
    total += 1; correct += int(predicted == y.long())

print('Accuracy of logistic regression on 500 test examples: %2f %%\n ' % (100 * correct / total))
torch.save({
            'epoch': epoch,
            'model_state_dict': model.state_dict(),
            'optimizer_state_dict': optimizer.state_dict(),
            'loss': loss,
            }, "C:/Users/name/Desktop/myo-python-1.0.4/bindsnet-master/bindsnet/pytorchsession")

Based on the error message you are either using an input and target with a different batch size or the model reshapes the activation in a wrong way.
As mentioned before in related posts, please share an executable code snippet to reproduce this issue, so that we could debug it.

Thank you for your message.
The shape of x is [1,64] and y is [1,6]. In the for statement, x shape = [64] and y shape = [6]
The error location is as follows.

for epoch in range(6):
    for i, (s, y) in enumerate(training_pairs):
        optimizer.zero_grad(); output = model(s.float())
        loss = criterion(output.unsqueeze(0),  y.unsqueeze(0).long()) ā˜šhere is error cord
        loss.backward(); optimizer.step()

Unfortunately, I cannot debug the issue if you donā€™t provide a code snippet I could run locally.
If you cannot provide executable code, add print statements after all tensors and check the shape.
Make sure the batch size stays the same and no view or reshape operation is changing it.

I checked the size of y and output.
The size of y is torch.Size([6])
The size of output is torch.Size([1, 6]
Also, when I adjusted the size to [1,6], I got the following error.

Traceback (most recent call last):
  File "C:/Users/tazawa/Desktop/myo-python-1.0.4/bindsnet-master/bindsnet/nextrsnn.py", line 107, in <module>
    loss = criterion(output.squeeze(-1),  y.squeeze(-1).long())
  File "C:\Python36\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "C:\Python36\lib\site-packages\torch\nn\modules\loss.py", line 948, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "C:\Python36\lib\site-packages\torch\nn\functional.py", line 2422, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "C:\Python36\lib\site-packages\torch\nn\functional.py", line 2218, in nll_loss
    ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: 1D target tensor expected, multi-target not supported

Since you are using nn.CRossEntropyLoss or nn.NLLLoss (based on the stack trace), I assume you are working with a multi-class classification. Based on the output shape of the model, the current batch contains a single sample with logits for 6 classes.
If thatā€™s the case, the target should have the shape [1] and contain a value in the range [0, 5].

2 Likes