How to load model which finished learning by pytorch

I made neural net and tried to cord which predict labels.
So I load model ,which finished learning, to the cord for estimating labels.
But, the error occurred about definition of variables which are defined in model.
Did I mistake to load model for neural net?
The error message

Traceback (most recent call last):
  File "C:/Users/name/Desktop/myo-python-1.0.4/bindsnet-master/bindsnet/predicted_RSNN.py", line 154, in <module>
    pred = torch.mean(predicted, feed_dict={x: np.array([tmp])})
NameError: name 'predicted' is not defined

Model of neural net

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

cord

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 >= 1:
                response = np.argmax(np.bincount(temp))
                print("Predicted gesture: {0}".format(response))
                temp = []
                start = time.time()
            if len(data) > 0:
                tmp = []
                for v in listener.get_emg_data():
                    tmp.append(v[1])
                tmp = list(np.stack(tmp).flatten())
                if len(tmp) >= 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)

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

I defined the model which based on logistic regression.
I don not know the predict method by logistic regression.
I thought that it is needed to correct input_data (data queue[1,8]) to the list(tmp) till length of list become 64.
After that the list of input_data substitute the model. like → pred = model(input_data)
Finally, label probabilities are output . [0.34,-0.2] (1×6)
So pred is calculated. pred = torch.argmax(pred, dim=1)
is my idea correct?
The model

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

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

Thank you for your reply.
I changed the model 's define by following your advice.
And I checked the contain of model(Input_data).
The shape of it is [1,6400].
I felt it was defined correctly and the code was working as it was.
The pred is [1,6400], while there are only 6 labels.
Will it calculate the index of the maximum value of this?
Or is the model’s define so wrong because I ues the bindsnet.
cord

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

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

Is it correct? so I changed the model’s define because I made the 2 layers in network.
Input layer has 64 neuron to match the shape of input_data and output layers has 6400 neurons.

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