-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
112 lines (80 loc) · 2.44 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json
from nltk_utils import tokenize, stem, bag_of_word
import numpy as np
from torch.utils.data import Dataset, DataLoader
import torch
import torch.nn as nn
from model import Bot_Model
with open("intents.json", "r") as f:
intents = json.load(f)
all_words = []
tags = []
xy = [] # for dataset...😄
for intent in intents["intents"]:
tag = intent["tag"]
tags.append(tag)
for pattern in intent["patterns"]:
w = tokenize(pattern)
all_words.extend(w) # keep adding to the array
xy.append((w, tag))
ignore_word = ["?", ",", ".", "!"]
# ignore some words
all_words = [stem(w) for w in all_words if w not in ignore_word]
# keep unique word
all_words = sorted(set(all_words))
tags = sorted(set(tags))
X_train = [] # train data
y_train = [] # train label
for (pattern, tag) in xy:
bag = bag_of_word(pattern, all_words)
X_train.append(bag)
label = tags.index(tag)
y_train.append(label) # class label encoding directly
X_train = np.array(X_train)
y_train = np.array(y_train)
class Chat_Dataset(Dataset):
def __init__(self, x, y):
super().__init__()
self.x = x
self.y = y
def __len__(self):
return len(self.x)
def __getitem__(self, index):
word = self.x[index]
label = self.y[index]
# return word,label
return {
"word": torch.tensor(word, dtype=torch.float32),
"label": torch.tensor(label, dtype=torch.long),
}
# !! also try this
chat_dataset = Chat_Dataset(X_train, y_train)
train_loader = DataLoader(chat_dataset, batch_size=4, shuffle=True)
# hyper parameters
epochs = 1000
input_size = len(X_train[0])
hidden_size = 8
num_classes = len(tags)
model = Bot_Model(input_size=len(X_train[0]), hidden_size=8, num_classes=len(tags))
loss_fun = nn.CrossEntropyLoss()
optim = torch.optim.Adam(model.parameters(), lr=0.01)
for epoch in range(epochs):
for i, data in enumerate(train_loader):
output = model(data["word"])
loss = loss_fun(output, data["label"])
loss.backward()
optim.step()
optim.zero_grad()
if epoch % 100 == 0:
print(f"epoch:{epoch}/{epochs} || loss:{loss}")
print(f"final-loss:{loss}")
data = {
"model_state": model.state_dict(),
"all_words": all_words,
"tags": tags,
"epochs": 1000,
"input_size": len(X_train[0]),
"hidden_size": 8,
"num_classes": len(tags),
}
torch.save(data, f="model.pytorch")