File size: 3,923 Bytes
a3ab6c4 |
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 |
#### Imports ####
import torch
import torch.nn as nn
import random
adj_list = [[1,2,3], [0,2,3], [0, 1, 3], [0, 1, 2], [5, 6], [4,6], [4, 5], [1, 3]]
size_vertex = len(adj_list) # number of vertices
#### Hyperparameters ####
w = 3 # window size
d = 2 # embedding size
y = 200 # walks per vertex
t = 6 # walk length
lr = 0.025 # learning rate
v=[0,1,2,3,4,5,6,7] #labels of available vertices
#### Random Walk ####
def RandomWalk(node,t):
walk = [node] # Walk starts from this node
for i in range(t-1):
node = adj_list[node][random.randint(0,len(adj_list[node])-1)]
walk.append(node)
return walk
class Model(torch.nn.Module):
def __init__(self):
super(Model, self).__init__()
self.phi = nn.Parameter(torch.rand((size_vertex, d), requires_grad=True))
self.phi2 = nn.Parameter(torch.rand((d, size_vertex), requires_grad=True))
def forward(self, one_hot):
hidden = torch.matmul(one_hot, self.phi)
out = torch.matmul(hidden, self.phi2)
return out
model = Model()
def skip_gram(wvi, w):
for j in range(len(wvi)):
for k in range(max(0,j-w) , min(j+w, len(wvi))):
#generate one hot vector
one_hot = torch.zeros(size_vertex)
one_hot[wvi[j]] = 1
out = model(one_hot)
loss = torch.log(torch.sum(torch.exp(out))) - out[wvi[k]]
loss.backward()
for param in model.parameters():
param.data.sub_(lr*param.grad)
param.grad.data.zero_()
for i in range(y):
random.shuffle(v)
for vi in v:
wvi=RandomWalk(vi,t)
skip_gram(wvi, w)
print(model.phi)
#### Hierarchical Softmax ####
def func_L(w):
"""
Parameters
----------
w: Leaf node.
Returns
-------
count: The length of path from the root node to the given vertex.
"""
count=1
while(w!=1):
count+=1
w//=2
return count
# func_n returns the nth node in the path from the root node to the given vertex
def func_n(w, j):
li=[w]
while(w!=1):
w = w//2
li.append(w)
li.reverse()
return li[j]
def sigmoid(x):
out = 1/(1+torch.exp(-x))
return out
class HierarchicalModel(torch.nn.Module):
def __init__(self):
super(HierarchicalModel, self).__init__()
self.phi = nn.Parameter(torch.rand((size_vertex, d), requires_grad=True))
self.prob_tensor = nn.Parameter(torch.rand((2*size_vertex, d), requires_grad=True))
def forward(self, wi, wo):
one_hot = torch.zeros(size_vertex)
one_hot[wi] = 1
w = size_vertex + wo
h = torch.matmul(one_hot,self.phi)
p = torch.tensor([1.0])
for j in range(1, func_L(w)-1):
mult = -1
if(func_n(w, j+1)==2*func_n(w, j)): # Left child
mult = 1
p = p*sigmoid(mult*torch.matmul(self.prob_tensor[func_n(w,j)], h))
return p
hierarchicalModel = HierarchicalModel()
def HierarchicalSkipGram(wvi, w):
for j in range(len(wvi)):
for k in range(max(0,j-w) , min(j+w, len(wvi))):
#generate one hot vector
prob = hierarchicalModel(wvi[j], wvi[k])
loss = - torch.log(prob)
loss.backward()
for param in hierarchicalModel.parameters():
param.data.sub_(lr*param.grad)
param.grad.data.zero_()
for i in range(y):
random.shuffle(v)
for vi in v:
wvi = RandomWalk(vi,t)
HierarchicalSkipGram(wvi, w)
for i in range(8):
for j in range(8):
print((hierarchicalModel(i,j).item()*100)//1, end=' ')
print(end = '\n')
|