How to do it...

  1. 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)
  1. We created a list, trX, of about 1,000 users with their normalized movie ratings using Pandas 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)
  1. The cross-logit error per epoch decreases:
  1. 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))
  1. The result is merged with the existing data frame and we can see the recommendation score for this user:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.221.116