To integrate SLIM with other models (for example, use SLIM to construct the item similarity graph and run GCN on it to compute item embeddings for item-based recommendation), it'll be nice that SLIM exports the similarity matirx as a scipy sparse matrix directly.
Currently, SLIM exports the similarity matrix to a file first before being loaded as a scipy matrix. Here is an example code we have to do. This is slow and inconvenient.
from SLIM import SLIM, SLIMatrix
model = SLIM()
params = {'algo': 'cd', 'nthreads': 2, 'l1r': 1.0, 'l2r': 1.0}
trainmat = SLIMatrix(user_movie_spm.tocsr())
model.train(params, trainmat)
model.save_model(modelfname='slim_model.csr', mapfname='slim_map.csr')
def read_csr(filename):
f = open(filename, 'r')
all_rows = []
all_cols = []
all_vals = []
for i, line in enumerate(f.readlines()):
strs = line.split(' ')
cols = [int(s) for s in strs[1::2]]
vals = [float(s) for s in strs[2::2]]
all_cols.extend(cols)
all_vals.extend(vals)
all_rows.extend([i for _ in cols])
all_rows = np.array(all_rows, dtype=np.int64)
all_cols = np.array(all_cols, dtype=np.int64)
all_vals = np.array(all_vals, dtype=np.float32)
mat = spsp.coo_matrix((all_vals, (all_rows, all_cols)))
return mat
movie_spm = read_csr('slim_model.csr')
To integrate SLIM with other models (for example, use SLIM to construct the item similarity graph and run GCN on it to compute item embeddings for item-based recommendation), it'll be nice that SLIM exports the similarity matirx as a scipy sparse matrix directly.
Currently, SLIM exports the similarity matrix to a file first before being loaded as a scipy matrix. Here is an example code we have to do. This is slow and inconvenient.