A two-tower network is a recommendation system architecture consisting of two neural networks: a user tower that generates user embeddings from user features (like age, preferences, country), and an item tower that generates item embeddings from item features (like item ID, description, image). The system trains these towers to bring user embeddings closer to relevant item embeddings using dot product similarity, where positive interactions (likes, replays) pull embeddings together and negative interactions push them apart. This approach enables efficient ranking of items for each user by calculating similarity scores between user and item embeddings, forming the basis of real-time recommendation systems used by platforms like Instagram and TikTok.
Two-Tower Neural Network Recommendation System Implementation
Added:Hey guys, welcome to another video. In this video, we're going to learn about recommendation system. So, I've been learning about recommendation system for a while. So, I thought to create a video about this. Um, it's really cool to be honest like um the recommendation system is what we are using Instagram, Tik Tok, any kind of application, Amazon, all these applications are powered by recommendation systems. And you know the cool thing about it that it is keep constantly learning every minute, every hour, every day. It's just learning your interactions with the products and the UI and updating their recommendation system based on that. So these type of recommendation system we call this uh real time recommendation systems.
Although we in this video we're going to only learn about the how the recommendation systems are built. Uh in some other video I will show you how to build a realtime recommendation system and how these actually works and how they keep updating their weights and um inter u the whole system. So let's get started and uh let's first learn about the types of recommendation. So the there are mainly two types. The first is content based.
So content base is one of the simplest way to actually uh build the recommendation. It's not the recommendation but it's just the um the uh retrieval system where you can retrieve items based on similar like similar similar items based on a certain behavior. So let's suppose you have a user and then user have a certain uh features like user probably in a certain country certain age certain uh things uh they like. So you you will so this user will have certain features and then it probably you know keywords or text attributes and then there is a documents also which also have certain features keywords and stuffs. You basically do the cosign similarity between that and then if the similarity metric is high you just retrieve that uh certain documents or certain items. So this is uh very simple uh here it mentioned like bird embedding. So bird is a transformer based model. So let's suppose if you have very heavy uh transformer model you can generate embeddings. Embeddings are basically the representation numerical representation of the uh certain text attributes or if you want to go to multimodel you can generate embeddings too using images, videos and stuff like that. So basically you generate embeddings and then you you will have a user embedding and then you have a item embedding. So you just do the cosign similarity and any kind of distance metric you can uh use and then if the uh distance is very close then this item is very similar to what user like if not then you should not retrieve it. So it's basically like that. So let's learn about the next type which is the collaborative filtering. Uh so the intuition behind is that you can just understand that if a certain user are watching a similar videos and then there's a another user who is very similar to the user who are watching those videos. So that user will also get recommended to the same video. So uh the simplest way to understand like people who like X will also like Y. So you can understand using visuals. So let's suppose if user one has uh certain preferences um and it is very similar to the user two. So there's a very high probability that whatever the user one is watching and liking those videos will also get recommended to the user two. So this is called the collaborative filtering.
Um so how the large scale recommendation system works. Um so as we have seen like collaborative filtering is much like what we have seen in the Spotify, Tik Tok, Instagram like those basically you can just but it is not just entirely based on collaborative filtering. It is much more advanced. So Instagram and Tik Tok use this uh two tower network uh which is advanced type of recommendation system you can say. So in this two tower network it is kind of like um there are two towers you can imagine. So there's a one tower which has this user tower and there's a second tower which is like a item tower. So these tower are basically neuronet networks and then user tower actually what they do is like they take certain features and generate embeddings and item tower you uh take some features and generate some embedding. So what happens that the user tower u generates embedding and then item tower generates embedding and during the learning when you train this towers what uh you try to actually get closer to the item tower.
So the user tower try to get closer to the item tower. So the embeddings of user will get closer to the item uh embedding. So if it get closer then you can use this to retrieve or basically recommend items. So we will going to learn like how I think it's uh let's let's go more dive into this Instagram model. So Instagram uses uh kind of different approaches like it's very advanced um recommendation system they have is divided into three parts. The first is retrieval.
is like they have like a billions of uh media source media things whatever you can call like images videos and stuff and then they try to actually get a thousand media using kind of a retrieval technique retrieval is basically exactly what I have described as content based you just you use some embeddings and then you just filter out a certain number of uh uh similar items what users likes based on the their age their uh preferences and stuff like that and after that you rank it. So this is much more uh important which we we were going to focus on this one which is like a two tower model which we have just mentioned here. So let's go like what exactly inside this. So after this first training you they you again like select 100 media which again you rank these things but we're going to focus on this one the first ranking stage because that's more important.
So if we take this uh in more detail about this two tower model ranking, you have this user tower and you have this item tower and then you put a certain feature user uh in user tower you put user features. User features can be like a certain uh users like any kind of like features. It could be age, preferences, uh country they live and all types of whatever you can just define. You could just use it as a features. And then item tab where you have item features. you put item features which could be um item ID and item um description all the stuffs and even though what how the item looks like the item image embedding and stuff uh and then you actually put into this towers which generates some embeddings. So user tower generates user embedding item tower generates user item embedding. Uh so here mentioned and introducing embeddings and then what you do is like you you do and then what you do you do the similarity between user embedding. So you calculate the dot product. So dot product gives a correlation between how these two things are similar or not.
Um and then what you do you train the model. So we have put like user features and item features and there will be some labels. So let's suppose um labels could be interactions. So let's suppose one could be positive labels. So posit positive interaction.
So let's suppose user liked this image, user u had a certain replays in this uh item and stuff like that. and zero could be like user just skipped it or like had a negative interaction stuff. So based on this labels let's suppose um we are training a certain we are training let's suppose we are training and then it is uh item has a label one and the and the tower predict and this neural network predictor 3 um dotproduct so like a score of.3 and then what will happen uh it we will create we will calculate the loss.
Loss will be like a distance between this label and the scores which will which will be 7. So 7 is very high. So the next time it when it will going to update the model has uh indication that okay it has 7 loss. So we need to minimize it. So it will increase the prediction which will be going to 7 next time.8 8 which has a loss of 2.1 and so that's how it will going to learn and the user tower will be get closer to the item tower and basically user tower will be uh in in another way you can say like user tower a user will be get closer to the items features. So if user one has a certain features, it will be closer to the item uh one features. So and if it's get closer to the item one features, you can just directly retrieve it based on like embeddings. So let's suppose if embeddings user users embeddings are matching to a certain item embeddings, you can know with this item user will user will like this item. And if it's a very far thing, if the score is.12, then you know it's user will not going to like it. So that's basically you do you just train it like that. And um you you do this offline although like when you interact in Instagram or Tik Tok it you know all the interactions you do it it get uh updated in their database and what happens that every minute every 2 minutes we train the model. So they update the embedding they update again to make it more closer to the item. You watch you w in evening you watched comedy video. So it get closer to the item tower which had the uh comedy videos. It it you know like it it closer to the items which uh had com which were comedies uh content and stuff. And um in the night if you started watching any kind of like action videos then it will again learn your preferences and then it will again update those things and again recommend based on that. So you will notice that it is it takes a very less time to update those things just you know because the interactions get updated and the embeddings get updated too. So that's why like how the entire system works in this called real time um recommendation systems what Instagram uh Tik Tok has. Yeah. So I think that's that's how the um the whole recommendation system works. Um and yeah I think uh the learning objective is to predict the engagement events. Example, someone liking a post a similarity measure between user and item embedding and in the end like a training user embedding should be close to the embedding of a relevant item for a given user. So this is all about the um recommendation system. Uh now let's going to let's train something. Yeah, I think we going to train if this going to be open or not. What the hell? My god.
What happened to this? Okay, I have to open it again.
That's a very bad hide notebook. Yep.
So what we are going to try to do is like we were going to train a very simple model here you know um so there's a movie lens data sets you probably can search it about it and uh in that data sets you will you have certain things so let's look you know wait so this is a data set we're going to use now I can go back Il small something like that and then in this you have a certain CSV files uh the first is movies and movies have movie ids title genders and stuff like that um and uh the main thing which we're going to use which is the ratings so you have this user ids and these basically user ids and these are movies ids and the user let you can see here user So one has interaction with movie and which gives some results which is a rating. So it has a four rating four rating four five certain things. So we're going to use this these user and movie and its interaction with it to train a model which we're going to recommend users next movies watch based on the previous interactions and and also with the others. Uh it were going to find a patterns also within the whole data set that if user one like the like this the movie one and the user two like the movie one and user one also like the movie two then the user two will also get recommended to movie two movie three sorry yeah so similar like that like the neural network is amazing so it we're going to find a pattern somehow it's more important how we construct it and what kind of loss we function we're going to this. So we'll focus on that and [Music] um yeah so let's start building this. I have already written this code uh this code basically going to just load this files and stuffs and uh ratings if here we are just loading it. Yep. So um here we have uh uh loaded this amount of ratings this number of users and this number of items we have and we have split it into train val and test this.
So here we splitting it into train validation set and test set. Uh let's look at what exactly inside training val or training df.
So you have this train here and then you actually have this user ID, movie ID, rating and stuff like that. And now we're going to build the data sets using data set class of P do. Uh here we will just going to use three features which is the user ID, user item ID and the ratings. Ratings will be the labels for it. And uh then we will load this into the data loader which B side of 512. Awesome. Now here we we were going to um construct our model which is the user tower item tower. This is a tower contains the embeddings which has the uh you will be the number of users and D is the dimension of the embeddings which probably going to be 32. Yes, it's going to be 32. and um the MLP then so MLP is like we were going to wrap this ML embedding into this MLP. So in the formattered we are wrapping the MLP. Uh yeah so in the power method we have this and then yeah we have this embedding we will generate the embedding and then do the prediction also using the MLP MLP just going to do is like another kind of adds more complexity here although you can just like do the ML embedding but I'm using the MLP here to just add more complexity which doesn't really require but it's okay for for this data set specifically because we are just using this uh simple user ID and then converting into tenses. Uh then we are doing uh we are gen uh building item tower similar to the user. Nothing changed here. And then we are defining two these these two towers. And in the forward method we are predicting the output of this tower which will be going to embedding and this item tower going to be IC which is the embedding and then what we are doing is like we are calculating the dot product as we have mentioned in the sides and then this sum. So this will generate the some kind of a score based on all the items we have. So let's suppose we have 9,724 items. So dot product let's suppose item one will have certain dot product and something certain dot certain score sorry. So each item have certain score let's suppose 0.3 0.4 four five and then we going to update each item scores based on user interaction. So let's run this and uh in the test evaluation where is the learning wait why we are here is the test evaluation we need to move this here okay so here we setting up the training uh the loss is MSE and the this is the optimizer with the learning rate of is defining the model and putting into this device. Here I have this MPS. So I'm just going to use MPS here. This is our training loop. Uh pretty much uh not complex just the predicting using the model doing the calculating loss doing the backward and optimizer taking a step. And here just the validation step similar to the training. Now just going to run this. This started training and it going to train till 30 box. So you can see like our loss is going lower and lower in both side of it in the training and the validation side of it.
Yeah, it it is not trained. Now we can just test this uh test is similar kind of thing and in the end like in the we are just uh testing this in a test testing this using a test set and uh in the final this is uh important like this is something we are recommending based on the scores. So um this is how you can use this in inference or this can be used in inference pipeline. So you basically generate the user embedding using the user tower. So you might have noticed that we are not using model or basically this model to do the generation. Instead we are just using the independent tower. Here we are using user tower to generate user embedding and then we are extracting all the im uh items and we are using item tower and putting all the items. Here we just using single user to generate a single user embedding and then here we are putting uh getting all the embedding of the item items. Yeah. And what we do is we again do the prediction calculate the scores among the all the items and we do the top K like arsh shot which select top five highest scores like top K uh K is 10 here's a top 10 highest scores and then you just map it to the movies based on its ID. So it's going to recommend the movies for us. So test uh predict test uh loss is also kind of a similar to the training one. It's like training had the.7 and the val has8 and test has this much although if you train more possibly go down and uh here we have the recommendations which is cool kind of thing. Uh so we got the recommendation for the user one is like 10 recommendation movies. It's like a ponyo. I've never watched it. Going in style. All these like movies they have.
Let's do this again. Although like the weights are same. So it won't going to recommend differently. But if you train it again possibly have some different preferences stuff like that. So although like this was the um two tower network model, I hope you guys learned like how how this actually works the recommendation system and the the ranking system works. But I hope I'm I'm kind of like want to make a another video a separate videos on the realtime recommendations where I was just going to go very deep into it and build a really cool recommendation system which automatically going to update when user interacts with something. So I hope I will create another video for that. But yeah, I think it was amazing to showing this um and explaining this to you. I hope you liked it. Thank you guys for watching this video. See you. Bye.
Up Next

Latent Factor Recommender Systems | Stanford Lecture 55
@ArtificialIntelligenceAllinOne
50K views•2016-04-13

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science































![19. Lấy mẫu âm: Cải tiến Skip-gram - [Sequence Models]](https://i.ytimg.com/vi/VNnPK8yH_k4/maxresdefault.jpg)


![[Open DMQA Seminar] What is Hard Negative Sample?](https://i.ytimg.com/vi/61q35az-khQ/maxresdefault.jpg)




