Create models.py
Browse files
models.py
ADDED
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
import re
|
4 |
+
import numpy as np
|
5 |
+
import pandas as pd
|
6 |
+
from collections import OrderedDict
|
7 |
+
import requests
|
8 |
+
from bs4 import BeautifulSoup
|
9 |
+
|
10 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
11 |
+
if device == 'cuda:0':
|
12 |
+
torch.cuda.set_device(device)
|
13 |
+
print(device)
|
14 |
+
|
15 |
+
def extract_text_from_link(url):
|
16 |
+
response = requests.get(url)
|
17 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
18 |
+
text = soup.get_text()
|
19 |
+
return text
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
doc = """The word "deep" in "deep learning" refers to the number of layers through which the data is transformed. More precisely,
|
24 |
+
deep learning systems have a substantial credit assignment path (CAP) depth. The CAP is the chain of transformations from input to
|
25 |
+
output. CAPs describe potentially causal connections between input and output. For a feedforward neural network, the depth of the
|
26 |
+
CAPs is that of the network and is the number of hidden layers plus one (as the output layer is also parameterized). For recurrent
|
27 |
+
neural networks, in which a signal may propagate through a layer more than once, the CAP depth is potentially unlimited.[13] No
|
28 |
+
universally agreed-upon threshold of depth divides shallow learning from deep learning, but most researchers agree that deep
|
29 |
+
learning involves CAP depth higher than 2. CAP of depth 2 has been shown to be a universal approximator in the sense that it
|
30 |
+
can emulate any function.[14] Beyond that, more layers do not add to the function approximator ability of the network. Deep
|
31 |
+
models (CAP > 2) are able to extract better features than shallow models and hence, extra layers help in learning the features
|
32 |
+
effectively."""
|
33 |
+
|
34 |
+
|
35 |
+
class Text2Words:
|
36 |
+
def __init__(self, document):
|
37 |
+
self.text_all = re.findall(r'\b[A-Za-z]+\b', document)
|
38 |
+
self.text = list(set(self.text_all))
|
39 |
+
self.chars_all = ''.join(self.text)
|
40 |
+
self.chars = self.unique_chars(self.chars_all)
|
41 |
+
self.int2char = dict(enumerate(self.chars))
|
42 |
+
self.char2int = {char: ind for ind, char in self.int2char.items()}
|
43 |
+
self.maxlen = len(max(self.text, key=len))
|
44 |
+
self.update_text()
|
45 |
+
self.input_seq_char, self.target_seq_char = self.get_seq_char(self.text)
|
46 |
+
self.input_seq_index, self.target_seq_index = self.get_seq(self.char2int, self.input_seq_char, self.target_seq_char, len(self.text))
|
47 |
+
self.dict_size = len(self.char2int)
|
48 |
+
self.seq_len = self.maxlen - 1
|
49 |
+
self.batch_size = len(self.text)
|
50 |
+
self.input_seq = self.one_hot_encode(self.input_seq_index, self.dict_size, self.seq_len, self.batch_size)
|
51 |
+
|
52 |
+
def one_hot_encode(self, sequence, dict_size, seq_len, batch_size):
|
53 |
+
# Creating a multi-dimensional array of zeros with the desired output shape
|
54 |
+
features = np.zeros((batch_size, seq_len, dict_size), dtype=np.float32)
|
55 |
+
|
56 |
+
# Replacing the 0 at the relevant character index with a 1 to represent that character
|
57 |
+
for i in range(batch_size):
|
58 |
+
for u in range(seq_len):
|
59 |
+
features[i, u, sequence[i][u]] = 1
|
60 |
+
return features
|
61 |
+
|
62 |
+
def get_seq(self, char2int, input_seq_char, target_seq_char,n):
|
63 |
+
x=[]
|
64 |
+
y=[]
|
65 |
+
for i in range(n):
|
66 |
+
x.append([char2int[character] for character in input_seq_char[i]])
|
67 |
+
y.append([char2int[character] for character in target_seq_char[i]])
|
68 |
+
return x,y
|
69 |
+
|
70 |
+
def get_seq_char(self, text):
|
71 |
+
input_seq = []
|
72 |
+
target_seq = []
|
73 |
+
|
74 |
+
for i in range(len(text)):
|
75 |
+
# Remove last character for input sequence
|
76 |
+
input_seq.append(text[i][:-1])
|
77 |
+
# Remove first character for target sequence
|
78 |
+
target_seq.append(text[i][1:])
|
79 |
+
return input_seq, target_seq
|
80 |
+
|
81 |
+
def unique_chars(self, chars_all):
|
82 |
+
chars = []
|
83 |
+
for letter in chars_all:
|
84 |
+
if letter not in chars:
|
85 |
+
chars.append(letter)
|
86 |
+
# chars = sorted(chars)
|
87 |
+
if ' ' not in chars:
|
88 |
+
chars.append(' ')
|
89 |
+
return sorted(chars)
|
90 |
+
|
91 |
+
def update_text(self):
|
92 |
+
for i in range(len(self.text)):
|
93 |
+
while len(self.text[i])<self.maxlen:
|
94 |
+
self.text[i] += ' '
|
95 |
+
|
96 |
+
def description(self):
|
97 |
+
text = {}
|
98 |
+
for word in self.text:
|
99 |
+
char = word[0]
|
100 |
+
if char not in text:
|
101 |
+
text[char] = []
|
102 |
+
text[char].append(word.strip())
|
103 |
+
for k,v in (sorted(text.items())):
|
104 |
+
print(f'{k} : {sorted(v)}')
|
105 |
+
|
106 |
+
def lengt_analysis(self):
|
107 |
+
text = {}
|
108 |
+
words = set(self.text_all)
|
109 |
+
for word in words:
|
110 |
+
n = len(word)
|
111 |
+
if n not in text:
|
112 |
+
text[n] = []
|
113 |
+
text[n].append(word.strip())
|
114 |
+
for k,v in (sorted(text.items())):
|
115 |
+
print(f'{k} : count = {len(v)} list = {sorted(v)}')
|
116 |
+
return None # text
|
117 |
+
|
118 |
+
|
119 |
+
def create_object(doc):
|
120 |
+
return Text2Words(doc)
|
121 |
+
|
122 |
+
|
123 |
+
def get_inputs(obj):
|
124 |
+
input_seq = torch.tensor(obj.input_seq, device=device)
|
125 |
+
target_seq_index = torch.tensor(obj.target_seq_index, device=device)
|
126 |
+
return input_seq, target_seq_index
|
127 |
+
|
128 |
+
class Model(nn.Module):
|
129 |
+
def __init__(self, input_size, output_size, hidden_dim, n_layers):
|
130 |
+
super(Model, self).__init__()
|
131 |
+
|
132 |
+
# Defining some parameters
|
133 |
+
self.hidden_dim = hidden_dim
|
134 |
+
self.n_layers = n_layers
|
135 |
+
|
136 |
+
#Defining the layers
|
137 |
+
# RNN Layer
|
138 |
+
self.rnn = nn.RNN(input_size, hidden_dim, n_layers, batch_first=True)
|
139 |
+
# Fully connected layer
|
140 |
+
self.fc = nn.Linear(hidden_dim, output_size)
|
141 |
+
|
142 |
+
def forward(self, x):
|
143 |
+
batch_size = x.size(0)
|
144 |
+
hidden = self.init_hidden(batch_size)
|
145 |
+
out, hidden = self.rnn(x, hidden)
|
146 |
+
out = out.contiguous().view(-1, self.hidden_dim)
|
147 |
+
out = self.fc(out)
|
148 |
+
return out, hidden
|
149 |
+
|
150 |
+
def init_hidden(self, batch_size):
|
151 |
+
# This method generates the first hidden state of zeros
|
152 |
+
torch.manual_seed(42)
|
153 |
+
hidden = torch.zeros((self.n_layers, batch_size, self.hidden_dim), device=device)
|
154 |
+
return hidden
|
155 |
+
|
156 |
+
def create_model(obj):
|
157 |
+
model = Model(input_size=obj.dict_size, output_size=obj.dict_size, hidden_dim=2*obj.dict_size, n_layers=1)
|
158 |
+
model.to(device)
|
159 |
+
lr=0.01
|
160 |
+
criterion = nn.CrossEntropyLoss()
|
161 |
+
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
|
162 |
+
return model, criterion, optimizer
|
163 |
+
|
164 |
+
# This function takes in the model and character as arguments and returns the next character prediction and hidden state
|
165 |
+
def predict(model, character):
|
166 |
+
# One-hot encoding our input to fit into the model
|
167 |
+
# print(character)
|
168 |
+
character = np.array([[obj.char2int[c] for c in character]])
|
169 |
+
# print(character)
|
170 |
+
character = obj.one_hot_encode(character, obj.dict_size, character.shape[1], 1)
|
171 |
+
# print(character,character.shape)
|
172 |
+
character = torch.tensor(character, device=device)
|
173 |
+
character.to(device)
|
174 |
+
out, hidden = model(character)
|
175 |
+
# print(out, hidden)
|
176 |
+
prob = nn.functional.softmax(out[-1], dim=0).data
|
177 |
+
# print(prob)
|
178 |
+
char_ind = torch.max(prob, dim=0)[1].item()
|
179 |
+
# print(sorted(prob, reverse=True))
|
180 |
+
return obj.int2char[char_ind], hidden
|
181 |
+
|
182 |
+
# This function takes the desired output length and input characters as arguments, returning the produced sentence
|
183 |
+
def sample(model, out_len, start='h'):
|
184 |
+
model.eval() # eval mode
|
185 |
+
chars = [ch for ch in start]
|
186 |
+
char = chars[-1]
|
187 |
+
chars = chars[:-1]
|
188 |
+
# Now pass in the previous characters and get a new one
|
189 |
+
while char != ' ':
|
190 |
+
chars.append(char)
|
191 |
+
char, h = predict(model, chars)
|
192 |
+
return ''.join(chars)
|
193 |
+
|
194 |
+
|
195 |
+
def load_checkpoint(filepath):
|
196 |
+
checkpoint = torch.load(filepath)
|
197 |
+
# print(checkpoint['state_dict'])
|
198 |
+
model = checkpoint['model']
|
199 |
+
# print(model)
|
200 |
+
model.load_state_dict(checkpoint['state_dict'])
|
201 |
+
# print(model.parameters())
|
202 |
+
# for parameter in model.parameters():
|
203 |
+
# parameter.requires_grad = False
|
204 |
+
# print(parameter)
|
205 |
+
|
206 |
+
|
207 |
+
model.eval()
|
208 |
+
return model
|
209 |
+
|
210 |
+
model = load_checkpoint('checkpoint.pth')
|
211 |
+
|
212 |
+
sample(model, obj.maxlen, 'ap')
|