Docsity

AlphaZero from Scratch - Machine Learning, Summaries of Machine Learning

Toggle sidebar
bg1
Alpha zero is a game playing algorithm that uses artificial intelligence
and machine learning techniques to learn to play board games at
superhuman levels. In this machine learning course from Robert Forster,
you will learn how to create alpha zero from scratch using Python and the
deep learning framework. Pytorch. a Monte Carlo tree search algorithm is
this search algorithm right here and it will take a state on our case a
block position as input and then we find the action that looked most
promising to us. And this algorithm gets this information by declaring a
root node at the initial state and then by building up this tree into the
future. each node stores. This variable W and W is just the total number
of wins that we achieved when we played in this direction into the
future. So basically when we took action one initially and arrived. At
this node, we were able to win three times. the result of this tree right
here that we got at this time would be that action one should be the
action. We we would like to play..
There are always two possible ways in which you could expand in this
example, right here. So this is a leaf node and then also this would be a
default right here. Because this node doesn't have any child right here.
the next phase of the Mcts is the expansion phase. So right here. In
expansion we want to create a new node and dependent to our needs.. The
idea is basically to play randomly into the future until we have reached
a state UH where the game is over right. So we want to play. the
simulation phase is the simulation phase and here we play using random
actions so we just play randomly and finally we have just reached this
node right here. the number of points we got when we walked in this
direction should now be three because we have visited it one time. we.
Would again um choose an action and we would just check the children of
our route again and again could just choose the action based on the
winning amount of wins right here so this is great Uh let 's also just
have an example where we start from at the beginning so that you might
find it easier to understand the search algorithm as well..
So let 's do it like this and yeah Now basically we walk in this
direction right here and next we can do simulation again. So we can say
that we have actually lost the game. So when I propagate all the way up.
I will only increase the visit count right here so the physical should
now be one, but the total number of wins should still stay the same being
zero because we have lost here. the UCb formula actually gives us a
higher results for this node just because of the visit count that is
lower right here. So when we now walk in this direction, we again have to
check if we have reached the leaf node or not. because of that we move to
expansion and create this new node right here and this should just be
equal to s4. Then so we can say that we took action 4 right here. then
again the W and the this account Um should be set to zero. the UCB
formula has been updated to reflect the new model that is being used in
Monte Carlo research. we also want to use the information of the value
that we got from our neural network. we then tend to select children more
often that were assigned a high policy by its parent right. So this way
our model can guide us through the selection phase..
pf3

Partial preview of the text

Download AlphaZero from Scratch - Machine Learning and more Summaries Machine Learning in PDF only on Docsity!

Alpha zero is a game playing algorithm that uses artificial intelligence and machine learning techniques to learn to play board games at superhuman levels. In this machine learning course from Robert Forster, you will learn how to create alpha zero from scratch using Python and the deep learning framework. Pytorch. a Monte Carlo tree search algorithm is this search algorithm right here and it will take a state on our case a block position as input and then we find the action that looked most promising to us. And this algorithm gets this information by declaring a root node at the initial state and then by building up this tree into the future. each node stores. This variable W and W is just the total number of wins that we achieved when we played in this direction into the future. So basically when we took action one initially and arrived. At this node, we were able to win three times. the result of this tree right here that we got at this time would be that action one should be the action. We we would like to play.. There are always two possible ways in which you could expand in this example, right here. So this is a leaf node and then also this would be a default right here. Because this node doesn't have any child right here. the next phase of the Mcts is the expansion phase. So right here. In expansion we want to create a new node and dependent to our needs.. The idea is basically to play randomly into the future until we have reached a state UH where the game is over right. So we want to play. the simulation phase is the simulation phase and here we play using random actions so we just play randomly and finally we have just reached this node right here. the number of points we got when we walked in this direction should now be three because we have visited it one time. we. Would again um choose an action and we would just check the children of our route again and again could just choose the action based on the winning amount of wins right here so this is great Uh let 's also just have an example where we start from at the beginning so that you might find it easier to understand the search algorithm as well.. So let 's do it like this and yeah Now basically we walk in this direction right here and next we can do simulation again. So we can say that we have actually lost the game. So when I propagate all the way up. I will only increase the visit count right here so the physical should now be one, but the total number of wins should still stay the same being zero because we have lost here. the UCb formula actually gives us a higher results for this node just because of the visit count that is lower right here. So when we now walk in this direction, we again have to check if we have reached the leaf node or not. because of that we move to expansion and create this new node right here and this should just be equal to s4. Then so we can say that we took action 4 right here. then again the W and the this account Um should be set to zero. the UCB formula has been updated to reflect the new model that is being used in Monte Carlo research. we also want to use the information of the value that we got from our neural network. we then tend to select children more often that were assigned a high policy by its parent right. So this way our model can guide us through the selection phase..

So first of all we want to walk down until we have which relief mode mode and here we can already see that our root node is in fact additional because we can say expanded in these two directions here. because of that we will then move to the next step right here, which is the expansion phase. here we will actually create our new nodes so now When we expand we will create this these two new nodes. currently the way this is implemented. We get we would get a division by zero error so here we have to make sure that we will only check for the winning probability. If we have this account that is larger than zero. and then also we were just walk down in the direction where the UCB formulas the highest right. We basically continuously perform a Monte Carlo tree search sample an action out of it play and then change the perspective so that we are now the other player or the opponent, and we do that until we actually have reached the terminated state. 0. 1 here and we will raise this visit count by one. So let 's change that one right here and remember that we want to back propagate all the way up to our root note so here we will change the value so w 0 here to 0. 5 so also add 0. 1 to it and then erase this number of physical by one as well, so it will be two afterwards.. For each state, we also want to store the Mcts distribution and the reward for the given state. The reward is equal to the final outcome for the player that we are on this given state so basically. In this example, right here X won the game so that means that for all states when we were player X, we want the final reward or final outcome be equal to one and in other cases when we are player O and we want reward to be negative one. Because we lost the game. combinations of the state. The M CTs distribution. and reward will then be stored as two bits to our training data and then we can later use these. for training right in order to improve our model. So this is alpha zero is structured and now we can actually get to coding so let 's actually start. by programming Alpha zero. we will start everything just by creating a simple game of Tictactoe and then we will build the monte colored research around it. after we have gone so far. We will eventually build Alpha 0. On top of the multicolored research we had previously.. There are four different ways in which you could win a game. So in Tictactoe. There are four conditions in which players could win. the most weather field is equal to zero so let 's write a method get valid moves music and here we want the state and then action so let us first get the row again so we just say action integer division self-dot column card.. The way we can do this is by using the Np dot diag method, so I put up the documentation right here. I say if Np dot sum of safe. getwell moves of the given state music equals zero, then we just return 0 for the value.. tictactoe is a board game of Tic tac toe.. Currently we get all the indices for the possible actions and then we want to check if the indices itself is valid. So we say if valid moves of i equals one right. then we want an action so the action will just be an input that would be casted with an integer.. In all other cases the game must continue. in our init. We can say self. game equals game and safe. ax num searches music and then below. We 'll define our search method. for each search we 'd like to do. The selection phase, then the expansion fits, then the simulation phase and then the back propagation phase.. After we have done