Friday, May 4, 2018

Looking at Ext-RESCAL Xk prediction w.r.t. won tensor-util

In my previous blog post "Notes for Ext-Rescal (may 3rd)"[1] I talked about:

Xk = A*R*A.T where A*R*A.T is a prediction for Xk .

Today I am going to use a utility from the Web of Needs to (attempt to) verify this assumption.

tensor-utils.py in won-matcher-rescal/../python/tools/ contains lines 240 to 262. Line 240 states
"
TESTING METHOD for rescal algorithm output predict hints"

Line 244 states

# - threshold: write out only those predictions that are above the threshold

Line 249 to 250 show how to create predictions.

# compute prediction array with scores
hint_prediction_matrix = np.dot(A,np.dot(R[SparseTensor.CONNECTION_SLICE], A.T))

Following numpy documentation, numpy.dot is for a 2-D array
"If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred."


I also guessed that SparseTensor.CONNECTION_SLICE was 0. A short python program derived from lines 29 and 31 verifies this.

class SparseTensor:
   CONNECTION_SLICE = 0

print SparseTensor.CONNECTION_SLICE






>> 0

A short program to implement lines 249 to 250 would be:

import numpy as np

 A = np.array([[-0.70710678, 0.70710678], [ 0.52943053, 0.52943053], [ 0.52943053, 0.52943053],[ 0.00206809, 0.00206809]])

R = np.array([np.array([[  5.47627165e-01,  -1.16883182e-16],[ -6.07013365e-17,   1.29500171e-32]]), np.array([[  1.06958431e-03,  -1.65920612e-19], [ -2.28287465e-19,   3.54015545e-35]]), np.array([[  1.74139035e-33,   5.09236793e-17], [  5.09233343e-17,   1.47866314e+00]])])

hint_prediction_matrix = np.dot(A,np.dot(R[0], A.T))


Lines 252 to 253:
# choose indices above threshold to keep
hint_indices = hint_prediction_matrix > threshold

This is like the first paragraph describing theta in "4.4 Solving Relational Learning Tasks" in
M. Nickel et al., "A Three-Way Model for Collective Learning on Multi-Relational Data"

A short program to implement lines 252 to 253 would be:

threshold = 5.99602451e-04
hint_indices = hint_prediction_matrix > threshold

print hint_indices
>> array([[ True, False, False, False],
       [False,  True,  True, False],
       [False,  True,  True, False],
       [False, False, False, False]], dtype=bool)

Lines 252 to 257:

   # choose indices above threshold to keep
    hint_indices = hint_prediction_matrix > threshold
    if not keepScore:
        hint_prediction_matrix[hint_indices] = 1
    hint_mask_matrix = np.zeros(hint_prediction_matrix.shape)
    hint_mask_matrix[hint_indices] = 1




A short program to implement lines 252 to 257 would be:
#if not keepScore:
#       hint_prediction_matrix[hint_indices] = 1
hint_mask_matrix = np.zeros(hint_prediction_matrix.shape)
hint_mask_matrix[hint_indices] = 1

print hint_indices
print hint_mask_matrix

I am not sure what lines253 and 254 do, so I commented them out (keepScore = True or keepScore=False) and got the same result...



















No comments:

Post a Comment