赞
踩
本文使用文本卷积神经网络,并使用自己的电视节目数据集完成电影推荐的任务。
本文在参考了 https://blog.csdn.net/chengcheng1394/article/details/78820529 的基础上,采用自己的数据集对代码进行修改,终于运行出来了!不过对于神经网络是如何搭建的,具体实现原理还是不是很懂的!
需要安装TensorFlow1.0,Python3.5
模型设计:图片来自https://blog.csdn.net/chengcheng1394/article/details/78820529
跟上图不同的是,本文采用的数据集没有用户属性中的性别,年龄和职业编号,仅有用户号。
电影id和用户id以及评分数据都不需要进行转换,但电影名称和节目类型的数据需要将其转为对应的数字,不然没法处理。
文本卷积神经网络的图如下,
图片来自Kim Yoon的论文:Convolutional Neural Networks for Sentence Classification
http://www.wildml.com/2015/11/understanding-convolutional-neural-networks-for-nlp/
- # -*- coding: utf-8 -*-
-
- import pandas as pd
- from sklearn.model_selection import train_test_split
- import numpy as np
- from collections import Counter
- import tensorflow as tf
-
- import os
- import pickle
- from tensorflow.python.ops import math_ops
- from urllib.request import urlretrieve
- from os.path import isfile, isdir
- from tqdm import tqdm
-
-
- def load_data():
- """
- Load Dataset from File
- """
-
- os.chdir('E:/广电大数据营销推荐项目案例/数据清洗/电视节目信息数据预处理')
-
-
-
- # # #读取User数据
- users = pd.read_table('./wordsbag/dataprocess/data/week/mydata/data1_users.csv', sep=',', header='infer', engine = 'python')
- users_orig = users.values
-
- #读取Movie数据集
- # movies_title = ['MovieID', 'Title', 'Genres']
- movies = pd.read_table('./wordsbag/dataprocess/data/week/mydata/data1_tv.csv', sep=',', header='infer', engine = 'python')
- movies = movies.filter(regex='program_id|program_title|genres_good')
- movies_orig = movies.values
-
- #电影类型转数字字典
- genres_set = set()
- for val in movies['genres_good'].str.split('/'):
- genres_set.update(val)
-
- genres_set.add('<PAD>')
- genres2int = {val:ii for ii, val in enumerate(genres_set)}
-
- #将电影类型转成等长数字列表,长度是18
- genres_count = 18
- genres_map = {val:[genres2int[row] for row in val.split('/')] for ii,val in enumerate(set(movies['genres_good']))}
-
- for key in genres_map:
- for cnt in range(genres_count - len(genres_map[key])):
- genres_map[key].insert(len(genres_map[key]) + cnt,genres2int['<PAD>'])
-
- movies['genres_good'] = movies['genres_good'].map(genres_map)
-
- #电影Title转数字字典
- title_set = set()
- for val in movies['program_title'].str.split():
- title_set.update(val)
-
- title_set.add('<PAD>')
- title2int = {val:ii for ii, val in enumerate(title_set)}
-
- #将电影Title转成等长数字列表,长度是15
- title_count = 15
- title_map = {val:[title2int[row] for row in val.split()] for ii,val in enumerate(set(movies['program_title']))}
-
- for key in title_map:
- for cnt in range(title_count - len(title_map[key])):
- title_map[key].insert(len(title_map[key]) + cnt,title2int['<PAD>'])
-
- movies['program_title'] = movies['program_title'].map(title_map)
-
-
- #读取评分数据集,评分为1-10分
- ratings = pd.read_table('./wordsbag/dataprocess/data/week/mydata/data1_ratings.csv', sep=',', header='infer', engine = 'python')
- ratings = ratings.filter(regex='user_id|program_id|rating')
-
- #合并表
- data = pd.merge(ratings,movies)
-
- #将数据分成X和y两张表
- target_fields = ['rating']
- features_pd, targets_pd = data.drop(target_fields, axis=1), data[target_fields]
-
- features = features_pd.values
- targets_values = targets_pd.values
-
-
-
- return title_count, title_set, genres_count, genres2int, features, targets_values, ratings, users, movies, data, movies_orig,users_orig
-
-
- title_count, title_set, genres_count, genres2int, features, targets_values, ratings, users, movies, data, movies_orig,users_orig= load_data()
-
- pickle.dump((title_count, title_set, genres_count, genres2int, features, targets_values, ratings, movies, data, movies_orig,users_orig), open('./wordsbag/dataprocess/data/week/mydata/preprocess.p', 'wb'))
-
-
-
- # 预处理后
-
- #users.head()
-
- movies.head()
-
- movies.values[0]
-
- title_count, title_set, genres_count, genres2int, features, targets_values, ratings, movies, data, movies_orig,users_orig = pickle.load(open('./wordsbag/dataprocess/data/week/mydata/preprocess.p', mode='rb'))
-
-
- import tensorflow as tf
- import os
- import pickle
-
- def save_params(params):
- """
- Save parameters to file
- """
- pickle.dump(params, open('./wordsbag/dataprocess/data/week/mydata/params.p', 'wb'))
-
-
- def load_params():
- """
- Load parameters from file
- """
- return pickle.load(open('./wordsbag/dataprocess/data/week/mydata/params.p', mode='rb'))
-
-
- # 编码实现
-
- #嵌入矩阵的维度
- embed_dim = 32
- #用户ID个数
- uid_max = max(features.take(0,1)) + 1 # 1966+1=1967
- print(uid_max)
-
- #电影ID个数
- movie_id_max = max(features.take(1,1)) + 1 # 995+1 = 996
- print(movie_id_max)
- #电影类型个数
- movie_categories_max = max(genres2int.values()) + 1 # 104为什么有这么多重复的个数
- print(movie_categories_max)
- #电影名单词个数
- movie_title_max = len(title_set) # 501+1=502
- print(movie_title_max)
-
- #对电影类型嵌入向量做加和操作的标志,考虑过使用mean做平均,但是没实现mean
- combiner = "sum"
-
- #电影名长度
- sentences_size = title_count # = 15
- #文本卷积滑动窗口,分别滑动2, 3, 4, 5个单词
- window_sizes = {2, 3, 4, 5}
- #文本卷积核数量
- filter_num = 8
-
- #电影ID转下标的字典,数据集中电影ID跟下标不一致,比如第5行的数据电影ID不一定是5
- movieid2idx = {val[0]:i for i, val in enumerate(movies.values)}
- print(movieid2idx)
-
-
-
- # 超参
- # Number of Epochs
- num_epochs = 5
- # Batch Size
- batch_size = 256
-
- dropout_keep = 0.5
- # Learning Rate
- learning_rate = 0.0001
- # Show stats for every n number of batches
- show_every_n_batches = 20
-
- save_dir = './wordsbag/dataprocess/data/week/mydata/save2'
-
-
-
- # 输入
- def get_inputs():
- uid = tf.placeholder(tf.int32, [None, 1], name="uid")
-
- movie_id = tf.placeholder(tf.int32, [None, 1], name="movie_id")
- movie_categories = tf.placeholder(tf.int32, [None, 18], name="movie_categories")
- movie_titles = tf.placeholder(tf.int32, [None, 15], name="movie_titles")
-
- targets = tf.placeholder(tf.int32, [None, 1], name="targets")
- LearningRate = tf.placeholder(tf.float32, name = "LearningRate")
- dropout_keep_prob = tf.placeholder(tf.float32, name = "dropout_keep_prob")
- return uid, movie_id, movie_categories, movie_titles, targets, LearningRate, dropout_keep_prob
-
-
- # 构建神经网络
- def get_user_embedding(uid):
- with tf.name_scope("user_embedding"):
- uid_embed_matrix = tf.Variable(tf.random_uniform([uid_max, embed_dim], -1, 1), name = "uid_embed_matrix")
- uid_embed_layer = tf.nn.embedding_lookup(uid_embed_matrix, uid, name = "uid_embed_layer")
- return uid_embed_layer
-
-
- #将User的嵌入矩阵一起全连接生成User的特征
- def get_user_feature_layer(uid_embed_layer):
- with tf.name_scope("user_fc"):
- #第一层全连接
- uid_fc_layer = tf.layers.dense(uid_embed_layer, embed_dim, name = "uid_fc_layer", activation=tf.nn.relu)
-
- #第二层全连接
- user_combine_layer = tf.concat([uid_fc_layer], 2) #(?, 1, 128)
- user_combine_layer = tf.contrib.layers.fully_connected(user_combine_layer, 200, tf.tanh) #(?, 1, 200)
-
- user_combine_layer_flat = tf.reshape(user_combine_layer, [-1, 200])
- return user_combine_layer, user_combine_layer_flat
-
-
-
- #定义Movie ID的嵌入矩阵
-
- def get_movie_id_embed_layer(movie_id):
- with tf.name_scope("movie_embedding"):
- movie_id_embed_matrix = tf.Variable(tf.random_uniform([movie_id_max, embed_dim], -1, 1), name = "movie_id_embed_matrix")
- movie_id_embed_layer = tf.nn.embedding_lookup(movie_id_embed_matrix, movie_id, name = "movie_id_embed_layer")
- return movie_id_embed_layer
-
-
- #对电影类型的多个嵌入向量做加和
- def get_movie_categories_layers(movie_categories):
- with tf.name_scope("movie_categories_layers"):
- movie_categories_embed_matrix = tf.Variable(tf.random_uniform([movie_categories_max, embed_dim], -1, 1), name = "movie_categories_embed_matrix")
- movie_categories_embed_layer = tf.nn.embedding_lookup(movie_categories_embed_matrix, movie_categories, name = "movie_categories_embed_layer")
- if combiner == "sum":
- movie_categories_embed_layer = tf.reduce_sum(movie_categories_embed_layer, axis=1, keep_dims=True)
- # elif combiner == "mean":
- return movie_categories_embed_layer
-
- # Movie Title的文本卷积网络实现
-
- def get_movie_cnn_layer(movie_titles):
- #从嵌入矩阵中得到电影名对应的各个单词的嵌入向量
- with tf.name_scope("movie_embedding"):
- movie_title_embed_matrix = tf.Variable(tf.random_uniform([movie_title_max, embed_dim], -1, 1), name = "movie_title_embed_matrix")
- movie_title_embed_layer = tf.nn.embedding_lookup(movie_title_embed_matrix, movie_titles, name = "movie_title_embed_layer")
- movie_title_embed_layer_expand = tf.expand_dims(movie_title_embed_layer, -1)
-
- #对文本嵌入层使用不同尺寸的卷积核做卷积和最大池化
- pool_layer_lst = []
- for window_size in window_sizes:
- with tf.name_scope("movie_txt_conv_maxpool_{}".format(window_size)):
- filter_weights = tf.Variable(tf.truncated_normal([window_size, embed_dim, 1, filter_num],stddev=0.1),name = "filter_weights")
- filter_bias = tf.Variable(tf.constant(0.1, shape=[filter_num]), name="filter_bias")
-
- conv_layer = tf.nn.conv2d(movie_title_embed_layer_expand, filter_weights, [1,1,1,1], padding="VALID", name="conv_layer")
- relu_layer = tf.nn.relu(tf.nn.bias_add(conv_layer,filter_bias), name ="relu_layer")
-
- maxpool_layer = tf.nn.max_pool(relu_layer, [1,sentences_size - window_size + 1 ,1,1], [1,1,1,1], padding="VALID", name="maxpool_layer")
- pool_layer_lst.append(maxpool_layer)
-
- #Dropout层
- with tf.name_scope("pool_dropout"):
- pool_layer = tf.concat(pool_layer_lst, 3, name ="pool_layer")
- max_num = len(window_sizes) * filter_num
- pool_layer_flat = tf.reshape(pool_layer , [-1, 1, max_num], name = "pool_layer_flat")
-
- dropout_layer = tf.nn.dropout(pool_layer_flat, dropout_keep_prob, name = "dropout_layer")
- return pool_layer_flat, dropout_layer
-
- # 将Movie的各个层一起做全连接
- def get_movie_feature_layer(movie_id_embed_layer, movie_categories_embed_layer, dropout_layer):
- with tf.name_scope("movie_fc"):
- #第一层全连接
- movie_id_fc_layer = tf.layers.dense(movie_id_embed_layer, embed_dim, name = "movie_id_fc_layer", activation=tf.nn.relu)
- movie_categories_fc_layer = tf.layers.dense(movie_categories_embed_layer, embed_dim, name = "movie_categories_fc_layer", activation=tf.nn.relu)
-
- #第二层全连接
- movie_combine_layer = tf.concat([movie_id_fc_layer, movie_categories_fc_layer, dropout_layer], 2) #(?, 1, 96)
- movie_combine_layer = tf.contrib.layers.fully_connected(movie_combine_layer, 200, tf.tanh) #(?, 1, 200)
-
- movie_combine_layer_flat = tf.reshape(movie_combine_layer, [-1, 200])
- return movie_combine_layer, movie_combine_layer_flat
-
-
-
- #构建计算图
- tf.reset_default_graph()
- train_graph = tf.Graph()
- with train_graph.as_default():
- #获取输入占位符
- uid, movie_id, movie_categories, movie_titles, targets, lr, dropout_keep_prob = get_inputs()
- #获取User的4个嵌入向量
- uid_embed_layer = get_user_embedding(uid)
- #得到用户特征
- user_combine_layer, user_combine_layer_flat = get_user_feature_layer(uid_embed_layer)
- #获取电影ID的嵌入向量
- movie_id_embed_layer = get_movie_id_embed_layer(movie_id)
- #获取电影类型的嵌入向量
- movie_categories_embed_layer = get_movie_categories_layers(movie_categories)
- #获取电影名的特征向量
- pool_layer_flat, dropout_layer = get_movie_cnn_layer(movie_titles)
- #得到电影特征
- movie_combine_layer, movie_combine_layer_flat = get_movie_feature_layer(movie_id_embed_layer,
- movie_categories_embed_layer,
- dropout_layer)
- #计算出评分,要注意两个不同的方案,inference的名字(name值)是不一样的,后面做推荐时要根据name取得tensor
- with tf.name_scope("inference"):
- #将用户特征和电影特征作为输入,经过全连接,输出一个值的方案
- #简单的将用户特征和电影特征做矩阵乘法得到一个预测评分
- inference = tf.reduce_sum(user_combine_layer_flat * movie_combine_layer_flat, axis=1)
- inference = tf.expand_dims(inference, axis=1)
-
- with tf.name_scope("loss"):
- # MSE损失,将计算值回归到评分
- cost = tf.losses.mean_squared_error(targets, inference )
- loss = tf.reduce_mean(cost)
- # 优化损失
- # train_op = tf.train.AdamOptimizer(lr).minimize(loss) #cost
- global_step = tf.Variable(0, name="global_step", trainable=False)
- optimizer = tf.train.AdamOptimizer(lr)
- gradients = optimizer.compute_gradients(loss) #cost
- train_op = optimizer.apply_gradients(gradients, global_step=global_step)
-
-
- # 取得batch
- def get_batches(Xs, ys, batch_size):
- for start in range(0, len(Xs), batch_size):
- end = min(start + batch_size, len(Xs))
- yield Xs[start:end], ys[start:end]
-
-
-
-
- # 训练网络
- #%matplotlib inline
- #%config InlineBackend.figure_format = 'retina'
- import matplotlib.pyplot as plt
- import time
- import datetime
-
- losses = {'train':[], 'test':[]}
-
-
- with tf.Session(graph=train_graph) as sess:
-
- #搜集数据给tensorBoard用
- # Keep track of gradient values and sparsity
- grad_summaries = []
- for g, v in gradients:
- if g is not None:
- grad_hist_summary = tf.summary.histogram("{}/grad/hist".format(v.name.replace(':', '_')), g)
- sparsity_summary = tf.summary.scalar("{}/grad/sparsity".format(v.name.replace(':', '_')), tf.nn.zero_fraction(g))
- grad_summaries.append(grad_hist_summary)
- grad_summaries.append(sparsity_summary)
- grad_summaries_merged = tf.summary.merge(grad_summaries)
-
- # Output directory for models and summaries
- timestamp = str(int(time.time()))
- out_dir = os.path.abspath(os.path.join(os.path.curdir, "runs", timestamp))
- print("Writing to {}\n".format(out_dir))
-
- # Summaries for loss and accuracy
- loss_summary = tf.summary.scalar("loss", loss)
-
- # Train Summaries
- train_summary_op = tf.summary.merge([loss_summary, grad_summaries_merged])
- train_summary_dir = os.path.join(out_dir, "summaries", "train")
- train_summary_writer = tf.summary.FileWriter(train_summary_dir, sess.graph)
-
- # Inference summaries
- inference_summary_op = tf.summary.merge([loss_summary])
- inference_summary_dir = os.path.join(out_dir, "summaries", "inference")
- inference_summary_writer = tf.summary.FileWriter(inference_summary_dir, sess.graph)
-
- sess.run(tf.global_variables_initializer())
- saver = tf.train.Saver()
- for epoch_i in range(num_epochs):
-
- #将数据集分成训练集和测试集,随机种子不固定
- train_X,test_X, train_y, test_y = train_test_split(features,
- targets_values,
- test_size = 0.2,
- random_state = 0)
-
- train_batches = get_batches(train_X, train_y, batch_size)
- test_batches = get_batches(test_X, test_y, batch_size)
-
- #训练的迭代,保存训练损失
- for batch_i in range(len(train_X) // batch_size):
- x, y = next(train_batches)
-
- categories = np.zeros([batch_size, 18])
- for i in range(batch_size):
- categories[i] = x.take(3,1)[i]
- # categories[i] = x.take(6,1)[i]
-
- titles = np.zeros([batch_size, sentences_size])
- for i in range(batch_size):
- titles[i] = x.take(2,1)[i]
-
- feed = {
- uid: np.reshape(x.take(0,1), [batch_size, 1]),
-
- movie_id: np.reshape(x.take(1,1), [batch_size, 1]),
- movie_categories: categories, #x.take(3,1)
- movie_titles: titles, #x.take(2,1)
-
- targets: np.reshape(y, [batch_size, 1]),
- dropout_keep_prob: dropout_keep, #dropout_keep
- lr: learning_rate}
-
- step, train_loss, summaries, _ = sess.run([global_step, loss, train_summary_op, train_op], feed) #cost
- losses['train'].append(train_loss)
- train_summary_writer.add_summary(summaries, step) #
-
- # Show every <show_every_n_batches> batches
- if (epoch_i * (len(train_X) // batch_size) + batch_i) % show_every_n_batches == 0:
- time_str = datetime.datetime.now().isoformat()
- print('{}: Epoch {:>3} Batch {:>4}/{} train_loss = {:.3f}'.format(
- time_str,
- epoch_i,
- batch_i,
- (len(train_X) // batch_size),
- train_loss))
-
- #使用测试数据的迭代
- for batch_i in range(len(test_X) // batch_size):
- x, y = next(test_batches)
-
- categories = np.zeros([batch_size, 18])
- for i in range(batch_size):
- categories[i] = x.take(3,1)[i] #x.take(3,1)中的3要根据自己的数据去做修改
-
- titles = np.zeros([batch_size, sentences_size])
- for i in range(batch_size):
- titles[i] = x.take(2,1)[i] #x.take(2,1)中的2要根据自己的数据去做修改
-
- feed = {
- uid: np.reshape(x.take(0,1), [batch_size, 1]),
-
- movie_id: np.reshape(x.take(1,1), [batch_size, 1]),
- movie_categories: categories, #x.take(3,1)
- movie_titles: titles, #x.take(2,1)
-
- targets: np.reshape(y, [batch_size, 1]),
- dropout_keep_prob: 1,
- lr: learning_rate}
-
- step, test_loss, summaries = sess.run([global_step, loss, inference_summary_op], feed) #cost
-
- #保存测试损失
- losses['test'].append(test_loss)
- inference_summary_writer.add_summary(summaries, step) #
-
- time_str = datetime.datetime.now().isoformat()
- if (epoch_i * (len(test_X) // batch_size) + batch_i) % show_every_n_batches == 0:
- print('{}: Epoch {:>3} Batch {:>4}/{} test_loss = {:.3f}'.format(
- time_str,
- epoch_i,
- batch_i,
- (len(test_X) // batch_size),
- test_loss))
-
- # Save Model
- saver.save(sess, save_dir) #, global_step=epoch_i
- print('Model Trained and Saved')
-
- # 保存参数
- save_params((save_dir))
-
- load_dir = load_params()
-
-
- # 显示训练Loss
- plt.plot(losses['train'], label='Training loss')
- plt.legend()
- _ = plt.ylim()
-
- # 显示测试Loss
- plt.plot(losses['test'], label='Test loss')
- plt.legend()
- _ = plt.ylim()
-
-
-
-
- # 获取Tensors
- def get_tensors(loaded_graph):
-
- uid = loaded_graph.get_tensor_by_name("uid:0")
-
- movie_id = loaded_graph.get_tensor_by_name("movie_id:0")
- movie_categories = loaded_graph.get_tensor_by_name("movie_categories:0")
- movie_titles = loaded_graph.get_tensor_by_name("movie_titles:0")
-
- targets = loaded_graph.get_tensor_by_name("targets:0")
- dropout_keep_prob = loaded_graph.get_tensor_by_name("dropout_keep_prob:0")
- lr = loaded_graph.get_tensor_by_name("LearningRate:0")
- #两种不同计算预测评分的方案使用不同的name获取tensor inference
- # inference = loaded_graph.get_tensor_by_name("inference/inference/BiasAdd:0")
- inference = loaded_graph.get_tensor_by_name("inference/ExpandDims:0") # 之前是MatMul:0 因为inference代码修改了 这里也要修改 感谢网友 @清歌 指出问题
- movie_combine_layer_flat = loaded_graph.get_tensor_by_name("movie_fc/Reshape:0")
- user_combine_layer_flat = loaded_graph.get_tensor_by_name("user_fc/Reshape:0")
- return uid, movie_id, movie_categories, movie_titles, targets, lr, dropout_keep_prob, inference, movie_combine_layer_flat, user_combine_layer_flat
-
-
-
- #指定用户和电影进行评分
- def rating_movie(user_id_val, movie_id_val):
- loaded_graph = tf.Graph() #
- with tf.Session(graph=loaded_graph) as sess: #
- # Load saved model
- loader = tf.train.import_meta_graph(load_dir + '.meta')
- loader.restore(sess, load_dir)
-
- # Get Tensors from loaded model
- uid, movie_id, movie_categories, movie_titles, targets, lr, dropout_keep_prob, inference,_, __ = get_tensors(loaded_graph) #loaded_graph
-
- categories = np.zeros([1, 18])
- categories[0] = movies.values[movieid2idx[movie_id_val]][2]
-
- titles = np.zeros([1, sentences_size])
- titles[0] = movies.values[movieid2idx[movie_id_val]][1]
-
- feed = {
- uid: np.reshape(users.values[user_id_val-1][0], [1, 1]),
-
- movie_id: np.reshape(movies.values[movieid2idx[movie_id_val]][0], [1, 1]),
- movie_categories: categories, #x.take(6,1)
- movie_titles: titles, #x.take(5,1)
- dropout_keep_prob: 1}
-
- # Get Prediction
- inference_val = sess.run([inference], feed)
-
- return (inference_val)
-
- rating_movie(23, 1)
-
-
-
- #生成Movie特征矩阵
- loaded_graph = tf.Graph() #
- movie_matrics = []
- with tf.Session(graph=loaded_graph) as sess: #
- # Load saved model
- loader = tf.train.import_meta_graph(load_dir + '.meta')
- loader.restore(sess, load_dir)
-
- # Get Tensors from loaded model
- uid, movie_id, movie_categories, movie_titles, targets, lr, dropout_keep_prob, _, movie_combine_layer_flat, __ = get_tensors(loaded_graph) #loaded_graph
-
- for item in movies.values:
- categories = np.zeros([1, 18])
- categories[0] = item.take(2)
-
- titles = np.zeros([1, sentences_size])
- titles[0] = item.take(1)
-
- feed = {
- movie_id: np.reshape(item.take(0), [1, 1]),
- movie_categories: categories, #x.take(3,1)
- movie_titles: titles, #x.take(2,1)
- dropout_keep_prob: 1}
-
- movie_combine_layer_flat_val = sess.run([movie_combine_layer_flat], feed)
- movie_matrics.append(movie_combine_layer_flat_val)
-
- pickle.dump((np.array(movie_matrics).reshape(-1, 200)), open('movie_matrics.p', 'wb'))
- movie_matrics = pickle.load(open('movie_matrics.p', mode='rb'))
-
- #生成User特征矩阵
- loaded_graph = tf.Graph() #
- users_matrics = []
- with tf.Session(graph=loaded_graph) as sess: #
- # Load saved model
- loader = tf.train.import_meta_graph(load_dir + '.meta')
- loader.restore(sess, load_dir)
-
- # Get Tensors from loaded model
- uid, movie_id, movie_categories, movie_titles, targets, lr, dropout_keep_prob, _, __,user_combine_layer_flat = get_tensors(loaded_graph) #loaded_graph
-
- for item in users.values:
-
- feed = {
- uid: np.reshape(item.take(0), [1, 1]),
- dropout_keep_prob: 1}
-
- user_combine_layer_flat_val = sess.run([user_combine_layer_flat], feed)
- users_matrics.append(user_combine_layer_flat_val)
-
- pickle.dump((np.array(users_matrics).reshape(-1, 200)), open('./wordsbag/dataprocess/data/week/mydata/users_matrics.p', 'wb'))
- users_matrics = pickle.load(open('./wordsbag/dataprocess/data/week/mydata/users_matrics.p', mode='rb'))
-
-
- users_matrics = pickle.load(open('./wordsbag/dataprocess/data/week/mydata/users_matrics.p', mode='rb'))
-
- #
- #
- def recommend_same_type_movie(movie_id_val, top_k = 20):
-
- loaded_graph = tf.Graph() #
- with tf.Session(graph=loaded_graph) as sess: #
- # Load saved model
- loader = tf.train.import_meta_graph(load_dir + '.meta')
- loader.restore(sess, load_dir)
-
- norm_movie_matrics = tf.sqrt(tf.reduce_sum(tf.square(movie_matrics), 1, keep_dims=True))
- normalized_movie_matrics = movie_matrics / norm_movie_matrics
-
- #推荐同类型的电影
- probs_embeddings = (movie_matrics[movieid2idx[movie_id_val]]).reshape([1, 200])
- probs_similarity = tf.matmul(probs_embeddings, tf.transpose(normalized_movie_matrics))
- sim = (probs_similarity.eval())
- # results = (-sim[0]).argsort()[0:top_k]
- # print(results)
-
- print("您看的电影是:{}".format(movies_orig[movieid2idx[movie_id_val]]))
- print("以下是给您的推荐:")
- p = np.squeeze(sim)
- p[np.argsort(p)[:-top_k]] = 0
- p = p / np.sum(p)
- results = set()
- while len(results) != 10:
- c = np.random.choice(501, 1, p=p)[0]
- results.add(c)
- for val in (results):
- print(val)
- print(movies_orig[val])
-
- return results
-
- recommend_same_type_movie(20, 10)
-
-
-
-
-
-
-
调用recommend_your_favorite_movie(用户收看过的节目id,推荐个数N) ,可以输入和用户收看节目标签类似的节目。
recommend_your_favorite_movie(20, 10)
运行结果如下:
- 您看的电影是:[20 '欢乐颂' '剧情 / 爱情']
- 以下是给您的推荐:
- 257
- [401 '生活启示录' '剧情 / 爱情']
- 193
- [275 '朋友圈' '剧情 / 爱情']
- 262
- [409 '平凡岁月' '剧情 / 爱情']
- 426
- [770 '公主夜游记' '剧情 / 爱情']
- 460
- [867 '观音山' '剧情 / 爱情']
- 46
- [75 '人间至味是清欢' '剧情 / 爱情']
- 303
- [498 '上海滩' '剧情 / 爱情']
- 16
- [20 '欢乐颂' '剧情 / 爱情']
- 308
- [505 '逆光飞翔' '剧情 / 爱情']
- 86
- [134 '向幸福出发' '剧情 / 爱情']
基于深度学习的推荐算法研究,路漫漫其修远兮。。。。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。