what is going on everybody welcome to a
part 11 of the artificial intelligences
in StarCraft two with Python tutorial
series in the last video we built our
neural network convolutional model and
in this tutorial what we're me doing is
working on iterating through our data
and feeding it through this model and we
should actually begin to train the model
in this tutorial don't forget in the
previous tutorial all the training data
was linked also the trained model I will
also be hosting I'm already hosting it
so again that's in the text-based
version of this tutorial so with that
let's go ahead and get started
so the first thing we need to iterate
through the training director training
data is to iterate through the training
data directory so I'm just gonna call
that train data because that's what mine
looks like what try to bring it over
there we go
that's what mine is and here is all the
data so when you download it from the
training data just extract it into
whatever your working directory is okay
so once you've done that we're ready to
start so the way that we're going to do
this is we're going to iterate through
so so each one of these files in here is
a game that we won and each game has
like 30 to maybe 60 actions that it took
that we saved so each of these is a game
know we don't have to load in just one
game at a time and in fact we really
don't want to do that because that would
be sometimes a very small batch size
that we probably want to load in much
more plus we can load in much more most
likely depends on your GPU or I guess
your RAM if you're not running the GPU
version tensorflow
so either V RAM or Ram regardless we we
have more than four megabytes all right
so we want to load in a few games at a
time so we're gonna do it in like chunks
but we still also probably want to have
more than one epoch so or epoch
for those that get really angry at me
for saying it so so so what we want to
do is we want to iterate through all of
this data many times but we have to do
it in chunks now built in a Karass if
you pass your entire data set you you
can batch that data but you still have
to have it in memory so I'm sure in
Kerris there's a way to do this because
this is like a super common problem
right your training data is much larger
than your vram or ram so if somebody
knows the way please feel free to link
below the way I'm going to do it is a
hacky way around it but it gets the job
done so move this side and we're gonna
specify hm e pox we're gonna say 10 and
now we're just gonna iterate over those
epochs so for I in range hm a pox what
do we want to do well we need to track
where we are because we want to stop
slicing through the data again there's
probably a pythonic answer to this
question I don't know it's a I guarantee
you one of my viewers knows that though
so post it below if you've got a sexier
solution to this so then we're gonna say
increment by 200 so I know you can
iterate through something in increments
but that wouldn't stop us I don't anyway
it's not important hey if you know the
answer put it below anyways then I'm
gonna say not maximum someone's going to
just be really bothered by the way I'm
doing this but that's okay all files
equals OS dot list
derp and we're just gonna list out this
train dated or so so so now that we've
got all these files a thing that would
be nice to add in as to not because we
have the opportunity we don't need to go
through the same order of the files
every single time right we're gonna keep
loading these in for each epoch so one
thing that we could do is just shuffle
them every time we load them in so first
of all let's grab the maximum so we're
just gonna say maximum equals Len
all files this way we know when to stop
iterating and then what we're gonna do
is random dot shuffle and we're going to
shuffle all these files so then what
we're gonna say is for file in all files
and then we're gonna say current to
current plus increment and then what
we're going to do is and in fact let me
think about this for a second so
actually probably we'd want to do is
randomly shuffle we would rather say wow
not max maximum while not maximum then
we want to iterate through the files and
as we iterate through the files we would
probably well first let's just say print
currently doing blank : link and let's
format and then we'll format current and
then current plus increment this way we
know where we are
in the training and then for file for
each of the files what we're gonna want
to do is we're going to want to take the
samples that we have in populate lists
that are those actions so populate lists
that are attack enemy base do nothing
that sort of thing
so what we're gonna do is we're going to
specify four empty lists so we're gonna
say no attacks that's an empty list and
then we're gonna say attack closest to
Nexis empty list attack enemy structures
that's an empty list and then attack
enemy start that'll be an empty list as
well and then as we iterate over these
files we're gonna just give the full
path to that file is gonna be OS top
path dot join train data
directory and whatever the file is then
data equals NP load the full path what's
that you said Oh two O's got it so the
next thing that we're going to do is
data is going to equal list data we
actually don't need it to be numpy we
probably could pickled it or something
like that but right now we want it to be
a list now we're gonna do is for D in
data we're gonna say choice is NP arg
max d0 so the zero width so the zeroeth
was the choice and then the first tooth
was the actual image itself so so the
choice and in fact we we didn't really
need to arc max this we could have done
this another way but I feel like it's a
little more clear if we do it this way
so if choice equals zero then we're
gonna know attacks dot append we
actually
so in my in the text-based version of
this tutorial I wrote D 0 and then D 1
we should be able to just say a pen D
alright that was kind of silly so now
I'm gonna copy this and paste now it's L
if this will be a 1 and I'll fix the
other the list in a second so 1 2 3 and
then we're just gonna go in order so no
attacks a 1 is a attack closest to the
Nexus 2 is attack enemy structures and
then a 3 was attack an enemy start ok
once we have that we've iterated through
all of the files in that slice and we've
populated these lists now what we want
to do is we want to balance these lists
we want them to be the
same length as everyone else so we're
gonna say lengths we want to get all the
links of lists equals check data so now
we need to build check data but first a
quick thank you to my recent sponsors
Johannes Geisler or Ioannis Keisler
Brock Elmore veeram and high in limb
thank you guys very much you guys are
incredible
ok so for our check data function this
is some pretty basic stuff I'm gonna
copy and paste this one in so I'm just
going to stuff this here and all this
does is again not the most ideal you
probably want to like pass the lists in
some as parameters to make this like a
more utilizable function but anyways all
it's all it's doing is a block of code
that I you ended up using twice so I
just threw it in a function but you'd
probably want to improve that a little
bit again none of this is going to be
very polished stuff I'm just trying to
run through it find things that work and
keep iterating over so feel free if you
guys want to clean up the code or
whatever have at it
anyways all we're gonna do is iterate
over the lists get the lengths of all
the lists and then return the lengths of
all the lists so once we've got the
lengths of all of these lists the next
thing that we want to do is go with the
smallest one so we're gonna say lowest
data equals the minimum of those lengths
and then we want to shuffle all of them
so we want to shuffle no attacks because
we're about to trim them so I'm gonna
copy paste paste paste be nice maybe to
iterate over the lists rather than doing
doing this like copy and paste but
change one little thing like for lists
and a list of the lists basically do
that anyway so we're gonna shuffle those
and then what we're gonna say is we want
all these lists
to be sliced from the beginning to the
length of whatever the lowest data is so
again this would be better if we
iterated because then we would also be
able to iterate this part so we're gonna
say no attacks equals no attacks up to
the lowest data amount so again this is
how we're going to balance all of the
data so if one action has let's say you
got a binary choice right you've got a
yes or no and you're gonna feed yeses
and noes to a neural network if the
yeses and noes are a 50/50 balance
chances are the neural network is likely
to figure out the real reasons why why
you might have a yes or no if you have
yeses that are eight you know 80% of
your training data is yes the neural
network is instead most likely are more
likely to just learn always predict yes
that's a really easy thing to learn so
it's a really quick descent or ascent
and in whichever way you're going and
that it's just not good right so you
want to avoid that and one way you can
avoid doing that or falling into that
pitfall is if you balance all your data
so again I'm just going to copy paste
that was closest to Nexis paste paste
okay so now we're trimming all of the
data and then I just want to check data
one more time to make sure it's of the
length that we intended so let me go
ahead and run that real quick make sure
everything's working before we do the
rest
really I coulda sworn check data printed
it does
what's what will be happening here why
wouldn't there's no error there's just
these like tensorflow keep Dems warnings
which is fine why did this not throw or
what why is it running and then doesn't
make any sense so it runs through the
epochs no problem
while not maximum oh okay
pretty sure I'm in that well we have not
reached the maximum yeah but we're gonna
do that for 10 bucks so we're doing the
slices cool cool cool cool
okay so so you can already see it's
trimming and stuff what was the deal X I
don't think that cancels the build
though yeah let's just hit cancel build
okay all right so everything's working
up to this point so now what I want to
do is we want to combine all this data
so we're gonna say train data and that's
gonna be no attacks plus attack closes
it doesn't really matter we just need
all four lists so attack structures
attack closest sin exes cool so now we
have all the train data now we again
want a random shuffle the train data and
again we want to do this because these
are pretty long lists and the way the
neural network is gonna work is you
might start with no attacks but you're
ending on this like I say attack closest
to Nexus so the last you know 2,000
samples that the neural network is going
to see are going to be all one answer
that's going to cause problems so it's
going to go through new attacks and then
slowly start to always predict no
attacks and then we're gonna switch over
to attack enemy start and it's going to
only predict that it's going to just
keep doing that over and
this is not not good idea so we'll do
this way the next thing is test size so
we're gonna test we're gonna set our
test size to be a hundred and then we're
gonna set our batch size to be 128 now
we're going to set X train and why train
I'm gonna copy and paste these ins kind
of arduous to type these out and it's
not really too much point so somebody
somewhere help me avoid what's a man got
to do to not have to type I understand
why we have to reshape here but if I
just try to do let's say I try to do
train data and just do a slice of the
train data and like so for example if
you always want the first if you can be
like :
was it : comma 1 or I can never remember
the the exact order
I think it's : comma 1 for the always
the first tooth element anyway if you
try to do that here it just doesn't work
why why do I have to rebuild the array
and then reshape it there's got to be a
better way
so anyways somebody somebody tell me the
the better way basically so I can avoid
doing this part this I 1/4 I in train
data to then test size or two through
the nevermind anyways so I kind of I'd
avoid doing this thank you
so what's going on here is basically
what we're really trying to do is we're
just saying okay the the training data
for X X is always like your input Y is
your target so X the input this is gonna
this is the imagery this is the the
picture that we drew in OpenCV of the
game map and all our units and colors
and sizes so that's what our X's are and
the Y's are the attack choice that we
make so what we're doing is we're just
gonna say the training X is all the way
up to or basically keep the last hundred
right the last test size so up to the
last 100 elements that's our training
data and then the last 100 elements down
here for the testing data and then we
just have to reshape them this way this
is just what
the models want that's basically the
case if you're passing this to
scikit-learn or you're doing straight
raw tensorflow or in this case Karis
okay so once we've got that we're ready
to do a model dot fit and to fit we need
X train we need our Y train and then
we're gonna have our batch size equals
batch size the validation underscore
data will go with X test and Y test and
we do want to shuffle so shuffle equals
true actually we probably don't need
that I think we're kind of handling the
shuffling and all of that ourselves
verbose verbose is one and callbacks is
just going to be that tensor board so I
actually haven't toyed with verbose but
I'm gonna wager that that's going to be
in conjunction but you want to do I
think that's the four straight-up tensor
board what kind you know it's like at
least with the way that we're doing
things here we should be keeping track
of accuracy and validation accuracy and
loss like the main things but you can
raise this up and in track more things
I'm not 100% on that but I'm pretty
confident that's that's all that is
after we do the fitment we're gonna go
ahead and model dot Save and we can say
this is whatever we want and in fact I'm
gonna copy and paste this last part I
don't feel like typing that out and then
so all I'm doing is kind of dynamically
saving this I'm saying how many epochs
am i doing what was the learning rate
you could make other things I'm putting
stage 1 just because it's that first
model and then all I'm doing is
incrementing our current number where we
are and as long as current is not
greater than maximum we're going to
continue moving along so once it is boom
we pop out and if we have more epochs to
do we do more epochs
again not the most ideal thing also X
test and Y test these are not not
out-of-sample because they're random
right we shuffled every time so you know
you might want to separate out your you
know have some truly out-of-sample
testing so like the validation accuracy
that we're gonna get here you know take
it with a grain of salt basically again
I'm just trying to get answers can a
model learn with this input I'm gonna
run it just to see the recording may
screw up it doesn't like it because it's
gonna hog my entire GPU so as long as it
works I'm gonna stop it will see it may
not work I hear it I see it okay so we
can see he's training right now we can
look at loss and we can kind of see
what's going on it's gonna take a while
so we at least can see losses going down
accuracy is about 25% what's 25%
I should probably I have no idea I
probably cutting out at this point I can
see I'm lagging at least on OBS anyways
25% is you know if you were to choose
all one right or anything at random you
would probably have a 25% accuracy but
over time things get better so these are
just some of the graphs I took from
tensor board and I just put them in the
text-based version of the tutorials so
initially I just used 6,000 samples
things weren't really going very good
then I increased to having about 110,000
samples and then after some training I
got this and then this was the loss but
then if I zoomed in really really far
into the loss graph you can see oh it's
still sort of going down so so I let it
go for very much longer time and you can
see here basically what happens it just
kind of levels out and then it's first
of all so yeah very interesting so
anyway here is my over a much longer
time frame this was after 30 full epochs
the result so it definitely can learn 60
percent accurate
see on four choices is actually pretty
decent you would want it to be a little
better but then again the like the model
and the actual structure of everything
is it's pretty crude here so 60%
accuracy I'm taking them as a victory
anyway
full code is is in the text-based
version that's tutorial if you need it
in the next tutorial we're gonna be
talking about is is using the model so
actually using the model is not too hard
we should be able to run through that
pretty quickly and then we're gonna talk
a little bit about a lot of the problems
at least the ones off the top of my head
I've been taking notes throughout this
entire kind of segment now of all the
things I kind of want to work on and
stuff like that and that'll be if you
have any suggestions for things you
think we should improve that would be
the time so start thinking about those
things anyways if you've got questions
comments concerns whatever you know the
deal leave them below otherwise I'll see
you in the next video