Only one element tensors can be converted to Python scalars

ryunosuke0723: <p>I created the cord which has function of prediction for labels.<br>
Also, I created the cord of neural net based on pytorch.<br>
So I need to convert to tensor type from numpy array.<br>
First, the input data are acquired as numpy array and be put on the list format.<br>
So, I used (Input = torch.FloatTensor(Input)) to convert to tensor from numpy list.<br>
Next, I tried to the follow changes. I want to know the way to fix it.<br>
response = np.argmax(np.bincount(temp))<br>
↓<br>
response = torch.argmax(torch.bincount(temp))<br>
Then , the error occurred.</p>
<p>Error message</p>
<pre><code class="lang-auto">temp = torch.FloatTensor(temp)
ValueError: only one element tensors can be converted to Python scalars
</code></pre>
<p>cord</p>
<pre><code class="lang-auto">from collections import deque
from threading import Lock
import myo
import time
import numpy as np
from time import sleep
import torch
import torch.nn as nn
from bindsnet.network import Network
from including.models import LogisticRegression
model_path = "C:/Users/name/Desktop/myo-python-1.0.4/bindsnet-master/bindsnet/pytorchsession/snn.pth"
model = LogisticRegression(64,6)

class MyListener(myo.DeviceListener):
    def __init__(self, queue_size=8):
        self.lock = Lock()
        self.emg_data_queue = deque(maxlen=queue_size)
        self.orientation_data_queue = deque(maxlen=queue_size)
        self.acceleration_data_queue = deque(maxlen=queue_size)
        self.gyroscope_data_queue = deque(maxlen=queue_size)
        self.rssi_data_queue = deque(maxlen=100)
        self.pose = myo.Pose.rest
        self.connected = False
        self.battery_level = 100
        self.emg_enabled = False
        self.locked = False
        self.rssi = None
        self.emg = None
        self.device_name = None
        self.device = None
        self.myo_firmware = None
        self.arm = None
        self.x_direction = None
        self.sync = None

    def on_paired(self, event):
        if __name__ == '__main__':
            print('paired')
        else:
            pass

    def on_unpaired(self, event):
        if __name__ == '__main__':
            print('unpaired')
        else:
            pass

    def on_connected(self, event):
        self.device = event.device
        event.device.unlock()
        event.device.lock()
        event.device.stream_emg(True)
        self.connected = True
        self.device_name = event.device_name
        self.myo_firmware = '.'.join(map(str, event.firmware_version[:-1]))

    def on_disconnected(self, event):
        self.connected = False

    def on_arm_synced(self, event):
        self.sync = True
        self.arm = event.arm
        self.x_direction = event.x_direction
        if __name__ == '__main__':
            print(self.x_direction)
        else:
            pass

    def on_arm_unsynced(self, event):
        self.sync = False
        if __name__ == '__main__':
            print(f'arm unsynced : {event.arm}')
        else:
            pass

    def on_unlocked(self, event):
        self.locked = False

    def on_locked(self, event):
        self.locked = True

    def on_pose(self, event):
        self.pose = event.pose

    def on_orientation(self, event):
        with self.lock:
            self.orientation_data_queue.append((event.timestamp,
                                                event.orientation))
            self.gyroscope_data_queue.append((event.timestamp,
                                              event.gyroscope))
            self.acceleration_data_queue.append((event.timestamp,
                                                 event.acceleration))

    def on_rssi(self, event):
        with self.lock:
            self.rssi_data_queue.append(-event.rssi)

    def on_battery_level(self, event):
        self.battery_level = event.battery_level

    def on_emg(self, event):
        with self.lock:
            self.emg_data_queue.append((event.timestamp,
                                        event.emg))

    def on_warmup_completed(self, event):
        event.device.stream_emg(True)
        self.emg_enabled = True

    def get_emg_data(self):
        with self.lock:
            return list(self.emg_data_queue)

    def get_orientation_data(self):
        with self.lock:
            return list(self.orientation_data_queue)

    def get_gyroscope_data(self):
        with self.lock:
            return list(self.gyroscope_data_queue)

    def get_accelerometor_data(self):
        with self.lock:
            return list(self.acceleration_data_queue)



if __name__ == '__main__':

    myo.init(bin_path=r'C:\Users\田澤龍之介\Desktop\myo-sdk-win-0.9.0\bin')
    HUB = myo.Hub()
    model.eval()
    listener = MyListener()
    start = time.time()
    temp = []
    with HUB.run_in_background(listener.on_event):
        while True:
            data = listener.get_emg_data()
            print(temp)
            if time.time() - start &gt;= 1:
                #response = np.argmax(np.bincount(temp)) # conventionally  numpy format
                temp = torch.FloatTensor(temp)    # I tried to convert to tensor from numpy list
                response = torch.argmax(torch.bincount(temp)) 
                #print(response)
                print("Predicted gesture: {0}".format(response))
                temp = []
                start = time.time()
            if len(data) &gt; 0:              # len(data) = 8
                tmp = []
                for v in listener.get_emg_data():
                    tmp.append(v[1])
                tmp = list(np.stack(tmp).flatten())
                tmp = torch.tensor(tmp) 
                if len(tmp) &gt;= 64:
                    pred = model(tmp)
                    temp.append(pred[0])
            sleep(0.01)
</code></pre>

ptrblck: <p>Could you print the type and shape of <code>temp</code> in this line of code:</p>
<pre><code class="lang-python">temp = torch.FloatTensor(temp)    # I tried to convert to tensor from numpy list
</code></pre>
<p>Do you mean “numpy array” by “numpy list”? If so, you should be able to use <code>torch.from_numpy(temp)</code> as long as the array contains an array with a known numerical type.</p>