I'm currently working on a recommender system using PyG. The edges are defined as follows:
edge_index = tensor([[ 0, 0, 0, ..., 9315, 9317, 9317],
[ 100, 448, 452, ..., 452, 1, 307]], device='cuda:0')}
edge_index[0]
containing the indexes for a student and edge_index[1]
containing the index of connected modules (both have the same length). Therefore edge_index[0][i]
is the source node of edge i
and edge_index[1][i]
is the destination of edge i
.
After model training, I'm generating a 2D-tensor recs
with the shape of # of Students x # of Modules, with values from 0-1. 0 = not recommended and 1 = recommended. recs
could look like this:
recs = tensor([0.54, 0.23, 0.98, ..., 0.12, 0.43, 0.87],
...,
[0.43, 0.53, 0.12, ..., 0.92, 0.12, 0.53])
Of course, I don't want to recommend a module if the student has already taken it. Is there a way to set all edges from the original graph to zero, by using the edge_index
from PyG as coordinates or something?
Basically i want to set specific values in recs
to 0 like this:
for i in range(0, len(edge_index[0])):
recs[edge_index[0][i]][edge_index[1][i]] = 0
Is there a way using the tensor functions to achieve this?
Since you want to index recs
on both axes simultaneously a straight implementation is to to vectorize your for loop as:
>>> recs[edge_index[0], edge_index[1]] = 0
Which you can improve by splitting edge_index
with tuple
:
>>> recs[tuple(edge_index)] = 0