问题描述
https://github.com/python-engineer/pytorch-chatbot#usage
import numpy as np
import random
import json
import torch
import torch.nn as nn
from torch.utils.data import Dataset,DataLoader
from nltk_utils import bag_of_words,tokenize,stem
from model import NeuralNet
with open('intents.json','r') as f:
intents = json.load(f)
all_words = []
tags = []
xy = []
# loop through each sentence in our intents patterns
for intent in intents['intents']:
tag = intent['tag']
# add to tag list
tags.append(tag)
for pattern in intent['patterns']:
# tokenize each word in the sentence
w = tokenize(pattern)
# add to our words list
all_words.extend(w)
# add to xy pair
xy.append((w,tag))
# stem and lower each word
ignore_words = ['?','.','!']
all_words = [stem(w) for w in all_words if w not in ignore_words]
# remove duplicates and sort
all_words = sorted(set(all_words))
tags = sorted(set(tags))
print(len(xy),"patterns")
print(len(tags),"tags:",tags)
print(len(all_words),"unique stemmed words:",all_words)
# create training data
X_train = []
y_train = []
for (pattern_sentence,tag) in xy:
# X: bag of words for each pattern_sentence
bag = bag_of_words(pattern_sentence,all_words)
X_train.append(bag)
# y: PyTorch CrossEntropyLoss needs only class labels,not one-hot
label = tags.index(tag)
y_train.append(label)
X_train = np.array(X_train)
y_train = np.array(y_train)
# Hyper-parameters
num_epochs = 1000
batch_size = 8
learning_rate = 0.001
input_size = len(X_train[0])
hidden_size = 8
output_size = len(tags)
print(input_size,output_size)
class ChatDataset(Dataset):
def __init__(self):
self.n_samples = len(X_train)
self.x_data = X_train
self.y_data = y_train
# support indexing such that dataset[i] can be used to get i-th sample
def __getitem__(self,index):
return self.x_data[index],self.y_data[index]
# we can call len(dataset) to return the size
def __len__(self):
return self.n_samples
dataset = ChatDataset()
train_loader = DataLoader(dataset=dataset,batch_size=batch_size,shuffle=True,num_workers=2)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = NeuralNet(input_size,hidden_size,output_size).to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(),lr=learning_rate)
# Train the model
for epoch in range(num_epochs):
for (words,labels) in train_loader:
words = words.to(device)
labels = labels.to(device)
# Forward pass
outputs = model(words)
# if y would be one-hot,we must apply
# labels = torch.max(labels,1)[1]
loss = criterion(outputs,labels)
# Backward and optimize
optimizer.zero_grad()
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 = "data.pth"
torch.save(data,FILE)
print(f'training complete. file saved to {FILE}')
当我运行train.py文件时(向上)
RuntimeError: DataLoader worker (pid (s) 11343,11344) exited unexpectedly
我遇到错误。
train.py之后,我需要运行chat.py文件。但是,当我运行chat.py文件时,出现以下错误。
FileNotFoundError: [Errno 2] No such file or directory: 'data.pth'
首先,有人知道我在train.py上遇到此错误的原因吗?如果您有帮助,我会很高兴。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)