Conf42 Observability 2025 - Online

- premiere 5PM GMT

Using the activity logs to predict user behaviour and improve experience

Video size:

Abstract

Web apps are growing more and more complex, loading many assets per page. We’ll build a tool to optimize this by analyzing past user activity to predict future actions. Based on these predictions, the app will prefetch needed assets in real time, improving performance through predictive prefetching.

Summary

Transcript

This transcript was autogenerated. To make changes, submit a PR.
Hi everyone. My name is Alex Hang. I'm welcome to Comfort two. I'll talking today about using the activity logs to predict user behavior and improve experience. So what are we talking about? How can you use it to predict the user activity? I try to predict the next activity of a user based on his previous activity logs and see how can you optimize the application? Why am I doing this well? Because I want to optimize the applications to make sure that the server has resources and to make sure that the user has the best experience. I. How am I going to do it? By adding machine learning in the application machine learning that will analyze the activity logs and some examples to see why you are here. That'll be the most important part. But first, a bit of theory. What's ai? What's machine learning? Are those fancy names? What do they do for you? We know that activity logs are stored past active, past actions for users. Machine learning models are trained on historical data. I think you heard about machine learning about AI in the past few. Months, years maybe heard of ai. And you know that there are a lot of models such as neur networks, long short term memory, transformers, some other things can be used to predict activities, to predict tasks, to predict numbers and sequences and some other things. How much machine learning model works? They let's say they want to build a system to classify. An image and say it's a cat or a dog. How? I give him 10,000 images of dogs and that has images of cats. I'm gonna ask you something right now. Which one's a cat? Which one's a dog here? That's a stupid question. Of course everybody knows, which is a dog, which is a cat. Now he comes a hard question. How do you know that? How do you know which is a cat and which is a dog? I'm just curious. I'll give you 10 seconds to think a bit about it. The point is a dog, obviously, but how do you know that? Did you took the image and analyze it? Does it have small eyes or big eyes? Does it have big teeth or small teeth? Does it have white skin, white, fall black? No, you didn't do that. Okay, come on. You just saw the image and it's a dog. How do you know that this one is more tweaky than you might think? Actually, you know it because you saw. Thousands of dogs in your life. Okay? You know how a dog looks like, but you don't spend time to say, how does it look like? How are it seals? How is it mud? How are his? How is his nose? Or whatever. You don't do that, so you just know. Your brain is trained to recognize which is which, because you saw them. This is exactly how machine learning models work. They're using training data to build complex models in inside memory, and they're using those models in order to recognize patterns. This is what they're doing and they're doing, using them to predict, to classify some other things. So we gonna feed training data to the machine learning model. And then once the training is done, you'll see in a few seconds how training is done. We'll proceed to testing. We're gonna give him 100 images, other images of cats and dogs. How do we know which is which? The model should already know, which is which I'm going to ask him, which is a cat, which is a dog. Let's say the data of 100 images, he managed to classify correctly, 80 of them and 20 are wrong. In that case, the accuracy is 80%. The compute accuracy, I'll say the accuracy is 80%. I'm happy, if not back to training. Okay, so how do I use it right now? I have a few machine learning models to try. The first and most common was linear regression. The linear regression is not good for what I want here. I told you that I want to try to predict the next action that the user would take. The linear regression is good for predicting linear things, how well it'll take points, let's say the stock market, the value that the stock market had in the past two 10 years, and to try to predict the next value of the stock market based on historic data. That's what the integration is doing. The decision to be used to classify things well, it'll be good for classification between cats and dogs, actually, but not really. But it'll be good for other types of classifications. More simple ones. Let's say that you want to classify a text and say if it's English or French or other language. It'll take a look. It'll take a look at the verb, so the pronouns that some things I need to try to classify it. Okay. The random four is a more complex decision tree. It's a collection of decision trees. This also used for classifications. We're not going to use it today. And here comes tricky, key part, newer network. This is what we're going to use today. Neur networks are very good because they're adaptive. They can easily adapt to change. They can easily adapt to patterns, that can recognize images, that can do a lot of things, and they're very versatile, but they're a big black box. We don't really know from the outside how they're building the models and other things. So now I'm going to ask you a simple question. I asked you two slides ago, how did you know if that is a cat or a dog? And you said, I just know it. What if I tell you it's not really like that? Of course, for you it's like that. I just know it's a dog. Okay, but how do you know that? He comes a tricky part. Our brain is structured like a neur network. Actually, let me put this right. The Neur network is structured like our brain. The Neur network is built because the computer scientist, which built it 20 years ago or more than that, sorry. Ago, something like this said you know what? Let's try to mimic the human brain. How does the human brain work? We have the input layer. What is the input in your brain? Your senses. You have your eyes, your ears, you have the, you can touch things, you can feel things, whatever. This is the input. What's the output? You can do an action. You can move your hand, you can speak, you can close your eyes. You can do whatever you want. What's the hidden layer then where that your actual, your brain is built out of nuance. This is where the term neuro network comes from nuances. Nuances are small cells inside your brain. You have billions or actually trillions of them, and those nuances are communicating between each other, forming super complex networks in which electrical currents are circulating and they're communicating with them with each other. Millions of times per second, to create your thought, your feelings, and other things. This is what Neuronets are trying to replicate. But instead of using cells, because we can build living cells, we're going to use functions. Some small objects functions, classes, which have a function in them. It'll be called activation functions in a second. What that means, because I wanna explain right now, those are functions that are communicating with each other, each one of them doing a specific task and showing the results. Then the final layer would filter this and give you a final result. So it's basically like a human brain. So why do we try to do this? As I told you, I want to be able to predict the next action of a user, but you're going to ask me why do you want to do that? Good question. I want this because I want to personalize app experience, how to optimize some common workflows and to identify which are the common workflow for each user, and to optimize them, and how to predict user actions to prepare for increased workload and specific services. How do I do this? Let's give the setup. Let's say that you have a user activity logs. We predicting real time, which tasks user perform so that we can predict some usage per task. We'll be this more new network to predict the next action for user and some other alternatives can be a statistical approach to not go into new net. To just go on statistics and say out of 100 users, 40 of them are going from home to newsfeed and from newsfeed to messages. Okay, that could work, but it'll no longer be personalized. There will be some sign of some sort of a generic optimization. I want to do something deeper. I want to do personalized applications for each user and to predict for each user activity. So how I'm going to do this 10 flow. Okay. I'm going to say what? Yeah. Is a framework built by Google and maintained by them for building and running machine learning models. Why do I, why did I choose 10 though? Because it can be used to, you can use existing pre-train models, you can retrain existing models and can develop new machine learning models. You can quickly train new models. You can adapt various models that exist and can run it on server or in the browser. And this is the main thing why I chose 10 Flow, because I have some other alternatives too. But I want TensorFlow because they also provide us a browser solution, a JavaScript solution. So instead of putting more pressure on the server. I try to transfer the workload on the front end here because to be something per users, I told you my task here is to do something per user to predict his workload. I want to see what the user will do what actions he will take, and the most importantly, how is this? How should the system react if I know the next action for user? I can pre, I can prefetch some data for it. I can predict what data he will need. I can predict the server load for that and some with other things. This is why I want TensorFlow, because I have a TensorFlow JS JavaScript alternative, and one more reason is that it's already used by hundreds of top companies. I won't even mention them. What I told you here. You might think that I'm repeating myself when I said I can use existing Python models or I can retrain existing models. You say, isn't it the same? Not really. I can take an existing model already built by Google, by Amazon, by Meta, by Adobe, by whatever other huge company, and use it if it's free. They're usually published. Some gonna be published on Hugging Face. It's one of the most common websites, hugging face. Or I can take that model and simply put it in TensorFlow and use it for my application. If you go on TensorFlow website, you can see that they're already using. Of them. But let's say that I have a more specific use case. Let's say that Google built a model to identify cats and dogs. I'll tell you again, I'll give that example. Let's say that somebody built it. Somebody at a random university or a random company built it. Okay, what am I doing with it? I want it, but I want something more specific. I want to be able to identify cats, dogs, and rabbits. I can't use that model because he doesn't know what a rabbit looks like. So how can I do that? I take that model and I retrain it. I retrain the model that they built, and I give him 10,000 images of rabbits, and I hope that I'll be able to get the accuracy on all three classes and secure and identify catch docs and rabbit and so on. If that doesn't work, I will develop my new model from scratch. I say, okay, leave aside what those guys built and let me build my own model. So I will try to show you right now something I told you about the models regression decision three Random forest neur network. What if I tell you that a newer network can be so adaptive that can do the things that the other ones can also do? Yeah, I'm serious. Linear regressions, I told you are exam for approximating things. Guess what? You are not. Oxygen also approximating things. So how am I going to do that? I'm going to be the basic regression example to approximate the next number in a function. Let's say have a function or two x minus one or something like this. But I won't add the computer, which is a function, and I'm going to approximate using a new network. Obviously the best example, building a regression, but I'm trying to mimic a linear regression with an EO network. Exact exactly in a second. Why am I telling you this? I'm going to import it as. I'm to define the model, which is sequential. Exactly. The data is sequential. I know all the numbers. 0, 1, 2, 3, whatever. We're gonna add hidden layers to the model. I told you earlier. This is basically the brain. The hidden layers are the brain of the model. This is what's thinking and taking decisions. I'm going to compile the model. In a few seconds what those things, loss optimiz and other things mean. I'm going to define our import training data. This is the tricky part. Now I'm going to define the data. I'm saying in value minus one, the form function is minus three in value. Zero is minus one in one, it's one in two, it's three, and so on. So now the question will be, which is the function? It's pretty hard to find it out, but once the model is trained, she can easily predict it and say, okay, if in this point it's like this. Okay. And then I'll ask him. What would be the value of the function in point 20? And the model will say, okay, based on the function, it'll be whatever, it'll be 35 or something like this. I dunno, I'm not good at math. I didn't compute it, I just showed you a random number. But this is how the model will work. You try to predict the value of the function in point 20. So why do I show you this? 'cause this stuff is not what I want with the user. Let's say today. Let's say that in the second the user is at page home. Then he's at newsfeed, then he's at messages. Then where will he be in the next two actions or two actions, or four actions or so on? So this is what we're going to do today, our next price prediction system. Let's say the user goes from home to newsfeed to messages to marketplace to newsfeed, and where does he go next? I can do a statistical approach or I can use the newer network to say, okay, predict me the next page in which the user will be, this is what I want to do right now. How. I'm going to gather required data from activity logs because I know what the user is doing. I told you most of the applications were destroyed. And to transform the data for training and to initiate the model, train the model, and use it to predict how, let me give the practical example. Let's start writing code. This is why we can Q Actually, until now, this was all theory. Now let's start playing. So we have a lightweight now, just application web around the machine learning app. We have 10 for model, wanting to predict the next action, and we predict the actions. Then the predict actions will be used to preload data when ensure several resources are available. So how do we get the data? Let's say that we just write in memory right now. I don't want to stress the network or something we, cause no, let's just have an array network, which will mimic the activity log. So how do we train and run the model? This is pseudo code, basically three us just to psdo code, and I give the actual code in the next five, six slides. First of all, we're going to transform the data. We're going to initiate the model, train the model, and then we're going to use it to predict the page. So how do we do each of those steps? How do we transform the data? You have the number of pages that the application has, let's say 10 pages. Can I just pass to the model? Something like home newsfeed messages shop contact. No, I can't. Why? Newer networks don't really work to okay with strings. It just, they can't just handle words or strings. We have other models which are way too complex for us right now because we are not AI engineers. We're just software engineers right now. And, there are more complex models, which are handling strings. Some of them are GPT transformers. Both might have heard of them on the news, but that's way too complicated for us right now. So what we're going to focus on right now is the Neur network. So in order to give the Neur network some data that it can process, we're going to transform all the data to numbers. And here we go. This is what we do here by transforming into numbers. We'll just say Page news. Suite is number one. Page home is number two. Page messages is number three, and so on, and got transformed this to a vector to a 10. Basically what we're doing right now is called the one hot ENC coding. That's what this is called. One hot encoding. This is how tensile flow accepts data. This is the best way to handle the data. Tensile flow. The name tensile means actually vector. It's some sort. The 10 is actually some sort of a vector. It's a more, it's a more powerful vector using machine learning to pass data from one led to another. But basically it's a wrap around the vector or in way. So now this is the most important one. This slide and the next one are the ones where we came here. So this how we initiate the model. We say it's a sequential model, I told you already, and we add layers to the model. What are those layers? I told you in the example we have nuance, newer network. So why now add two layers of nuance. Both of are dance layers because they have the nuance dance in general, and this is the tricky part. First we had the layer with the activation function level. This activation level. What is this layer? It's rectified linear unit. This is something that is used in the Neur network in the internal layers. I can give you material behind this. You should search online for activation functions. There are. A lot of them, each one with each pros and cons. The most common size are value gel, and for the final layer you usually use softmax. In the last layer softmax. Softmax is the function that would take the data from the previous layer and try to put it in a class or in a result, we try to classify it basically, and this is how we're going to obtain our final result. The first layer here, we try to act as. The email layer, which will calculate things and stuff like this, and the final layer with Softmax, we try to basically take the results, make them linear, and use them to predict the next action. And we're going to take this and compile it now, compile the model and can use it. You can see on the slide here what you can change here on your example. You can search online for of this, how to change the activation, how to change the units to optimize the loss and the metrics. So what do I have right now? I have the model. But what is this model? This is the brain of the system. Considerably. You just saw a newborn puppy or a newborn cat, or a newborn animal. What can a newborn puppy do? Nothing. He can just breed and eat. And that's all. Other than that, he doesn't know how to do anything, but he still has the brain in it, but the brain doesn't know how to do anything. It's same for puppies, for little cats, for. Kids for humans, for every, for everyone with the brain. So right now we built the brain of the system, but it's stupid. He doesn't know how to do anything. We need to teach him how to do stuff. So here comes the training part. We are training the model by giving him data, using the fit function model that fit. We're giving the user the activity, log the labels, which are the basic, which is the metrics, and we are going to see if it can predict them. If you can predict the next action of a user, so we're gonna specify 100 epoch. So try to train your model 100 times and the batch sizes, how many items you take in each batch for training. And now that we are the finish, we have the model, we have the brain. The brain has been trained, we can actually use it to predict, we have the function model that predict. And again, to give the user the current history and tell the model, Hey, this is the current history, which is the next item that the user will access. Tell me and what I going to do with this result. Whatever you want, you can pull out the next assets based on this. Let's say that you know that the user will now go to page messages. If you know that the next page is messages, you can easily do the request the server while app is idle and pull out the way the data so that when the user clicks on messages, they approval instantly. 'cause all the data is already there in the memo. We can use it to detect anomalies, user flows. We can use it to predict the server workload or some other things. That was the tricky key part, doing the actual prediction. If you're on the full code, you can see it on a GitHub, I give you here also the source code, and you see the how to initiate the model, how to navigate, how to process the data, how to train the model, how to predict and how to print it. If you have any questions, please feel free to contact me on my email, my website, my on the conference website to whatever you want. And feel free to ask questions. And thank you very much for attending Comfort Two, and have a nice day everyone.
...

Alexandru Hang

Software Engineer @ ING Tech Romania

Alexandru Hang's LinkedIn account Alexandru Hang's twitter account



Join the community!

Learn for free, join the best tech learning community for a price of a pumpkin latte.

Annual
Monthly
Newsletter
$ 0 /mo

Event notifications, weekly newsletter

Delayed access to all content

Immediate access to Keynotes & Panels

Community
$ 8.34 /mo

Immediate access to all content

Courses, quizes & certificates

Community chats

Join the community (7 day free trial)