C3_W2_RecSysNN_Assignment - Finding Similar Items

Can someone explain what is going on in this code:

input_item_m = tf.keras.layers.Input(shape=(num_item_features))    # input layer
vm_m = item_NN(input_item_m)                                       # use the trained item_NN
vm_m = tf.linalg.l2_normalize(vm_m, axis=1)                        # incorporate normalization as was done in the original model
model_m = tf.keras.Model(input_item_m, vm_m)                                
model_m.summary()

The comments mentioned here doesnt seem to explain what is going on, I cant print the values input_item_m and vm_m so difficult to visualize what data is it referring to. This syntax seems to be different than the usual tensorflow model training.

Same applies to this code:

count = 50  # number of movies to display
dim = len(vms)
dist = np.zeros((dim,dim))

for i in range(dim):
    for j in range(dim):
        dist[i,j] = sq_dist(vms[i, :], vms[j, :])
        
m_dist = ma.masked_array(dist, mask=np.identity(dist.shape[0]))  # mask the diagonal

disp = [["movie1", "genres", "movie2", "genres"]]
for i in range(count):
    min_idx = np.argmin(m_dist[i])
    movie1_id = int(item_vecs[i,0])
    movie2_id = int(item_vecs[min_idx,0])
    disp.append( [movie_dict[movie1_id]['title'], movie_dict[movie1_id]['genres'],
                  movie_dict[movie2_id]['title'], movie_dict[movie1_id]['genres']]
               )
table = tabulate.tabulate(disp, tablefmt='html', headers="firstrow")
table

Can someone explain what is happenning here

Hi @ronnyfrano,

The variables input_item_m and vm_m in the code snippet are Keras layers, not variables containing values. Therefore, you cannot directly print the values of a layer. To obtain the output values, you need to pass some input data to these layers.

That usual tensorflow model was created using Sequenctial API which has linear stack of layer, and in this code snippet Functional API is used, which more flexible. Check this out to get familiar with functional API.

The line input_item_m = tf.keras.layers.Input(shape=(num_item_features)) creates an input layer for the model. This layer is used to define the shape of the input that will be fed into the subsequent layers.

In the line vm_m = item_NN(input_item_m), item_NN refers to a sequential model that was created in one of the cells above. Here, item_NN is called as a function with input_item_m as its argument. This usage demonstrates the flexibility of the Functional API, where you can treat another neural network as a function acting as a layer in the current model.

The line vm_m = tf.linalg.l2_normalize(vm_m, axis=1) performs L2 normalization on the output of the previous layer. For example, if the input vm_m has a shape of (32, 100) representing 32 features and 100 items, each row of the matrix vm_m is treated as a vector. The l2_normalize function scales the values in each row such that the L2 norm of the row vector becomes 1. This ensures that each row has the same length, making it a unit vector in terms of the L2 norm.

model_m = tf.keras.Model(input_item_m, vm_m) constructs a model by specifying the input layer input_item_m and the output layer vm_m , indicating the flow of data from the input to the output through the intermediate layers defined in the previous steps.

Finally, the line model_m.summary() prints a summary of the model’s architecture. The summary includes information about the number of layers, the number of parameters, and the shape of the output. Understanding the summary can provide you with more insights into the structure of the model.

If you want to explore and learn more about these layers, I recommend referring to the TensorFlow official documentation.

This part of the code suggests movies that are similar in genre by calculating the difference between their genres and recommending the movie with the smallest difference. Make it a habit to debug the code yourself, as this is considered a best practice and will help you in the future. You can create a new cell, print each line, and observe the output to gain a better understanding. If you encounter any difficulties, feel free to ask. Remember to delete the newly created cell before submitting your assignment to avoid any potential grader issues.

Regards,
Mujassim