This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import pandas as pd | |
import numpy as np | |
import scipy.sparse as sparse | |
from scipy.sparse.linalg import spsolve | |
import random | |
from sklearn.preprocessing import MinMaxScaler | |
import implicit # The Cython library |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import pandas as pd | |
import numpy as np | |
import scipy.sparse as sparse | |
from scipy.sparse.linalg import spsolve | |
import random | |
from sklearn.preprocessing import MinMaxScaler | |
import implicit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import pandas as pd | |
import numpy as np | |
import scipy.sparse as sp | |
from tqdm import tqdm | |
#--------------------------- | |
# LOAD AND PREPARE THE DATA | |
#--------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------- | |
# HYPERPARAMS | |
#------------- | |
epochs = 50 | |
batches = 30 | |
num_factors = 64 # Number of latent features | |
# Independent lambda regularization values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------- | |
# TENSORFLOW GRAPH | |
#------------------------- | |
# Set up our Tensorflow graph | |
graph = tf.Graph() | |
def init_variable(size, dim, name=None): | |
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with graph.as_default(): | |
''' | |
Loss function: | |
-SUM ln σ(xui - xuj) + λ(w1)**2 + λ(w2)**2 + λ(w3)**2 ... | |
ln = the natural log | |
σ(xuij) = the sigmoid function of xuij. | |
λ = lambda regularization value. | |
||W||**2 = the squared L2 norm of our model parameters. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------ | |
# GRAPH EXECUTION | |
#------------------ | |
# Run the session. | |
session = tf.Session(config=None, graph=graph) | |
session.run(init) | |
# This has noting to do with tensorflow but gives |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#----------------------- | |
# FIND SIMILAR ARTISTS | |
#----------------------- | |
def find_similar_artists(artist=None, num_items=10): | |
"""Find artists similar to an artist. | |
Args: | |
artist (str): The name of the artist we want to find similar artists for |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#--------------------- | |
# MAKE RECOMMENDATION | |
#--------------------- | |
def make_recommendation(user_id=None, num_items=10): | |
"""Recommend items for a given user given a trained model | |
Args: | |
user_id (int): The id of the user we want to create recommendations for. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import pickle | |
def load_dataset(): | |
""" | |
Loads the lastfm dataset from a pickle file into a pandas dataframe | |
and transforms it into the format we need. | |
We then split it into a training and a test set. |