Movie recommendation system based on convolutional neural network
foreword
Nowadays, traditional recommendation algorithms such as collaborative filtering are widely used for recommendation, but there are also problems such as cold start and matrix sparseness. This project uses deep learning to realize movie recommendation. The core algorithm mainly refers to https://blog.csdn.net/chengcheng1394 /article/details/78820529 , Based on tensorflow and convolutional god-level network to realize personalized movie recommendation. The web side uses django for development.
1. Realize the effect
1.1 Algorithm running results
(1) Enter a movie, recommend similar movies and movies that people who have watched also like
(2) Enter user information and recommend their favorite movies
1.2 Main interface of the system
- login module
- Movie recommendation module
2. Main code implementation
2.1 Network model code
The network model code will not be released here, you can refer to https://blog.csdn.net/chengcheng1394/article/details/78820529
2.2 django code
Here are some main codes of django:
- urls.py configure routing
- settings.py configuration database
- login module
def login(request):
if request.method == 'GET':
return render(request, 'login.html')
else:
#用户POST提交的数据
u = request.POST.get('user')
p = request.POST.get('pwd')
#把账号密码写死了
# if u == 'cxx' and p == '123':
# #登录成功
# return redirect('/mess/')
#从数据库中判断账号密码是否正确
if u and p:
c = User.objects.filter(username=u,password=p).count()
if c >= 1:
#获取当前登录的用户id
cur_id = User.objects.get(username=u, password=p).id
return redirect('/mess/?user_id='+str(cur_id))
else:
return render(request, 'login.html', {
'msg': '账号密码错误'})
else:
#登录失败
return render(request, 'login.html', {
'msg': '请输入正确的账号密码'})
- Recommend favorite movies
def like(request):
user_id = request.GET.get('user_id')
#获取当前用户
my_user = models.User.objects.get(id=user_id)
global global_model
model = global_model
print('-------正在推荐--------', user_id)
list_like_movies_names, list_like_movies_ids = model.recommend_your_movie(int(user_id))
print('你喜欢的电影:', list_like_movies_names)
print('你喜欢的电影id:', list_like_movies_ids)
# 你喜欢的电影
list_dict_like = []
for i in list_like_movies_names[:6]:
list_dict_like.append(utils.movie_dic(i))
for i in range(len(list_dict_like)):
# list_dict_like[i]['movie_id'] = list_like_movies_ids[i]
list_dict_like[i]['movie_id'] = int(list_like_movies_ids[i]) #把字典中的id转int类型,便于前端if判断
print('相似电影列表:', list_dict_like)
context = {
}
context['list_dict_like'] = list_dict_like
context['my_user'] = my_user #返回当前用户,在前端页面显示当前登陆的用户名
return render(request, 'like.html', context)
- Recommend movies of the same type and movies that people who have watched like
def index(request):
#获取电影id
movie_id = request.GET.get('movie_id')
global global_model
model = global_model
print('-------正在推荐--------', movie_id)
#choice_movie_name 选择的电影名称
#list_same_movies_names 相似的电影名称
#list_pepole_like_movies_names 喜欢这个电影的人还喜欢的电影名称
#list_same_movies_ids 相似的电影id
#list_pepole_like_movies_ids 喜欢这个电影的人还喜欢的电影id
#和recommend_by_movie方法的返回值一一对应
choice_movie_name, list_same_movies_names, list_pepole_like_movies_names, list_same_movies_ids, list_pepole_like_movies_ids = model.recommend_by_movie(
int(movie_id))
print('选择电影:', choice_movie_name)
print('相似的电影:', list_same_movies_names)
print('喜欢这个电影的人还喜欢:', list_pepole_like_movies_names)
print('相似的电影id:', list_same_movies_ids)
print('喜欢这个电影的人还喜欢id:', list_pepole_like_movies_ids)
#选择的电影
list_dict_choice = []
for i in choice_movie_name:
list_dict_choice.append(utils.movie_dic(i))
list_dict_choice[0]['movie_id'] = movie_id
print('选择电影列表:', list_dict_choice)
# 相似的电影
list_dict_same = []
# for i in list_same_movies_names[:3]: #最多显示3部电影
for i in list_same_movies_names:
list_dict_same.append(utils.movie_dic(i))
for i in range(len(list_dict_same)):
# list_dict_same[i]['movie_id'] = list_same_movies_ids[i]
list_dict_same[i]['movie_id'] = int(list_same_movies_ids[i]) #把字典中的id转int类型,便于前端if判断
print('相似电影列表:', list_dict_same)
# 看过的用户还喜欢的电影
list_dict_otherlike = []
for i in list_pepole_like_movies_names:
list_dict_otherlike.append(utils.movie_dic(i))
for i in range(len(list_dict_otherlike)):
# list_dict_otherlike[i]['movie_id'] = list_pepole_like_movies_ids[i]
list_dict_otherlike[i]['movie_id'] = int(list_pepole_like_movies_ids[i]) #把字典中的id转int类型,便于前端if判断
print('喜欢这个电影的人还喜欢列表:', list_dict_otherlike)
context = {
}
context['list_dict_choice'] = list_dict_choice
context['list_dict_same'] = list_dict_same
context['list_dict_otherlike'] = list_dict_otherlike
return render(request, 'index.html', context)