For this blog Movielens-1m Data sets ratings.dat Train as recommended data MF Recommended model . The first column is users id(user_id)、 The second column is the items id(item_id)、 The third column is the user's rating of the item (score)、 The fourth column is the timestamp (timestamp).
stay Movielens-1m Data set , Be careful ratings.dat Yes, it is ::
As a separator ..
import numpy as np
import pandas as pd
import os
from tensorflow import keras # tensorflow == 2.X
import warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
warnings.filterwarnings('ignore')
# Make recommendations
def recommend(user_id, uel, mel, N):
movies = uel[user_id-1] @ mel.T # -1 Because the preprocessed user id from 0 Start
mids = np.argpartition(movies, -N)[-N:]
return mids
if __name__ == "__main__":
# ------ Read in the data ------ #
dataset = pd.read_csv("./ratings.dat", sep="::", names=["user_id", "item_id", "rating", "timestamp"])
# Data preprocessing , Subscript from 0 Start , Remove missing values so that the values are continuous
dataset.user_id = dataset.user_id.astype('category').cat.codes.values
dataset.item_id = dataset.item_id.astype('category').cat.codes.values
# Get a list of users and items
user_arr = dataset.user_id.unique()
movies_arr = dataset.item_id.unique()
# Get the number of users and items
n_users, n_movies = len(user_arr), len(movies_arr) # 6040 3706
n_latent_factors = 20
# ------ Set up Keras Parameters ------ #
# Set project parameters
movie_input = keras.layers.Input(shape=[1], name='Item')
movie_embedding = keras.layers.Embedding(n_movies + 1, n_latent_factors, name='Movie-Embedding')(movie_input)
movie_vec = keras.layers.Flatten(name='FlattenMovies')(movie_embedding)
# Set user parameters
user_input = keras.layers.Input(shape=[1], name='User')
user_embedding = keras.layers.Embedding(n_users + 1, n_latent_factors, name='User-Embedding')(user_input)
user_vec = keras.layers.Flatten(name='FlattenUsers')(user_embedding)
# Calculate the point product of the item vector and the user tensor
prod = keras.layers.dot([movie_vec, user_vec], axes=1, name='DotProduct')
# Create user - Project model
model = keras.Model([user_input, movie_input], prod)
# Setting up the model optimizer 、 Loss function 、 Measures
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae', 'mse'])
# ------ Training models ------ #
# Training users - Project model
# verbose=0: No log output ;verbose=1: Output every step Training progress and log ;verbose=2: Output each epochs Log
model.fit([dataset.user_id, dataset.item_id], dataset.rating, epochs=10, verbose=1)
# Get the embedding matrix of users and projects
user_embedding_learnt = model.get_layer(name='User-Embedding').get_weights()[0]
movie_embedding_learnt = model.get_layer(name='Movie-Embedding').get_weights()[0]
# ------ Make recommendations ------ #
# To the user 1 recommend top10
user = 1
topN = recommend(user_id=user, uel=user_embedding_learnt, mel=movie_embedding_learnt, N=10)
temp_topN = topN.tolist()
print("------ user ------")
print(user)
print("------ temp_topN ------")
print(temp_topN)
# Recommend to all users Top10
# topN_list = {} # Store a list of recommendations for each user
# for each_user in tqdm(user_arr, total=len(user_arr)):
# print("------ user ------")
# print(each_user)
# topN = recommend(user_id=each_user, uel=user_embedding_learnt, mel=movie_embedding_learnt, N=len(movie_embedding_learnt))
# topN_list[str(each_user)] = [i+1 for i in topN.tolist()]
# print("------ temp_topN ------")
# print(topN_list[-1])
For users 1(user_id=1) Produce a recommended output ( Example ):
Epoch 1/2
31257/31257 [==============================] - 223s 7ms/step - loss: 0.1862 - mae: 0.3020 - mse: 0.1862
Epoch 2/2
31257/31257 [==============================] - 202s 6ms/step - loss: 0.1042 - mae: 0.2151 - mse: 0.1042
------ user ------
1
------ temp_topN ------
[579, 1618, 2131, 576, 309, 892, 513, 1563, 106, 346]
Catalog 1 introduction 2 Probl
List of articles 1. Friends pr