Movie recommender model doesn't converge
@pjpratik. I am trying for a solution.
@orlean12 Pls Review whether it is appropriate or not.

One possible issue is related to the embedding layer. The code uses an embedding layer to encode the user input, but the embedding layer is not defined in the code. It is possible that the Embedding layer is defined outside the provided code, but it is unclear. Another possible issue is the input shape of the user input. The shape is defined as (1,), which may not be appropriate for the task at hand.

Another possible issue is related to the hyperparameters of the model, such as the number of hidden layers, the number of units in each layer, and the dropout rate. It is unclear whether the hyperparameters are optimized for the task at hand.

Based on the provided code, there are several issues that might be causing the high loss and low accuracy:

No preprocessing of input data: Before feeding the input data into the model, it's important to preprocess it to make it suitable for the model. In this case, the user's and movie data are being used as is, without any preprocessing. You might want to consider normalizing or scaling the input data.

No regularization: The model seems to have a lot of capacity and could be overfitting to the training data. To prevent this, it's important to include regularization techniques such as L1/L2 regularization, dropout or batch normalization layers.

Loss function: It's important to choose the right loss function for the problem at hand. In this case, since it's a regression problem, using mean squared error (MSE) as the loss function might be more appropriate.

Code Which might help:

mean_user = np.mean(users)
std_user = np.std(users)
users_norm = (users - mean_user) / std_user

mean_movies = np.mean(movies, axis=0)
std_movies = np.std(movies, axis=0)
movies_norm = (movies - mean_movies) / std_movies

