Expected object of scalar type Float but got scalar type Long for argument #2 'mat1' in call to _th_addmm

username: ryunosuke0723
cooked: <p>I am converting y (shape [1,6]) and output and x ([1,64]) to float type respectively, but I get an error.<br>
Is the conversion method wrong?</p>
<p>error message</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "C:/Users/name/Desktop/myo-python-1.0.4/bindsnet-master/bindsnet/nextrsnn.py", line 95, in &lt;module&gt;
    optimizer.zero_grad(); output = model(s)
  File "C:\Python36\lib\site-packages\torch\nn\modules\module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "C:/Users/name/Desktop/myo-python-1.0.4/bindsnet-master/bindsnet/nextrsnn.py", line 15, in forward
    return self.linear(x)
  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\linear.py", line 91, in forward
    return F.linear(input, self.weight, self.bias)
  File "C:\Python36\lib\site-packages\torch\nn\functional.py", line 1674, in linear
    ret = torch.addmm(bias, input, weight.t())
RuntimeError: Expected object of scalar type Float but got scalar type Long for argument #2 'mat1' in call to _th_addmm

Process finished with exit code 1
</code></pre>
<p>cord</p>
<pre><code class="lang-auto">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

# defining model
class LogisticRegression(nn.Module):
    def __init__(self, input_size, num_classes):
        super(LogisticRegression, self).__init__()
        self.linear = nn.Linear(input_size, num_classes)
    def forward(self, x):
        return self.linear(x)

# building network 
time = 500
network = Network(dt=1.0)
Batch_size = 300
# x(shape=[1,64]), y(shape=[1,6])
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)

# レイヤー同士の接続
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")

# Monitoring layer 
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,)

# Monitorをネットワークに接続
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)

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.float from numpy
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, 1), '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(40, 6); criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)

# training spikes and y
for epoch in range(6):
    for i, (s, y) in enumerate(training_pairs):
        optimizer.zero_grad(); output = model(s)
        loss = criterion(output.softmax(0).view(1, -1), y.squeeze(0).long())
        loss.backward(); optimizer.step()

test_pairs = []
for i, (x, y) in enumerate(zip(x.view(-1, 64), y)):
    network.run(inputs=inputs, 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")
</code></pre>

username: ptrblck
cooked: <p>Based on the error message it seems that either the input or the model parameters are <code>LongTensor</code>s.<br>
You would have to make sure both are <code>FloatTensor</code>s by calling <code>s.float()</code> or <code>model.float()</code> before executing the forward pass.</p>

username: ryunosuke0723
cooked: <p>That was what you said. Rewriting output = model (s.float ()) resolved this error.<br>
However, I got the following error. Where does the 500x1 in this error point? I am very sorry to ask you many questions.</p>
<pre><code class="lang-auto">RuntimeError: size mismatch, m1: [500 x 1], m2: [40 x 6] at ..\aten\src\TH/generic/THTensorMath.cpp:41
</code></pre>

username: ptrblck
cooked: <p>I’m not sure where this error is coming from as your model definition isn’t posted.<br>
Check the complete stack trace and see, if you can spot the line of code which raises the error.<br>
Based on the posted error message e.g. a matrix multiplication might fail with the shape mismatch e.g. in a linear layer.</p>