Recommendtion system in tflearn?

I have been trying to make lesson4 recommendation system in tflearn but my code gets stuck with the following error:

import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.embedding_ops import embedding
from tflearn.layers.merge_ops import merge
from tflearn.layers.core import input_data, dropout, fully_connected , flatten
from tflearn.layers.estimator import regression

import matplotlib as plt
import numpy as np
import pandas as pd
import os

path = "/home/payas/dl_box/data/movielens/ml-latest-small/"
model_path = "/home/payas/dl_box/saved_models/"
if not os.path.exists(model_path): os.mkdir(model_path)

ratings = pd.read_csv(path+'ratings.csv')

movie_names = pd.read_csv(path+'movies.csv').set_index('movieId')['title'].to_dict()
users = ratings.userId.unique()
movies = ratings.movieId.unique()

userid2idx = {o:i for i,o in enumerate(users)}
movieid2idx = {o:i for i,o in enumerate(movies)}

ratings.movieId = ratings.movieId.apply(lambda x: movieid2idx[x])
ratings.userId = ratings.userId.apply(lambda x: userid2idx[x])

n_users = ratings.userId.nunique()
n_movies = ratings.movieId.nunique()

msk = np.random.rand(len(ratings)) < 0.8

trn = ratings[msk]
val = ratings[~msk]

n_factors = 50

user_in = input_data(shape = [None,1], name='user_in')
u = embedding(user_in,n_users, n_factors,validate_indices=False, weights_init='xavier', trainable=True, restore=True, reuse=False, scope=None, name='user_Embedding')
movies_in = input_data(shape = [None,1], name='movies_in')
m = embedding(movies_in,n_users, n_factors,validate_indices=False, weights_init='xavier', trainable=True, restore=True, reuse=False, scope=None, name='movies_Embedding')

x = merge([u,m], "concat", axis=1, name='input')
x = flatten(x, name ="input" )

net = fully_connected(x, 1024, activation='relu')
net = dropout(net, 0.8)
net = fully_connected(net, 1, activation='relu')
net = regression(net, optimizer='adam', learning_rate=0.003, loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(net,tensorboard_dir="/tmp/tflearn_logs/",tensorboard_verbose=3)[trn.userId, trn.movieId], {'targets': trn.rating}, n_epoch=2, validation_set=( [val.userId, val.movieId], {'targets': val.rating}),
snapshot_step=500, show_metric=True, run_id='movielens')

ValueError Traceback (most recent call last)
in ()
9[trn.userId.T, trn.movieId], {‘targets’: trn.rating}, n_epoch=2, validation_set=([val.userId, val.movieId], {‘targets’: val.rating}),
—> 10 snapshot_step=500, show_metric=True, run_id=‘movielens’)

/home/payas/anaconda3/lib/python3.6/site-packages/tflearn/models/ in fit(self, X_inputs, Y_targets, n_epoch, validation_set, show_metric, batch_size, shuffle, snapshot_epoch, snapshot_step, excl_trainops, validation_batch_size, run_id, callbacks)
214 excl_trainops=excl_trainops,
215 run_id=run_id,
–> 216 callbacks=callbacks)
218 def fit_batch(self, X_inputs, Y_targets):

/home/payas/anaconda3/lib/python3.6/site-packages/tflearn/helpers/ in fit(self, feed_dicts, n_epoch, val_feed_dicts, show_metric, snapshot_step, snapshot_epoch, shuffle_all, dprep_dict, daug_dict, excl_trainops, run_id, callbacks)
337 (bool(self.best_checkpoint_path) | snapshot_epoch),
338 snapshot_step,
–> 339 show_metric)
341 # Update training state

/home/payas/anaconda3/lib/python3.6/site-packages/tflearn/helpers/ in _train(self, training_step, snapshot_epoch, snapshot_step, show_metric)
816 tflearn.is_training(True, session=self.session)
817 _, train_summ_str =[self.train, self.summ_op],
–> 818 feed_batch)
820 # Retrieve loss value from summary string

/home/payas/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/ in run(self, fetches, feed_dict, options, run_metadata)
893 try:
894 result = self._run(None, fetches, feed_dict, options_ptr,
–> 895 run_metadata_ptr)
896 if run_metadata:
897 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/home/payas/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/ in _run(self, handle, fetches, feed_dict, options, run_metadata)
1098 'Cannot feed value of shape %r for Tensor %r, '
1099 ‘which has shape %r’
-> 1100 % (np_val.shape,, str(subfeed_t.get_shape())))
1101 if not self.graph.is_feedable(subfeed_t):
1102 raise ValueError(‘Tensor %s may not be fed.’ % subfeed_t)

ValueError: Cannot feed value of shape (64,) for Tensor ‘user_in/X:0’, which has shape ‘(?, 64)’

Please Help to rectify problems in my code.
Thanks :slight_smile: