-
Notifications
You must be signed in to change notification settings - Fork 0
/
torchworks.py
172 lines (133 loc) · 4.85 KB
/
torchworks.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from torch import cuda, device, nn, relu, optim, flatten, cat, sigmoid, load, softmax, max as tmax, from_numpy, no_grad
from math import floor
from torch.utils.data import Dataset, DataLoader
from numpy import array as nparray
from os import path as px
batch = 64
s_len = 128
modelpath = px.join(px.dirname(__file__), "nets/")
devicex = device("cuda:0" if cuda.is_available() else "cpu")
def tensor2device(tensor, print_dev=False):
tensor = tensor.to(devicex)
if print_dev:
print(devicex)
return tensor
def get_device():
if cuda.is_available():
return "cuda:0"
else:
return "cpu"
class miniNN(nn.Module):
def __init__(self, num_feature):
super(miniNN, self).__init__()
self.layer_out = nn.Linear(num_feature, 2)
def forward(self, x):
x = self.layer_out(x)
return x
class TextClassifier(nn.ModuleList):
def __init__(self):
super(TextClassifier, self).__init__()
self.seq_len: int = 3
# Model parameters
self.embedding_size: int = s_len
self.out_size: int = 32
self.stride: int = 3
self.kernels = [4, 5]
# Training parameters
self.batch_size: int = batch
self.learning_rate: float = 0.001
# Dropout definition
self.dropout = nn.Dropout(0.25)
# CNN parameters definition
self.convs = []
self.pools = []
for kernel in self.kernels:
# Convolution layers definition
x = nn.Conv1d(self.seq_len, self.out_size, kernel, self.stride).to(devicex)
self.convs.append(x)
y = nn.MaxPool1d(kernel, self.stride).to(devicex)
self.pools.append(y)
# Fully connected layer definition
self.fc = nn.Linear(self.in_features_fc(), 2)
def in_features_fc(self):
out_pools = []
for i, kernel in enumerate(self.kernels):
x = ((self.embedding_size - 1 * (self.kernels[i] - 1) - 1) / self.stride) + 1
y = ((floor(x) - 1 * (self.kernels[i] - 1) - 1) / self.stride) + 1
out_pools.append(floor(y))
# Returns "flattened" vector (input for fully connected layer)
return (sum(out_pools)) * self.out_size
def forward(self, x):
x = x.float()
outs = []
for i, k in enumerate(self.kernels):
y = self.convs[i](x)
y = relu(y)
y = self.pools[i](y)
outs.append(y)
union = cat(tuple(outs), 2)
union = union.reshape(union.size(0), -1)
# The "flattened" vector is passed through a fully connected layer
out = self.fc(union)
# Dropout is applied
out = self.dropout(out)
# Activation function is applied
out = sigmoid(out)
return out.squeeze()
class DatasetMaper(Dataset):
def __init__(self, x, y):
self.x = x
self.y = y
def __len__(self):
return len(self.x)
def __getitem__(self, idx):
return self.x[idx], self.y[idx]
class ClassifierDataset(Dataset):
def __init__(self, X_data, y_data):
self.X_data = X_data
self.y_data = y_data
def __getitem__(self, index):
return self.X_data[index], self.y_data[index]
def __len__(self):
return len(self.X_data)
def pad(seq, target_length=s_len, padding=0):
length = len(seq)
if length > target_length:
seq = seq[:target_length]
else:
# seq.extend([padding] * (target_length - length))
for i in range(target_length-length):
seq.append(seq[i % length])
return seq
def netpass(input, net, cnn):
if cnn:
model = TextClassifier()
dataset = DatasetMaper
input = [[pad(x) for x in y] for y in input]
else:
model = miniNN(3)
dataset = ClassifierDataset
model.load_state_dict(load(str(px.join(modelpath, net)), map_location=device(devicex)))
model.to(devicex)
inputs = nparray(input)
pseudo_outs = nparray([0 for x in inputs])
test_dataset = dataset(from_numpy(inputs).float(), from_numpy(pseudo_outs).long())
test_loader = DataLoader(dataset=test_dataset, batch_size=1)
y_pred_list = []
y_prob_list = []
with no_grad():
model.eval()
for X_batch, _ in test_loader:
X_batch = X_batch.to(devicex)
y_test_pred = model(X_batch)
if cnn:
y_test_pred = y_test_pred.unsqueeze(0)
y_pred_softmax = softmax(y_test_pred, dim=1)
y_pred_prob, y_pred_tags = tmax(y_pred_softmax, dim=1)
y_pred_list.append(y_pred_tags.cpu().numpy())
y_prob_list.append(y_pred_prob.cpu().numpy())
y_pred_list = [a.squeeze().tolist() for a in y_pred_list]
y_prob_list = [a.squeeze().tolist() for a in y_prob_list]
if cnn:
y_pred_list[0] = y_pred_list[0]
return y_pred_list[0], round(y_prob_list[0], 4)