2022-02-24 20:12:08 +01:00
|
|
|
import numpy as np
|
2022-02-24 20:18:31 +01:00
|
|
|
|
|
|
|
from node2vec import Node2Vec
|
2022-02-24 20:12:08 +01:00
|
|
|
from sklearn.gaussian_process.kernels import Kernel, Hyperparameter
|
|
|
|
from sklearn.gaussian_process.kernels import GenericKernelMixin
|
|
|
|
from sklearn.gaussian_process import GaussianProcessRegressor
|
|
|
|
#from sklearn.gaussian_process import GaussianProcessClassifier
|
|
|
|
from sklearn.base import clone
|
|
|
|
|
|
|
|
class BookKernel(GenericKernelMixin, Kernel):
|
2022-02-24 21:53:54 +01:00
|
|
|
def __init__(self, wv):
|
|
|
|
self.wv = wv
|
2022-02-24 20:12:08 +01:00
|
|
|
|
|
|
|
def _f(self, s1, s2):
|
2022-02-24 21:53:54 +01:00
|
|
|
"""
|
|
|
|
kernel value between a pair of sequences
|
|
|
|
"""
|
|
|
|
s = self.wv.similarity(s1, s2)**2*0.99 + 0.01
|
|
|
|
if s <= 0:
|
|
|
|
print('bad!')
|
|
|
|
return s
|
2022-02-24 20:12:08 +01:00
|
|
|
|
|
|
|
def __call__(self, X, Y=None, eval_gradient=False):
|
|
|
|
if Y is None:
|
|
|
|
Y = X
|
|
|
|
|
|
|
|
if eval_gradient:
|
|
|
|
return (
|
2022-02-24 21:53:54 +01:00
|
|
|
np.array([[self._f(x, y) for y in Y] for x in X])
|
2022-02-24 20:12:08 +01:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
return np.array([[self._f(x, y) for y in Y] for x in X])
|
2022-02-24 21:53:54 +01:00
|
|
|
#return np.array(self.wv.n_similarity(X, Y))
|
2022-02-24 20:12:08 +01:00
|
|
|
|
|
|
|
def diag(self, X):
|
|
|
|
return self(X)
|
|
|
|
|
|
|
|
def is_stationary(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def clone_with_theta(self, theta):
|
|
|
|
cloned = clone(self)
|
|
|
|
cloned.theta = theta
|
|
|
|
return cloned
|
|
|
|
|
2022-02-25 00:44:58 +01:00
|
|
|
def genGprScores(G, scoreName='gpr_score', stdName='gpr_std'):
|
|
|
|
print('[\] Constructing Feature-Space-Projector')
|
2022-02-24 21:53:54 +01:00
|
|
|
node2vec = Node2Vec(G, dimensions=32, walk_length=16, num_walks=128, workers=8)
|
|
|
|
print('[\] Fitting Embeddings for Kernel')
|
|
|
|
model = node2vec.fit(window=8, min_count=1, batch_words=4)
|
|
|
|
wv = model.wv
|
|
|
|
print('[\] Constructing Kernel')
|
|
|
|
kernel = BookKernel(wv)
|
2022-02-25 00:44:58 +01:00
|
|
|
print('[\] Fitting GP')
|
2022-02-24 20:12:08 +01:00
|
|
|
X, y = [], []
|
|
|
|
for n in G.nodes:
|
|
|
|
node = G.nodes[n]
|
2022-02-24 21:53:54 +01:00
|
|
|
if 'rating' in node and node['rating']!=None:
|
2022-02-24 20:12:08 +01:00
|
|
|
X.append(n)
|
|
|
|
y.append(node['rating'])
|
2022-02-24 21:53:54 +01:00
|
|
|
gpr = GaussianProcessRegressor(kernel=kernel, random_state=3141, alpha=1e-8).fit(X, y)
|
2022-02-25 00:44:58 +01:00
|
|
|
print('[\] Inferencing GP')
|
2022-02-24 20:12:08 +01:00
|
|
|
X = []
|
|
|
|
for n in G.nodes:
|
|
|
|
node = G.nodes[n]
|
2022-02-24 22:29:52 +01:00
|
|
|
if not 'rating' in node or node['rating']==None:
|
2022-02-24 20:12:08 +01:00
|
|
|
X.append(n)
|
2022-02-24 21:53:54 +01:00
|
|
|
y, stds = gpr.predict(X, return_std=True)
|
|
|
|
i=0
|
2022-02-24 20:12:08 +01:00
|
|
|
for n in G.nodes:
|
|
|
|
node = G.nodes[n]
|
2022-02-24 22:29:52 +01:00
|
|
|
if not 'rating' in node or node['rating']==None:
|
2022-02-25 00:44:58 +01:00
|
|
|
s, std = y[i], stds[i][i][0]
|
2022-02-24 21:53:54 +01:00
|
|
|
i+=1
|
2022-02-24 22:29:52 +01:00
|
|
|
node[scoreName], node[stdName] = float(s), float(std)
|