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
def get_negatives(uids, iids, items, df_test): | |
"""Returns a pandas dataframe of 100 negative interactions | |
based for each user in df_test. | |
Args: | |
uids (np.array): Numpy array of all user ids. | |
iids (np.array): Numpy array of all item ids. | |
items (list): List of all unique items. | |
df_test (dataframe): Our test set. |
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
def get_train_instances(): | |
"""Samples a number of negative user-item interactions for each | |
user-item pair in our testing data. | |
Returns: | |
user_input (list): A list of all users for each item | |
item_input (list): A list of all items for every user, | |
both positive and negative interactions. | |
labels (list): A list of all labels. 0 or 1. | |
""" |
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
for epoch in range(epochs): | |
# Get our training input. | |
user_input, item_input, labels = get_train_instances() | |
# Generate a list of minibatches. | |
minibatches = random_mini_batches(user_input, item_input, labels) | |
# 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
import tensorflow as tf | |
import numpy as np | |
import pandas as pd | |
import math | |
import heapq | |
from tqdm import tqdm | |
# Load and prepare our data. | |
uids, iids, df_train, df_test, df_neg, users, items, item_lookup = load_dataset() |
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 | |
#------------- | |
num_neg = 4 | |
latent_features = 8 | |
epochs = 20 | |
batch_size = 256 | |
learning_rate = 0.001 |
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 | |
#------------- | |
num_neg = 6 | |
latent_features = 8 | |
epochs = 20 | |
batch_size = 256 | |
learning_rate = 0.001 |
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 | |
from sklearn.metrics.pairwise import cosine_similarity | |
from scipy import sparse | |
#------------------ | |
# LOAD THE DATASET | |
#------------------ |
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
#------------------------ | |
# ITEM-ITEM CALCULATIONS | |
#------------------------ | |
# As a first step we normalize the user vectors to unit vectors. | |
# magnitude = sqrt(x2 + y2 + z2 + ...) | |
magnitude = np.sqrt(np.square(data_items).sum(axis=1)) |
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 random | |
import pandas as pd | |
import numpy as np | |
import scipy.sparse as sparse | |
from scipy.sparse.linalg import spsolve | |
from sklearn.preprocessing import MinMaxScaler | |
#------------------------- | |
# LOAD AND PREP THE DATA |
OlderNewer