We will use the RBM class created in the previous recipe. Let's define our RBM network; the number of visible units will be the number of movies, in our case, 3,883 (movies_df is the data frame containing data from the movies.dat file):
m = len(movies_df) # Number of visible units n = 20 # Number of Hidden units recommender = rbm.RBM(m,n)
We created a list, trX, of about 1,000 users with their normalized movie ratings usingPandas merge and groupby commands. The list is of size 1000 x 3883. We use this to train our RBM:
Xtrain = np.array(trX) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) recommender.set_session(sess) err = recommender.fit(Xtrain, epochs=10)
The cross-logit error per epoch decreases:
The network is now trained; we use it get the recommendation for a random user with index 150 (it could be any existing user):
user_index = 150 x = np.array([Xtrain[user_index, :]]) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) recommender.set_session(sess) out = recommender.reconstruct(x.astype(np.float32))
The result is merged with the existing data frame and we can see the recommendation score for this user: