Skip to content

Instantly share code, notes, and snippets.

View victorkohler's full-sized avatar

Victor Köhler victorkohler

View GitHub Profile
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
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
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
#---------------------------
#-------------
# HYPERPARAMS
#-------------
epochs = 50
batches = 30
num_factors = 64 # Number of latent features
# Independent lambda regularization values
#-------------------------
# TENSORFLOW GRAPH
#-------------------------
# Set up our Tensorflow graph
graph = tf.Graph()
def init_variable(size, dim, name=None):
'''
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.
#------------------
# GRAPH EXECUTION
#------------------
# Run the session.
session = tf.Session(config=None, graph=graph)
session.run(init)
# This has noting to do with tensorflow but gives
#-----------------------
# 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
#---------------------
# 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.
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.