How to load model which finished learning by pytorch

username: ryunosuke0723
cooked: <p>I made neural net and tried to cord which predict labels.<br>
So I load model ,which finished learning, to the cord for estimating labels.<br>
But, the error occurred about definition of variables which are defined in model.<br>
Did I mistake to load model for neural net?<br>
The 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/predicted_RSNN.py", line 154, in &lt;module&gt;
    pred = torch.mean(predicted, feed_dict={x: np.array([tmp])})
NameError: name 'predicted' is not defined
</code></pre>
<p>Model of neural net</p>
<pre><code class="lang-auto">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):
        out = self.linear(x)
        predicted = torch.max(out.softmax(0), 1)
        #out = torch.sigmoid(self.linear(x.float().view(-1)))
        return out, predicted, x
</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
model_path = "C:/Users/name/Desktop/myo-python-1.0.4/bindsnet-master/bindsnet/pytorchsession/snn.pth"

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\name\Desktop\myo-sdk-win-0.9.0\bin')
    HUB = myo.Hub()
    listener = MyListener()
    start = time.time()
    temp = []
    with HUB.run_in_background(listener.on_event):
        while True:
            data = listener.get_emg_data()
            if time.time() - start &gt;= 1:
                response = np.argmax(np.bincount(temp))
                print("Predicted gesture: {0}".format(response))
                temp = []
                start = time.time()
            if len(data) &gt; 0:
                tmp = []
                for v in listener.get_emg_data():
                    tmp.append(v[1])
                tmp = list(np.stack(tmp).flatten())
                if len(tmp) &gt;= 64:
                    pred = torch.mean(predicted, feed_dict={x: np.array([tmp])}) ☚ error
                    pred = sess.run(y_pred_cls, feed_dict={x: np.array([tmp])})
                    temp.append(pred[0])
            sleep(0.01)

</code></pre>

username: ptrblck
cooked: <p>The <code>predicted</code> tensor is never calculated and based on your code snippet it doesn’t seem <code>listener</code> is called with input data, so you would have to add the model execution to the code.</p>

username: ryunosuke0723
cooked: <p>I defined the model  which based on logistic regression.<br>
I don not know the predict method by logistic regression.<br>
I thought that it is needed to correct input_data (data queue[1,8]) to the list(tmp) till length of list become 64.<br>
After that the list of input_data substitute the model. like → pred = model(input_data)<br>
Finally, label probabilities are output . [0.34,-0.2] (1×6)<br>
So pred is calculated. pred = torch.argmax(pred, dim=1)<br>
is my idea correct?<br>
The model</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import numpy as np
class LogisticRegression(nn.Module):
    def __init__(self, input_size, num_classes):
        super(LogisticRegression, self).__init__()
        self.linear = nn.Linear(input_size, num_classes)
        self.linear_1 = nn.Linear(num_classes, 6400)
    def forward(self, x):
       # x = torch.randn(1, 64).float()
        x = torch.zeros(1, 64, dtype=torch.float)
        y = torch.zeros(1, 6, dtype=torch.float)  # 2
        x = self.linear(x)
        output = self.linear_1(x)
        return output
</code></pre>

username: ptrblck
cooked: <p>Your idea of passing the input data to the model, getting the output, and using <code>argmax</code> to get the predictions, is correct.<br>
However, the forward pass (passing the data to the model) as well as the prediction calculation are not used in your code, so <code>predicted</code> is undefined and raises the error.</p>

username: ryunosuke0723
cooked: <p>Thank you for your reply.<br>
I changed the model 's define by following your advice.<br>
And I checked the contain of model(Input_data).<br>
The shape of it is [1,6400].<br>
I felt it was defined correctly and the code was working as it was.<br>
The pred is [1,6400], while there are only 6 labels.<br>
Will it calculate the index of the maximum value of this?<br>
Or is the model’s define so wrong because I ues the bindsnet.<br>
cord</p>
<pre><code class="lang-auto">class LogisticRegression(nn.Module):
    def __init__(self, input_size, num_classes):
        super(LogisticRegression, self).__init__()
        self.linear = nn.Linear(input_size, num_classes)
        self.linear_1 = nn.Linear(num_classes, 6400)
    def forward(self, x):
        x = self.linear(x)
        output = self.linear_1(x)
        pred = torch.max(output, 1)

        return output, pred
</code></pre>

username: ptrblck
cooked: <p>The last linear layer should output <code>num_classes</code> logits, so you would have to define <code>self.linear_1 = nn.Linear(..., num_classes)</code>.<br>
Also, <code>pred = torch.argmax(output, 1)</code> can be defined outside of the model, if needed (you also changed it to <code>torch.max</code> now).</p>

username: ryunosuke0723
cooked: <p>Is it correct?  so I changed the model’s define because I made the 2 layers in network.<br>
Input layer has 64 neuron to match the shape of input_data and output layers has 6400 neurons.</p>
<pre><code class="lang-auto">lass 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):
        output = self.linear(x)
        pred = torch.max(output, 1)

        return output, pred
</code></pre>