Я пытаюсь создать голосового помощника с искусственным интеллектом, затем я получил эту ошибку, которую я пытался найти в Интернете, но она не сработала

#python #machine-learning #neural-network #pytorch

#питон #машинное обучение #нейронная сеть #пыторч

Вопрос:

Я пытаюсь сделать голосового помощника, но он выдает мне ошибку, он пытался ее исправить, но не сработал. Код ниже, Кстати, имя помощника-HoverAssist

Код Для Brain.py

 import torch.nn as nn  class NeuralNet(nn.Module):  def __init__(self, input_size, hidden_size, num_classes):  super(NeuralNet, self).__init__()  self.l1 = nn.Linear(input_size,hidden_size)  self.l2 = nn.Linear(input_size,hidden_size)  self.l3 = nn.Linear(hidden_size,num_classes)  self.relu = nn.ReLU()   def forward(self, x):  out = self.l1(x)  out = self.relu(out)  out = self.l2(out)  out = self.relu(out)  out = self.l3(out)  return out  

Кодекс Train.py

 import numpy as np import json import torch import torch.nn as nn from torch.utils.data import Dataset,DataLoader from NeuralNetwork import bag_of_words, tokenize, stem from Brain import NeuralNet  with open('intents.json','r') as f:  intents = json.load(f)  all_words = [] tags = [] xy = []  for intent in intents['intents']:  tag = intent['tag']  tags.append(tag)   for pattern in intent['patterns']:  w = tokenize(pattern)  all_words.extend(w)  xy.append((w,tag))   ignore_words = [',','?','.',"/",'!'] all_words = [stem(w) for w in all_words if w not in ignore_words] all_words = sorted(set(all_words)) tags = sorted(set(tags))  X_train = [] y_train = []  for (pattern_sentence, tag) in xy:  bag = bag_of_words(pattern_sentence, all_words)  X_train.append(bag)  label = tags.index(tag)  y_train.append(label)  X_train = np.array(X_train) y_train = np.array(y_train)  num_epochs = 1000 batch_size = 8 learning_rate = 0.001 hidden_size = 8 input_size = len(X_train[0]) output_size = len(tags)  print("Training The Model...")  class ChatDataset(Dataset):   def __init__(self):  self.n_samples = len(X_train)  self.x_data = X_train  self.y_data = y_train   def __getitem__(self, index):  return self.x_data[index], self.y_data[index]   def __len__(self):  return self.n_samples  dataset = ChatDataset()  train_loader = DataLoader(dataset=dataset,  batch_size=batch_size,  shuffle = True)  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') model = NeuralNet(input_size,hidden_size,output_size).to(device= device) criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)  for epoch in range(num_epochs):  for (words, labels) in train_loader:  words = words.to(device=device,dtype=torch.long)  labels = labels.to(device=device,dtype=torch.long)  optimizer.zero_grad()  output = model(words)  loss = criterion(output,labels)  loss.backward()  optimizer.step()    if (epoch 1) % 100 == 0:  print(f"Epoch [{epoch 1}/{num_epochs}], Loss: {loss.item():.4f}") print(f"Final Loss: {loss.item():.4f}") data = { "model_state":model.state_dict(), "input_size":input_size, "hidden_size":hidden_size, "output_size":output_size, "all_words":all_words, "tags":tags }  FILE = "TrainData.pth" torch.save(data,FILE)  print(f"Traing Complete") print(f"Saved Model To {FILE}")  

Код для NeuralNetwork.py

 import numpy  import nltk  from nltk.stem.porter import PorterStemmer nltk.download('punkt') Stemmer = PorterStemmer()  def tokenize(text):  return nltk.word_tokenize(text)  def stem(word):  return Stemmer.stem(word.lower())  def bag_of_words(tokenized_text, words):  sentence_words = [stem(word) for word in tokenized_text]  bag = numpy.zeros(len(words), dtype=numpy.float32)  for idx, w in enumerate(words):  if w in sentence_words:  bag[idx] = 1  return bag  

Error While i am running train.py

(I even Tried to use x = x.type(torch.FloatTensor) but it gave me another error so i undid it)

 Training The Model... Traceback (most recent call last):  File "a:/Train.py", line 80, in lt;modulegt;  output = model(words)  File "C:UsersUserAppDataRoamingPythonPython36site-packagestorchnnmodulesmodule.py", line 1102, in _call_impl  return forward_call(*input, **kwargs)  File "a:Brain.py", line 13, in forward  out = self.l1(x)  File "C:UsersUserAppDataRoamingPythonPython36site-packagestorchnnmodulesmodule.py", line 1102, in _call_impl  return forward_call(*input, **kwargs)  File "C:UsersUserAppDataRoamingPythonPython36site-packagestorchnnmoduleslinear.py", line 103, in forward  return F.linear(input, self.weight, self.bias)  File "C:UsersUserAppDataRoamingPythonPython36site-packagestorchnnfunctional.py", line 1848, in linear  return torch._C._nn.linear(input, weight, bias) RuntimeError: expected scalar type Float but found Long