what's going on everybody welcome to
part 10 of the machine learning or deep
learning and he'll like three series up
to this point we just kind of started
talking about our con sense making our
imports talking about how we're going to
structure things let's just go ahead and
jump into it so at this point we've
shuffled all of the files and we're
ready to separate them out into training
and validation files they're already
shuffled so we can just iterate up to
whatever the you know validation amount
is for the validation stuff and then
after that do the training also we only
need to do that once we don't need to do
that every time through the epoch so
like I said I'm not going to use the
tensor flow generator stuff because I
think it's just over complicates things
it's just not really well done for the
user in my opinion so we want to make
sure we have some logic that allows us
to just load this data one time even if
we want to you know because basically we
can also set this to true once we have
those files and we don't have to load
them at all but if we do need to load in
the data we just want to load it in one
time not every time per epoch all right
let's get to it
so first of all we want to say give if
load train files this means we have to
load them so now what we want to do
before we even enter the epochs is we
want to load in the validation files so
if load train files so if we happen to
have them then we can just say test X
equals NP load test X dot npy right
that'll be there it's not there so we're
gonna have to this like because this is
false anyways but if it was wouldn't
that be nice we could load it but we
can't therefore else so now what we need
to do is basically we'll start with two
empty lists test Y and then we want to
iterate over all of our files so we're
gonna say for F in TQ t cubed DM we're
just going to use this to like let us
know where we are in each process and
then that's going to be training file
name
means up to the validation game count
so the first what is it 50 I think we
chose yeah first 50 that'll be our
validation data so then what we're gonna
say is data equals NP load that F that
file you know that data it's an entire
game so it's a list of Lists where the
list itself is a bunch of these you know
it's not really the game visualization
but it sort of is it's just all the data
from each of the coordinates basically
on the game map that we're curious about
so that'll be the so the zero element is
the 33 by 33 data and then the first
element is the move that was associated
you know what what move did the agent
make based on that data
okay so now so data equals NP eluted
okay so now what we need to do is we
want to iterate over that data so for D
and data we want to test X dot append d0
and then test Y append d1 and that's all
good and also probably Upen NP array so
the Y's don't actually need to be an
umpire array but I'm pretty sure the X
data
I think tensorflow is gonna gonna bark
at us if if we don't go with an array
for all the X data okay once that's done
MP dot save and whoops oh man I hit my
insert key or something what what is
happening there we go test X and I'm PI
this is like the same time as the
previous tutorial where I made an
egregious mistake so just go ahead and
expect that this is not gonna work you
know really any centex video just really
keep your standards very low and
everybody stays happy
okay so in this case now we save those
files that later we would look for if we
believed we could load it okay so we've
done that and we
get away with that because it's only 50
files and even when the files are like
50 megabytes each or whatever we can
still easily load in 50 but what about
in this case where you know we've got
like 3000 of these files that we're
gonna use and while there are only one
megabyte that's no big deal right
but when they're 50 it's a little bigger
of a deal and then one if we even have
even more games like what if we have
10,000 games that we're gonna go with at
some point we got up we have to chunk
these and load them in as batches so
that's what we want to do now the next
question is how do we separate these
into batches well you know there's a
bunch of different ways we could slice
and then use some sort of counter that
keeps adding you know plus 50 and slices
based on that you really could do that
or the thing I always do is I go to
google.com and then I search chunk split
let's say split a list into chunks
Python this will probably get us where I
want to be how do ya click on this very
first result and this is a beautiful
generator for doing just that you passed
the list and then you pass how many
items do you want each of the list and
it will just automatically chunk it and
then you can iterate over those chunks
very nicely I just I use this all the
time I find this to be one of the most
useful Stack Overflow copypastas that
you could possibly make so the other
thing I'll do is just copy that just in
case I post this code somewhere sors
blah okay so great well done everybody
good chunking okay now the next thing
that we're gonna do is define our neural
network now in in the case you know you
might already have a previous model just
like before so if load prevail
so if that's true then you can say model
equals TF caras dot is it models I think
it's top models yeah dot models dot load
model and then prevail name easy as that
we don't have a model so it's
and then we need to start specifying our
model so model equals sequential we'll
go with a sequential type of model I
think we're gonna steal from myself to
do to do um let's go care us again and
then it was what part three part of me
just wants to just take a copy and paste
that code let's go to the bottom
this looks good huh we don't want the
binary cross-entropy but the rest should
be fine so I think what I'm gonna do to
do everything down to that final dense
layer I think is what we'll take so we
will do this probably let me just copy
that this is probably not gonna end well
but hey nothing ventured nothing gained
oh that looks like a regular tab as well
let's fix that oh no no I thought we
fixed this before what's worried oh my
gosh this is the whole thing I could
have sworn we oh this is uh this is
really painful I thought I already set
this why do I got to do this again I
just now got to fix all this because
it's gonna probably get angry at me but
uh why I got to do this this is super
annoying
there's got to be like a better way to
translate all this stuff but I'm gonna
do all these that's really weird I swear
in not blows my mouth
I swear in one of the previous tutorials
I fixed this already I'm not sure why
this one suddenly has real tabs anyways
definitely want spaces so we can trans
transfer it to other other places okay
where was I so we probably don't need
this let's do a 64 64 and let's do let's
do a 3 by 64 activation can stayed
rectified linear that's fine
flatten to a dense 64 will keep
activation sigmoid we don't have one now
we're gonna go with five options and
then we need to choose sparse
yeah sparse categorical cross-entropy
on the fitment also at probably let me
think here on X what we just don't have
X also by this point we won't even we
won't have any of the X's so in fact
what we probably need to say is test X
dot shape so we definitely need that
thing to be an umpire array at this
point test X but also test X itself is
not an umpire ray we need to convert
that to probably be an umpire ray let me
just do that here test X equals NP array
test
X
because we still can't get a shape
because we it's a list of numpy raised
up until we do this so we've got that
then we've got this random function in
there that's fine well we can clean this
stuff up later for now let's make it
work I think we're I think we're almost
done here so the only other thing that
has been coming up recently
padding equals same is for some reason
and the newer versions of tensorflow
I get a lot of these like shaping issues
so I would recommend adding padding same
to every convolutional in pooling layer
hopefully that is where I think that's
all where it's meant to be because you
have to specify and for some reason the
defaults have changed and then now if if
if everything doesn't add up perfectly
you'll get an error and it's really it's
really tedious that's something that you
would want to happen in the lower level
tensorflow but not in the higher level
tensorflow layers api I think that's
really dumb that they've like compacted
having that stuff have to eat like used
to not have to think about it if you use
the tensorflow layers and I think that
is a better choice personally but it is
what it is okay okay that's our model
fantastic so then we're gonna specify
our optimizer and that's gonna be TF
caras optimizers dot capital a Adam and
then we'll say the learning rate whoops
learning rate 0.001 so one a neg let's
just do it this way one a negative three
we might find that one a negative for
180 or five makes more sense I think to
start we'll get away with one a negative
three but we'll probably end up having
to check a few also we could set the set
of decay also I'll set that to one e
negative three okay
then model model comm pile and we will
say loss equals sparse categorical cross
and true P that's really long we need
like an SCC
shorthand I know they have it for like
MSE for mean squared error we need SCC
please maybe that's a thing now someone
try it let me know optimizer optimizer
equals the opt that we just defined and
then the metrics that will track metrics
equals accuracy fantastic ok so step one
is let's see if this compiles so python
python sent de Bono's not what i want
brick
python it was sent to sent to train not
the part nine but this one okay okay so
it looks like our model does compile and
we are ready to go through epochs back
to our code
lovely lovely code okay so now for e in
range
epub so whatever that was print current
currently working on epoch epoch e cool
so we'll know which epoch we are on then
what we're gonna say is training file
chunks that is equal to that chunks
generator that we built here and then
you pass ln n so the entire list is all
of our files so that's training file
names after the validation for I'm sorry
almost fun almost messed up there okay
so validation game count on word and
then
training or does a training chunk sighs
done
okay I said fudged by the way okay
training file chunks probably actually
just define that up here that should be
fine to put that up there because we
don't need to find that every epoch okay
great for index and training files in
new and numerate training file chunks
print working on data chunk ID x plus
one out of
this out of this / training chunk size
so that might wind up giving us quite
the long so let's say round that to two
decimal places otherwise that might be
like a super long decimal or something
and really what we should say is math
dot seal but I'm not going to do that I
just want the general gist of where we
are okay so now what we'll say is if
load if load train files for E is
greater than zero so if we've already
done one whole epoch we have the data so
we can actually say x equals NP dot load
and then we'll call this X - something n
py and this would be ID X as if I'm
asking you guys but yeah that should
that should be what you want copy that
paste otherwise y equals NP load Y under
the ideal scenario then else x equals
empty list y equals empty list for F in
T qdm training file you know training
files what do we want to do we're gonna
say data equals NP dot load F really
this is all pretty similar to what we
had before also we still haven't
balanced anything huh way we'll get to
it data equals NP load F for D in data
let's say X dot
and NP array d0 y dot append d1 okay
same code as we used before if anything
probably make that a function or
something but I'm not gonna worry about
it okay now we need like a balancing
function and this kind of sucks there's
got to be like a built in a built-in
balance that we could plausibly use I'm
gonna say underscore one so we'll do
this for each of the each of the lists
it's almost embarrassing oppression
opposed to this but we got to balance it
there's got to be like a built in
balancing function though such a common
task for X Y in zip X Y basically we
need to get to the point where all of
these lists are the same length so
usually what I'll do is I'll iterate
through the data separate them out by
their classifications then you go you
find the min length and then you make
all of the lists you just slice them up
to that minimum length so the lowest one
so kind of lame that we're gonna do this
but this is it you got to balance it and
like I said there's probably out there
some really great balance function and
if you know of one it would really make
my day if if you like if you know of
like a pre-existing one in chaos or
something that is super easy to use most
of the time I don't really like their
helper functions but anyways if Y is
equal to zero will do underscore zero
okay dot append what hap okay then we're
gonna do this again sir
start acting up on me now so this is
like one of those scenarios where I
really wish it was acceptable Python
practice to just do this and like have
this on every line like why can't we as
a community come together and just
decide that if we have a 1:1 very short
line after an if statement that we
cannot put it on one line it's okay I
really want that to be the case two
three four five if I wasn't showing it
on video I might shoot I went the wrong
way I see one two three four and then
this needs to be one two three and I'm
definitely violating another rule which
is anytime you got copied code you're
probably doing something stupid so also
if you have a way to condense what we
write here even if it isn't something in
chaos post it below because again I end
up writing these stupid functions like
every time and it stinks min Len so Len
underscore zero so it will be the
minimum of all of these lengths so dang
it okay that would be one two three four
okay so this tells us what the shortest
list is chances are these are all at
this stage oh and this also needs to be
a list so it should be a list of these
lengths okay so this tells us what the
shortest list you know the shortest how
long the shortest list is got it okay
once we know that then we can just say
zero underscore 0 equals underscore 0 up
to shortest so I say shortest is a
thousand it'll be the first it'll be a
thousand of all of these lists so then
we just do this for all of the classes
it's like this I feel like
there's got to be some way to be like
here there are n classes but the only
problem is sometimes your classes are
gonna be one hot vectors or arrays or
whatever you want to call them and then
other times it'll be just scalars like I
have here but you could probably figure
out each unique one and then somehow
assign it there's there's gonna be a
good way to do this somebody else
somebody will save us at some point
okay so balanced equals and then it's
all these put together so zero
underscore zero underscore one and then
let me just do this two three okay I
don't even know what I hit hey I thought
this was gonna be easy okay 0 1 2 3 4 so
that's balanced will do a random shuffle
balanced cool just for our records let's
just say the shore shortest file was in
this case I'll just use a comma here and
then what was it shortest just so we get
the number okay and then what we want to
say is X is equals this Y is equals this
and in fact I kind of want to fix this
point there I want a little more
inflammation ok the shortest file was
shortest total new total balanced length
is and this will be len balanced so that
way we get a good idea cuz went after
balance you might find that you have way
less training samples than you thought
so then so once we've balanced the
training data we need to resep rate out
the XS and the Y's so now what we want
to say is for X Y in balanced X's depend
X Y is depend why return X's wise whew
ok looks great
good work everybody and I think ok so
now coming down here what we want to say
is X comma y equals balanced X comma Y
and then test X test y equals balanced
so what we're gonna have to prop we're
gonna have to reconvert test x2 an
umpire right here don't forget that test
y so and then also it needs to be a
capital X and also we did
did we call it okay test X test why
we're summers I thought I saw a capital
X test instead but I guess I didn't my
eyes are playing tricks on me maybe okay
okay we're good so then what we need to
do is convert these all of these things
to arrays so we're gonna say x equals NP
array X I don't think Y actually has to
be an array but we'll cat will convert
it anyways why test x equals NP array
test X tests test y equal test y equals
NP array test why okay now let's do NP
dot save and then F whoops F string and
we're gonna call this X dash whatever I
is I think we're gonna check to make
sure that's right I think I called it I
but I don't remember for sure
why I write X - so it's ID x ID x
okay
probably you should make sure that works
but I want to go ahead and just finish
the the code up to this point so in this
case we're loading in the files for ID X
so then what we'd like to do like once
you have the files in we want to fit so
I'm gonna say model that fit X Y bat
size will be we'll say 32 for now EPOC
equals 1 because we're handling epochs
on our end validation data is equal to
test X test y and we should do callbacks
at some point but I think we'll add that
at the end let's just see if this fits
and depending on the error will probably
have to cover it in the next video
because this one's getting to be really
long sent to Train I also probably
should make I should probably make these
chunks a lot smaller so we can dev a
little quicker if we if we do hit an
error I probably I'll probably have to
do that this is already taken forever
I'm bored so for now I'm just gonna soon
I'm gonna hit in there so I'm gonna
change this coming back where are we
here okay balance X Y X is not defined I
assumed we would hit an error and gosh
darn it was alright so balance needs to
be capital X try again idiot
and also our fitment I kind of messed up
there as well another lowercase X this
probably this should be out one I think
so for yeah so this should not happen in
here cuz that was under the else
statement but then we would never train
if we loaded a file um um let's see
where was the other silly lowercase X
right here save let's come back
run it again try again idiot - I really
do 200 do ten and a hundred save we
didn't save the actual array I'm just
gonna lower this yet again let's make it
fifty so it's even quicker
we're just I'm just trying to figure out
like get this to work so in this case we
want to save X capital X and then here
we want to save lowercase Y again I
would really like to see some training
going on that would just make me so
happy
I expected way more errors I can't
believe we're training already guys
okay so accuracy right now is pretty
darn bad but well that we should expect
that accuracy will be around 20% don't
forget we're training on random data
here so it we should only hope for
slightly better than random results so
the fact that we're already at 22
percent accuracy now 25 was my
out-of-sample oh my gosh
uh-oh is he it's moving too fast 26%
validation accuracy 28%
I would not even trust that at 28 we
really should be like slightly better
than random hopefully we don't I want to
start freaking out if we see like a 30
or something we should be pretty close
to random is my point but we do have a
pretty good threshold but we just
shouldn't be that far at least on the
first iteration but this is looking
pretty darn good okay so what I'm going
to do is I'm going to break it here
because these are way too small of
pieces also the validation set is
arguably a way too small so we're going
to go back to our original 50 and our
500 and then the other thing we should
be doing is come on in
after the fit so the fit is through
every index you could either save a
model every iteration or every epoch you
can decide what the heck you want to do
I'm guessing only one epoch is gonna be
required but definitely I would save at
least every epoch and at least I'm gonna
do that but a part of me says might as
well just save it every iteration so I
would say model dot save it was called
model name or something like that wasn't
it maybe it was his name was it just
capital name yeah and I can't remember
if name has a time associated with it or
not it does cool um okay good enough for
me and the next thing I also wouldn't
mind doing so model that saved name all
so let's save that two models slash name
cool so we'll also need to create that
models directory new models very well
very well
and finally this tutorial will end at
some point at this probably before we
load in the model let's define tensor
board and that's going to be equal to
tensor board object and then we specify
a log dur equals and again F logs slash
whatever the name of this model is and
then we come down to where we do our
fitment and we need to add in the tensor
board call back so should be callbacks
callbacks as a list and we want to pass
shoot I think it was just lower decays
tensor board if I recall right I mean
[Music]
yeah yeah okay tensor board come up here
and confirm tensor board I just wanna
make sure I didn't type of this we were
really angry if I if I hit some freaking
error at this stage okay all right I
think we're all set
so models and then I can't remember if
you have to have tensor board the log
Durr made so I'm just gonna throw in
logs as well just in case and now let's
go when to cut the video here if I hit
any errors for whatever reason along the
way I will throw in a little update at
the end of this video otherwise I'm
gonna let this train we're gonna check
out some of the results I might do some
tweaks or whatever and in the next video
we'll talk about the results and then
hopefully have time to apply it to you
know actually start training you know
making new games based on that model and
all that stuff so that's what you guys
have to look forward to if you have
questions comments concerns suggestions
a better idea for the balance whatever
feel free to leave those below otherwise
I will see you guys in the next video
and I kind of want to let this epoch
finish does the sea but I think it's
hopefully I'm gonna be really shocked
for like beyond thirties percentages but
okay validation and we actually got a
better validation accuracy then
in-sample interesting okay see you guys
later alright it wouldn't be a Centex
video without a mistake in an edit at
the very end so I was trying to compare
you know I ran the first epoch which you
know really we did we stuck around the
the range of 24 to 28 which for you know
validate well actually I'm looking at
the wrong thing let's look at a puck
validation accuracy we got a relative
and we can see where we where we've
stayed at somewhere between 24 and a
half and 25% accurate which in my
opinion is actually pretty good so those
are the results but I caught I was
trying to do multiple epoch
and I stumbled on hello there we go
this so what's going on so the first one
I set it to run for three epochs this is
epoch zero and that just skips epoch one
two it doesn't do anything the issue is
our generator I moved it yeah thinking
and that was the smart thing to do but
no that was the stupid thing to do what
we need to do is put this back where it
was
so for each epoch we need to specify
that generator because if we don't what
happens is that it becomes empty so so
we need to put it down here despite the
fact that we're loading the files only
we actually only need to load them one
time
there's probably actually even a better
way because the only really you really
this should be up here and then maybe
this here needs to be something that
calculates how many chunks there would
have been or something like that but I'm
not going to worry about that for now
what I am gonna say is the easiest fix
is to just throw this back down into
there and then now we should be able to
get three full epochs just for
comparison's sake I just want to see
more epochs and see does that help does
it do anything does it hurt or whatever
so back to training ok see you guys the
next video