-
Notifications
You must be signed in to change notification settings - Fork 1
/
skipgram.py
190 lines (143 loc) · 6.24 KB
/
skipgram.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from walk_generator import WalkGenerator
from tqdm import tqdm
import matplotlib.pyplot as plt
from torchviz import make_dot
from sklearn.decomposition import PCA
#this is a pytorch implementation. Code is throughly commented
#defining the model as a pytorch model
class skipgramModel(nn.Module):
vocabSize = 652
embeddingSize = 10
def __init__(self):
print("instatiated model")
super(skipgramModel, self).__init__()
#initial weights, each node has embeddingSize weights associated to it, this later becomes the embedding
self.embedding = nn.Embedding(self.vocabSize, self.embeddingSize)
#first layer weights
self.W1 = nn.Linear(self.embeddingSize, self.embeddingSize, bias=False)
#output layer weights
self.W2 = nn.Linear(self.embeddingSize, self.vocabSize, bias=False)
def forward(self, X):
#receives input node
embeddings = self.embedding(X)
#feeds input node into hidden layer, which goes to a relu activation
hidden_layer = nn.functional.relu(self.W1(embeddings))
#activates the output
output_layer = self.W2(hidden_layer)
return output_layer
def get_player_emdedding(self, player, player2idx):
player = torch.tensor([player2idx[player]])
return self.embedding(player).view(1,-1)
class skipgram:
skipGrams = []
player2idx = {}
idx2player = {}
walks = []
walksArray = []
players = []
embeddingSize = 10
vocabSize = 652
batch_size = 2
windowSize = 1
learningRate = 0.001
epochs = 150000
model = nn.Module()
def tokenize(self):
for walk in self.walks:
self.walksArray.append(walk)
for node in walk:
if node not in self.players:
self.players.append(node)
self.player2idx = {w: idx for (idx, w) in enumerate(self.players)}
self.idx2player = {idx: w for (idx, w) in enumerate(self.players)}
self.vocabSize = len(self.players)
def __init__(self, walks, embeddingSize, batch_size, windowSize, learningRate, epochs):
#number of weights/attributes associated with each node
self.vocabSize = 652
self.embeddingSize=embeddingSize
self.walks = walks
self.batch_size = batch_size
self.windowSize = windowSize
self.learningRate = learningRate
self.epochs = epochs
self.tokenize()
#function for generating batches
def randomBatch(self):
randomInputs = []
randomLabels = []
#generates a range of random indexes of size batch_size, replace false means generated indexes are unique
randomIndex = np.random.choice(range(len(self.skipGrams)), self.batch_size, replace=False)
#for every randomly generated index, appends target and context to their arrays
for i in randomIndex:
randomInputs.append(self.skipGrams[i][0]) # target
randomLabels.append(self.skipGrams[i][1]) # context word
#print(skipGrams[i])
return randomInputs, randomLabels
#generates node pairs between target nodes and their possible contexts
def generateSkipgram(self, walk):
for i in range(self.windowSize, len(walk) - self.windowSize):
target = self.player2idx[walk[i]]
#change this if changing windowSize
context = [self.player2idx[walk[i- self.windowSize]], self.player2idx[walk[i+ self.windowSize]]]
for w in context:
self.skipGrams.append([target, w])
#print(self.skipGrams)
def generateAllSkipgrams(self):
for walk in self.walksArray:
self.generateSkipgram(walk)
def skipgramTest(self, test_data, model):
correct_ct = 0
for i in range(len(test_data)):
input_batch, target_batch = self.randomBatch()
input_batch = torch.LongTensor(input_batch)
target_batch = torch.LongTensor(target_batch)
model.zero_grad()
_, predicted = torch.max(model(input_batch), 1)
if predicted[0] == target_batch[0]:
correct_ct += 1
print('Accuracy: {:.1f}% ({:d}/{:d})'.format(correct_ct/len(test_data)*100, correct_ct, len(test_data)))
def trainModel(self):
#instantiates skipgram
self.model = skipgramModel()
#loss function
criterion = nn.CrossEntropyLoss()
#pytorch optimizer
optimizer = optim.Adam(model.parameters(), self.learningRate)
#genereates all skipgrams for all walks
self.generateAllSkipgrams()
#forward and backproprag of the model using generated skipgrams
#te quiero demasiado
for epoch in tqdm(range(self.epochs)):
#generates random batches
input_batch, target_batch = self.randomBatch()
#puts random batches in a pytorch longtensor for faster computing
input_batch = torch.LongTensor(input_batch)
target_batch = torch.LongTensor(target_batch)
#reset gradient
optimizer.zero_grad()
#forward proprag
#print("aaaa" + str(input_batch))
output = model(input_batch)
#calculate loss
#print(np.shape(output), np.shape(target_batch))
loss = criterion(output, target_batch)
#show loss every 10000 epochs
if (epoch + 1) % 10000 == 0:
print('Epoch:', '%04d' % (epoch + 1), 'cost =', '{:.6f}'.format(loss))
#backward proprag
loss.backward(retain_graph=True)
#applies calculated correction
optimizer.step()
def showResults(self):
print(self.skipgramTest(self.skipGrams,model))
plt.figure(figsize=(20,15))
for player in self.players[int(len(self.players)*0.4):int(len(self.players)*0.6)]:
pca = PCA(n_components=2)
transformed = pca.fit_transform(model.get_player_emdedding(player, self.player2idx).detach().data.numpy()[0])
plt.scatter(transformed[0], transformed[1])
plt.annotate(player, xy=(x, y), xytext=(5, 2), textcoords='offset points', ha='right', va='bottom')
plt.show()