user_NN = tf.keras.models.Sequential([
    tf.keras.layers.Dense(256, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(num_outputs, activation='linear'),
])

item_NN = tf.keras.models.Sequential([
    tf.keras.layers.Dense(256, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(num_outputs, activation='linear'), 
])
model.compile(optimizer='adam', loss='mse')
 Hi @orlean12 Thanks for reporting the issue. 

In order to expedite the trouble-shooting process, please provide accessible files with colab gist/code snippet to reproduce the issue reported here. That will help better understand and investigate the issue further.

Are you trying to reproduce https://www.tensorflow.org/recommenders/examples/quickstart locally for custom data and hitting problems?

If so, can you please mention what steps have you changed?

Thanks! hello. I tried @NiharJani2002 's input, changing things around in the dataset, and yet I still struggle with low success rates- that is, until I removed the l2.normalize layer for the input vector. after doing so the loss rate had jumped to incrediably low numbers- 0.004 at times. I think that something is bugged with the calculation as that is fairly impossible, and the R2 calculation for this comes out to be -10(also impossible). the hyperparameters of the model should probably be just fine, because changing them did not make any significant diffrence in my loss rates. additionally the model is already regularized, using a MSE function.
I am adding a direct link to my project on colab labratory as with @pjpratik 's suggestion. Feel free to move around and change anything may that be worth changing. I think the problem has to do either with the embedding layer or the model itself.
Anyways, thank you SO SO MUCH to both of you for trying to help my project. I dont know if 'tipping' is allowed or recommended in this website, but if it is i'll donate a bit to both of you as thanks once this thing works.

Link to project(so far) : https://colab.research.google.com/drive/1yIqyRNw0PEcLCx9WSjHiOPyUz_Z5cHV5?usp=sharing
everything beyond the R2 might be bugged. Hi @orlean12 I have run the code provided in Tensorflow 2.11 and observed that loss comes down as the epochs progress. Also, the resultant mse on test set is 0.003 which seems to be good. The r2 score is 0.99 and I am not sure why mean is added to the test pred.  

![image](https://user-images.githubusercontent.com/118897289/224649493-fd4b0125-13e7-4904-b0bc-60ba66adf522.png)

Please find the gist [here](https://colab.research.google.com/gist/pjpratik/b31c05ec01e0d0f9cbc664bba965e3f8/59944.ipynb) and let us know if it helps.

Thank you. This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. Hey @orlean12 you can check out my movie recommendation system built using content based filtering and python
It is truly a working solution
https://github.com/amangupta201/MovieRecommendationSystem/blob/master/main.py This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/59944">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/59944">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Performance

### Have you reproduced the bug with TF nightly?

Yes

### Source

source

### Tensorflow Version

2.8

### Custom Code

Yes

### OS Platform and Distribution

Windows

### Mobile device

_No response_

### Python version

3.9

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current Behaviour?

```shell
I had started to train my Movie Recommender System (content based filtering) in order to guess user ratings for certain movies. However, my loss is incrediably high(7.3) my accuracy is very low(0.03) and the code barely improves- going from 7.357 loss to 7.355 in 50 epochs or so. I have no idea how to fix this problem and I tried many things including adding dropout layers, more layers, changing output and so. I think my problem might be related to the embedding layer?
note:
num_item_features = 21 , and the movies vector is a list of all the attributes of a certain movie. users vector is used in embedding.
```


### Standalone code to reproduce the issue

```shell
num_outputs = 32 #hyperparameter
K= 32 #hyperparameter, size of embedding vector
tf.random.set_seed(1)
user_NN = tf.keras.models.Sequential([
    tf.keras.layers.Dense(256, activation='relu'),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dropout(.2, ),
    tf.keras.layers.Dense(num_outputs, activation='linear'),
  
])

item_NN = tf.keras.models.Sequential([
    tf.keras.layers.Dense(256, activation='relu'),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dropout(.2, ),
    tf.keras.layers.Dense(num_outputs, activation='linear'), 
])

# create the user input and point to the base network
input_user = tf.keras.layers.Input(shape=(1,)) 
u_emb = Embedding(N, K)(input_user) # output is (num_samples, 1, K)
u_emb = Flatten()(u_emb)  # now it's (num_samples, K)

vu = user_NN(u_emb)
vu = tf.linalg.l2_normalize(vu, axis=1)

# create the item input and point to the base network
input_item = tf.keras.layers.Input(shape=(num_item_features))
vm = item_NN(input_item)
vm = tf.linalg.l2_normalize(vm, axis=1) 

# compute the dot product of the two vectors vu and vm
output = tf.keras.layers.Dot(axes=1)([vu, vm])

# specify the inputs and output of the model
model = tf.keras.Model([input_user, input_item], output)

model.summary()


history = model.fit([users, movies], ratings,callbacks=[callback], epochs=1000,batch_size=256,verbose=1,shuffle=True)


num_item_features=movies.shape[1]
users=df3['userId'].to_numpy()
ratings=df3['rating'].to_numpy()
```


### Relevant log output

```shell
Epoch 1/1000
511/511 [==============================] - 8s 12ms/step - loss: 7.5963 - accuracy: 0.0308
Epoch 2/1000
511/511 [==============================] - 8s 16ms/step - loss: 7.5560 - accuracy: 0.0311
Epoch 3/1000
511/511 [==============================] - 6s 12ms/step - loss: 7.5555 - accuracy: 0.0311
Epoch 4/1000
511/511 [==============================] - 8s 15ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 5/1000
511/511 [==============================] - 6s 13ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 6/1000
511/511 [==============================] - 7s 14ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 7/1000
511/511 [==============================] - 7s 13ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 8/1000
511/511 [==============================] - 7s 13ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 9/1000
511/511 [==============================] - 8s 15ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 10/1000
511/511 [==============================] - 6s 12ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 11/1000
511/511 [==============================] - 8s 16ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 12/1000
511/511 [==============================] - 6s 12ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 13/1000
511/511 [==============================] - 8s 16ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 14/1000
511/511 [==============================] - 9s 17ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 15/1000
511/511 [==============================] - 8s 15ms/step - loss: 7.5554 - accuracy: 0.0311
Epoch 16/1000
511/511 [==============================] - 6s 12ms/step - loss: 7.5554 - accuracy: 0.0311
```
</details>