-
Notifications
You must be signed in to change notification settings - Fork 3
/
Train.py
96 lines (78 loc) · 2.35 KB
/
Train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import numpy as Numpy
import json
import torch
import torch.nn as NN
from torch.utils.data import Dataset,DataLoader
from NeuralNetworks import BagOfWords,Tokenize,Stem
from Brain import NeuralNetwork
with open('Intents.json','r',encoding="utf8") as File:
Intents=json.load(File)
AllWords=[]
Tags=[]
Combinations=[]
for Intent in Intents['intents']:
Tag=Intent['tag']
Tags.append(Tag)
for Pattern in Intent['patterns']:
Token=Tokenize(Pattern)
AllWords.extend(Token)
Combinations.append((Token,Tag))
IgnoreWords=[',','?','.','!']
AllWords=[Stem(W) for W in AllWords if W not in IgnoreWords]
AllWords=sorted(set(AllWords))
Tags=sorted(set(Tags))
XTrain=[]
YTrain=[]
for (PaternSentence,Tag) in Combinations:
Bag=BagOfWords(PaternSentence,AllWords)
XTrain.append(Bag)
Label=Tags.index(Tag)
YTrain.append(Label)
[[0,1,2,3],[3.4,5,6]]
XTrain=Numpy.array(XTrain)
YTrain=Numpy.array(YTrain)
NumEpoch=1000
BatchSize=8
LearningRate=0.001
InputSize=len(XTrain[0])
HiddenSize=8
OutputSize=len(Tags)
print("Training The Model ...");
class ChatDataset(Dataset):
def __init__(self):
self.NSamples=len(XTrain)
self.XData=XTrain
self.YData=YTrain
def __getitem__(self,index):
return self.XData[index],self.YData[index]
def __len__(self):
return self.NSamples
Dataset=ChatDataset()
TraonLoader=DataLoader(dataset=Dataset,batch_size=BatchSize,shuffle=True,num_workers=0)
Device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
Model=NeuralNetwork(InputSize,HiddenSize,OutputSize).to(device=Device)
Criterion=NN.CrossEntropyLoss()
Optimizer=torch.optim.Adam(Model.parameters(),lr=LearningRate)
for Epoch in range(NumEpoch):
for (Words,Labels) in TraonLoader:
Words=Words.to(Device)
Labels=Labels.to(dtype=torch.long).to(Device)
Outputs=Model(Words)
Loss=Criterion(Outputs,Labels)
Optimizer.zero_grad()
Loss.backward()
Optimizer.step()
if(Epoch+1)%100==0:
print(f"Epoch: [{Epoch+1}/{NumEpoch}] , Loss: {Loss.item():.4f}")
print(f"Final Loss: {Loss.item():.4f}")
Data={
"ModelState":Model.state_dict(),
"InputSize":InputSize,
"OutputSize":OutputSize,
"HiddenSize":HiddenSize,
"AllWords":AllWords,
"Tags":Tags
}
File="DataTrain.pth"
torch.save(Data,File)
print("Training Complete and File Saved")