all right hello world this is cs50 live
where we do all kinds of things we
program things from scratch look at
technical concepts we looked at kali
linux last week yeah not super exciting
long time ago my name is called this is
cs50 znik Wang Oh what what are we
talking about today
yeah so today I think as advertised
we're talking about images and machine
learning and classifying some of the
three like key buzzwords they're thrown
out there's kind of a fun non buzzword
but a machine learnings out there
classification will actually be doing a
little bit of unsupervised learning as
opposed to supervised and things like
that
I think previously on the stream we
talked about a binary classifier they
could tell you if something was like I
think a cartoon that was your first very
first very first thing they did with the
episode like four five
my mouth I see that dongle joke is back
on the and people actually put up a
website that somebody asks we're gonna
have captions for this video and we are
in the process of captioning all of our
streams actually so this one will have
captions eventually maybe within a
couple of weeks or so and shout out to
all the people that are currently in the
chat that's really going to shade it
chance very poppin they're gonna caption
my incessant laugh yeah ha ha ha we're
gonna grep laugh and they're gonna be
like oh your shirt by the way is very
ethereal thank you yeah I feel very
light today you know like it's kind of a
nice ish day
there's a really nice day on Sunday I
was awesome Oh true true wreath eautiful
in very wise to me 17 and messages is
freezing bad by the way no they want
yeah
but sorry sorry dude yeah it is
beautiful so we talked about
classification that ties into sort of oh
yeah right so we classified kind of
between two just different groups that
were completely non intersectional and
now we are trying to give it a set of
data can we figure out how many classes
there should be right more or less is
this sort of alluding to we serve
publicized k-means clustering yes
so k-means for those who are kind of
unfamiliar with the term it's a form of
unsupervised machine
unsupervised meaning I don't tell it
what the right answer is it just figures
out what its guess as to the right
answer is and in this case that
manifests itself as there are K clusters
if you will of data and so let's say I
have my entire data set and actually
we'll just talk about the data set we
used today which is I think around 40 or
50 images so really not that much not
that much data but it is 40 or 50 images
and they're I think 4 or 5 classes and
those 4 or 5 classes are the different
streams that we've done that have the
same little box so you've seen lots of
screenshots of one stream throughout one
stream so within each cluster there
should be around ten images and those
ten images should be all from the same
stream
so essentially what our classifier will
want to do what a k-means classifier
will do is it says okay I have this
large data set and tuning I'm going to
just grab a bunch of random pieces from
the data set and those will be my
initial clusters so the initial means
are pretty random they're all roughly
the same ish
you know it depends on the distribution
area data but they're pretty reasonable
so we just kind of grab some random
edges throw them into a cluster and we
say all right now tell me which image is
or I guess actually for each image which
cluster is that image closest to by some
definition of distance so on the string
today will probably use Euclidean
distance it's pretty common it's really
easy for everyone it's just geometrical
XY I just do like X minus X well not
squared plus y minus y naught squared
and I take the square root of those sums
those sum together and that works in
many distance formula right the linear
distance your favor great like there
like some of the great concepts first
same right same concept and so will
basically be doing that and every
iteration of the algorithm it says hey
you know for each image what cluster are
you closest to and then I'm gonna stick
you in that cluster and once you're in
that cluster I'm then going to recompute
the new mean of the cluster and so
things that were kind of naturally
closer to some set of images they're
going to end up kind of clustering
themselves and those means will get
closer to the images that are all in
that cluster as you iterate over the
algorithm on one of the benefits of K
k-means clustering is it actually
doesn't take that many iterative steps
you can usually do it in a couple
like 10 or so so you really just kind of
iterate over it you know on the order of
ten times and after that your loss
doesn't really get any better you tend
to stick around you know it depends on
you situation it depends on what you've
built it and come into randomization
parameters you have in there but in
general you don't need that many
iterations to get a pretty accurate
representation of what things look like
one of the kind of cool parts of k-means
is I have this kind of center of a
cluster and so I would generally just
refer to that as like the mean image for
that cluster and those mean images are
often representative of what is in that
cluster so let's say one cluster is from
our Kali Linux stream then if I take
whatever the mean or the center of that
cluster is it should be pretty
representative of what we on average
looked like in that stream now I haven't
we haven't tested these right so hey
we're pretty sure that it'll work
reasonably well but we leave ourselves a
little untested to kind of verify that
things are kind of funny and it gives
you guys some room to experiment I guess
and we get to experiment and show you
guys cool things so you will see some
kind of entertaining stuff with our
k-means clustering but our prediction is
roughly that you should see what we look
like on average on a given stream and
given a new image it'll tell us which
cluster it should have belongs to and
it'll then classify for us do we usually
like manually choose the start of the
cluster or do we just kind of randomly
or how does how do we get to the
beginning so that's a great question a
lot of times we actually initialize it
with just completely random beginnings
so we just say hey here's my data set
I'm gonna use like a numpy application
to randomly pick some set of images or
randomly subdivide the images into these
clusters so our initial can be random
but there is another version of this
that is called semi supervised learning
so let's say you were able to label
about half of your data before your
research intern quit and by you label
your data I mean your intern labeled
half your data and then they quit so you
only have you have this data set of you
know a million images and 500,000 of
them have been labeled manually by some
poor college student and the other
500,000 have not been labeled so this is
where if everything was labeled
originally if all million images have
been labeled then we would call that
supervised learning
we know the answer and then we have some
test data set or some prediction kind of
way of accumulating things that we want
to predict upon and those are unlabeled
but we're going to use our trained model
to then predict on those now in
semi-supervised learning we have this
where maybe half our data is labeled and
half of it is not and so we take maybe
the half that's labeled and we assign
them to the right clusters so this is
where we actually start with like all of
this cluster we know this is the right
class all of this cluster is all in the
right class and so on but the other five
hundred thousand images we don't know
which cluster they belong to so we'll
then iterate this algorithm over those
images and say hey which cluster are you
closest to and so that can be a way of
like tightening up what your k-means
classifier will actually do but it gives
it more data which allows it to spread
over you know different kinds of
variants or whatever the underlying
distribution of those things are sggs
day before was saying even the second
guy seen hit boy no I was actually I
just we had a bug with our Facebook
deployment so I was just focused on
getting us streamed to Facebook which we
now are I guess as we were initially
published publishing this OBS sort of
bugged out and caused the fate to know I
was sorry I was like zoomed in on
getting that focus but yes sir or he or
she asked another question earlier which
was or was it I'm proficient in Python
but no nothing
ml is this stream for me would you have
to say but yeah I think this is a very
reasonable stream even if you're not
super great in Python or if you're like
very adequate I would count myself as
like decent in Python this would still
be pretty reasonable for you so our goal
is to kind of at the beginning we're
gonna give you this kind of high level
approach like what are we gonna go
through where kind of the concepts but
then we're gonna repeat those concepts
pretty much throughout the stream so
that you really get a sense of what
we're actually talking about so for
instance if you're really like you want
to do this yourself then you can code
along we should be going at a reasonable
speed you can always pause the video go
back to reiterate and that should be
easy for you if that's how you learn if
you learn through listening to what we
say then maybe a different iteration
will help you if this one doesn't work
if this makes perfect sense to you then
that's great there are many different
ways that we're going to try and
attack the same problem and I saw
somebody said please also talk about
limitations of k-means clustering as in
if there are two concentric circles
kinds of data then your k-means
clustering might have some problems with
like identifying which one is which so
for example let's say I have some data
set and then I have some other data set
that is a subset of the other of the
original data set is it this clustering
method doesn't really work super well
for classifying between those two data
sets right so let's say I wanted to
classify between like all kinds of dog
versus like poodles for example right it
might be a little bit tricky for me and
actually that might not be the best
example but it's kind of the idea which
is that I have the subset of the data
that I want to kind of classify as
separate from the rest of the data and
that can be really difficult for k-means
because k means by definition is really
just looking at like what mean image can
I get out of some data set and how can I
find things that are closest to that
mean by some measure of distance and so
for something that has kind of on
average that the mean image of the
entire data set is very very close to
the mean image of the subset data set
then k-means can't distinguish the two
but maybe you could look at other
feature discrimination algorithms so a
lot of complexity it's very interesting
but I'm very excited to learn I think a
little bit yeah I'm actually I'm gonna
transition to your laptop view yeah it
works to make sure I can see everything
and while you're doing that at the very
end of the stream we will hopefully have
a little bit of time to kind of sneak
peek what will go into next week which
is using an actual neural net to not
only classify images and understand like
which parts go where but also generate
new ones based on what it knows and
that's what we call a generative model
okay and then potentially in
degenerative an adversarial members yes
and eventually we will hopefully get all
right I guess not eventually after maybe
that we will hop into generative
adversarial networks so this was just a
single parts jam this one having my
brothers have many parts yeah but yeah I
think I think it'll help people to get a
I think is groundwork understanding up a
lot of the pieces yeah and so I think
that it's a very good point to recognize
that there are limitations to this that
the next
dream will kind of cover why there
aren't necessarily as many this is a
beautiful screens here we have for the
hands in a brand new right this is C
matrix I piped it so on OS X I piped it
through wall cat wall cat rainbow Phi's
things C matrix makes things it makes
kind of like a matrix tile screensaver
I'm a huge fan using it I don't know who
created it but I guess we're kind of
doing free advertising forum they're
really cool it's a cool project I'm a
huge fan of using it I think it looks
techy and anybody wants to look like
their true fan of sci-fi fantasy hacker
you know you have to code you just leave
that on there people think you know so
what is the first step I guess in an
actual at coding of today what are we
gonna look at right so you know let's
actually look at our data set just
exactly Batman image that was cool too
thank you actually these are all from
unsplash all of my desktop images are I
really just use that I'm learning react
and we use the unsplash API it's really
nice super well very very clean API so
we have this kind of four classes of and
maybe I can make this bigger
No okay is it for important questions
that are very simple oh this is a this
is a Snapple so we're also advertising
for some people this is raspberry tea hi
pretty much every stream grab a Snapple
because a lot of times I'm running over
here
and it helps me sound a little less
awful yeah harness on just a tiny little
bit of caffeine exactly and then just
like it's you know ya know in case I'm
not energetic enough I'm not really yeah
I also do love root beer but I'm trying
to avoid drinking too many sodas so yeah
a kind of interesting tangent to your
k-means classifier so if we have these
four different classifications of data
sorry I thought there might have been
five you're only four I'm just basically
there's there's two all I don't know
yeah there is all it is unfortunately
not a separate class this bro umbrella
guys it's kind of like everything and so
we basically we know the answers but
we're not going to use the answers so to
speak we're gonna do fully on supervised
learning to start but if you kind of
look through some of these images
they're literally burnt oh we look yeah
we're
whoopsies yeah we we look great in every
image I had fortunately did not have the
foresight to select for appealing images
but there are images of us and so you do
have many different images pulled
throughout a stream and it's the same I
notice it's the exact same dimensions
yes yeah so I normalized across
dimensions to make our lives a little
easier but I didn't normalize across how
we how much of the frame we occupy sure
so I did very minimal pre-processing in
that I literally only just made sure
they were the same size okay
the same size the same like pixel
qualities so alright we have the same
resolution on all of them but other than
that nothing else was done to crazily
now these first two are fairly easy to
distinguish we're wearing very different
colored shirts and we're in other
positions however these last two you'll
notice that we're both wearing black Wow
and the background is not particularly
easy to discriminate against yeah so we
kind of picked two things that are
pretty easy they'll be standout kind of
outliers almost there means should be
pretty different
these ones there means don't look that
different now we move around a lot and
how means translate as far as like
machines a concern is a little different
but you'll notice that the CTF stream
and our cally stream we were both
wearing black with the exception of the
white we're gonna auction off of the
machine with my fork yeah it might
literally be that like your glasses
out in that same yeah human beings
there's a Colton has glasses there's a
very shiny quality too I think both of
our foreheads in the one and I have a
logo on my shirt that definitely helped
the hair is pretty distinct I suppose
and today it looks like trash everyone
ever has those days and you know
you know exactly I'm not as any
suggestions on books about ml oh that's
a good question so there is what is the
name of that book I have a textbook on
my desktop somewhere that I really like
it is called the yeah now you're gonna
see all of my other weird things so
there's this bishop pattern recognition
and machine learning Springer 2006
textbook I have a pdf version of it but
I think that that's a pretty great book
for kind of going through it requires a
decent amount of math background and
like stats background but if you're
comfortable with like what a CDF is how
to convert between CDF PDFs what like
standard CD f's and PDFs might look like
how to like go through different
distributions how some basic linear
algebra concepts work so kind of like
different matrix operations if you're
familiar with like The Matrix cookbook
by MIT then this book is probably pretty
useful for you if that all sounded
really esoteric then that's fine I would
go online and kind of look at some kind
of basic tutorials medium is a good
place to start with like some very
simple tutorials kind of like what we're
gonna do right now wow the Internet's
amazing you guys just found that so
quickly okay and so there's those places
adamantine bipartite says try scholarly
articles for ML Google Scholar that's
definitely a great place to start be
aware that there is a lot of terminology
and symbolism that might earn notation
really that can be really intimidating
at first I wouldn't get like try not to
be intimidated by them they eventually
they just take time but I would
definitely recommend kind of like
looking through what things are out
there there's all sorts of cool papers
all the time on different things and a
lot of times those articles about those
papers that are even easier to read so
kind of you know go as you will I
realize a curious game is asking so what
do you guys do we didn't really talk
about the frame of our new viewers um
cs50 is Harvard's intro to computer
science is taught by David Malin who's
not present here in the stream
physically but might pop in a little bit
later yeah my name is Colton and I work
as a full time technologist and I also
do this twitch streaming and a bunch of
other stuff
programming other related things and I
think and then I actually was on cs50
staff for two years or so and that's how
I ended up working with these guys and
then
the rest of my time is spent being a
student no I'm just a full times to
remind me of what your I don't remember
100% if you were doing CSS your
concentration right yeah so my major I
actually have a joint or a double major
in bioengineering and computers and so
I'm doing both and yeah most of my time
is spent studying I think if I didn't
have to spend all of my time doing just
p-sets constantly problem sets for those
who aren't familiar then I would
probably produce much more high-quality
preparation on these streams but that's
okay I think it's still pretty pretty
entertaining this would be fine and
you've taken an ml course here yeah and
I'm also currently in our mo of course
actually
okay so I've actually tasted taken
mostly like systems courses so I focus a
lot on like systems I really like that a
visualization but I haven't gotten the
chance take a course in it
yeah actually wow I've really focused on
systems so I'm fairly familiar with
systems if you want a really bad version
of my C program and you can go watch our
C Perkins the point of that stream was
not my C programming but we did end up
kind of going through some C programming
it's pretty bad but you know it at least
that way you get to see me code very
poorly if you think that this is really
good if you think this is really poor
don't go look at that you'll probably
cry oh yeah but I really don't know
where I think I just kind of got
interested in ml as actually in cs50 I
was kind of like oh I machine learning
seems really cool my TF was kinda like
yeah you should go go for it you know
and I tried some very high-level
libraries I didn't have to understand
much about the theory and kind of from
there I hopped into a bunch of articles
and I started building little examples
myself yeah and I think I built like a
small room camera that upon someone from
my family entering my room it kind of
like identifies who they are and
notifies me you know it's kind of just
for fun a lot of times you just build
projects and as you go things kind of
pop out of the woodwork
but yeah the Arase ts interesting
question was do you have any hot takes
on whether the hype for going in the
future of ml is justified just beginning
to learn the language and you're gonna
go work for it yeah yeah
so it's kind of an appropriate question
I actually really like going and I'm not
super familiar with it yet but I do I do
intend
unbecoming like that on becoming much
more familiar with it
so going language events by Google
sometimes referred to as just go not to
be confused with the board game and
golang is a very interesting language in
that it still allows you to have access
to low-level things C++ style C style I
can still touch memory and access
primitives but it has a much nicer well
a lot of people believe and this will
set off a firestorm somewhere but many
people believe that it has a much nicer
interface kind of a lot like Python or
JavaScript then do like Siebels plus and
C this was a garbage collector it isn't
it does it has a very nice garbage to
collector so for those of you who aren't
familiar garbage collection is something
that's really popular in like Java for
example well really any like modern ish
language with the exception of Siebel
sports
like basically it kind of takes care of
certain things for you there's a life
cycle of a given object so for like
object-oriented languages you take
something where you create an object you
instantiate it and maybe you kind of all
the references to that object disappear
there's a garbage collector that
prevents that object from just floating
around forever in active memory or main
memory so golang has a nice garbage
collector go also has a really nice
feature that I haven't explored a whole
lot in it's like version of threads and
threading they're not as heavy weight as
like a C++ threat staples close I spin
that up and it's got its own stack and
it's doing all these things and it's
pretty heavy it's heavy memory usage is
have you can see V usage whereas going
threads are actually a little bit and
what I mean it's do the language very
much design for distributed system so
that makes sense yeah it's like it's
number one one of its their
consideration so they do a lot of very
cool things in going and actually yeah I
would recommend going and exploring it
may be kind of like if you were looking
at like Python golang in C++ you'd
probably see a lot of the different
differences in similarities if you
wanted to kind of paradigm shift and
test your ability to switch how you
think then maybe looking at like
something like Python versus something
like oh camel or closure or I think F
sharp maybe it's like functional
and I actually in industry were at least
at the industries that I've worked in
I've generally preferred functional
paradigms um because it makes it really
easy to test
whereas object-oriented paradigms it's
not that they're difficult to test but I
generally find that people are a little
less stringent in their testing
practices and that tends to lead to more
brittle practices and eventually no one
condemned your code I mean we've talked
about it I really like closure I want to
get like really good at closure like I
would to you know my growing popularity
and I mean I think functional
programming is and always has been I
think since its inception
I think the languages have more you know
recently especially they started to
adopt functional features even Java
which is a thing go figure
but c-sharp is like so great a great
with that with like link varies too much
but but you know I'm more involved in
teaching game dev course here and I'm
into game development recently been
considering the idea of making a game
completely in a functional like
enclosure or so yeah which I can be
fascinating cuz you're telling with
you're doing with either either the idea
of taking your entire frame and
performing mutations on it as right as
your state object we're taking just the
what you would take in like a react app
or just a general application like all
your entities important information
entering that is your state object
performing operations on that while so
having sort of sequential rendering
logic so I was like I think it would be
Oh an improper disclosure is about the
best-paying language to know according
to the new SF insights knew about that
too that's no cool doesn't happen to be
the main reason that I want to learn
closure but it's a nice little you know
next bonus we get to and get to hop into
a nice high-paying industry but what
you're saying is true I mean like doing
the state with object-oriented
programming with really large
applications and games are a prime
example inversion you don't know what's
manipulating what and especially when
you become so many side effects like the
nice thing about functional programming
is that it scales to multi-threaded
systems incredibly easily because
there's no state so I just just like a
GPU operation so your entire cluster
yeah and back to the person's point Oh
talked about golang and I think that
might be an important reason as to why
it has a feature in ml being able to
spread out something very easily to and
very performant leo
pipeline and put it down there were no
machines that's a point you could
essentially model a neural network with
just your machines right yeah so I think
it's actually it's pretty important like
you you'll probably want to stay abreast
of like some of these kind of heftier
languages that are sticking around
I mean problems a good example because
it's so multi-purpose but if you're
looking to maybe have some more
performant systems and people would be
like oh you can transpile Python into C
and like this all things you can do
that's true but like I mean if you
wanted to just out of the box have a
slightly more performance as well
actually amazingly more performant
system it'll always be worth your while
look at C++ but golang has many of the
properties that I think will end up
being important in the future such as
distributed systems and well yeah
actually really just this like
distributed systems paradigm where it's
like we can spin up many different
systems and that kind of surpasses our
hardware limitations yeah Moore's law is
like Jason and at the transistor level
is not going not old not really
continuing what they're saying will
continue but it will continue at scale
it's going to be a horizontal scaling
issue rather than individual cores yeah
which is really I don't know it's a very
interesting shift that we're starting to
see a world you know we're all in it so
yeah I also trucha nice ass is closure
and closure same as this thing they do
sounds the same sorry a closure is like
you you have like function closures it's
more of like a related to programming
across several languages whereas closure
it's a play on the turn yeah it is a
play on the tape it also uses closure
script uses the Google closure
javascript is a library so there's all
these close like yeah Google one or both
and you know at programming language at
the end and Google pop about like you
know you'll figure that yeah as Bobby
Knight says functional programming is
very easy to verify things and that's
it's pretty much a exact I was gonna say
side-effect of the fact that there are
no side effects right it's just super
useful and there's been articles written
about this too now like theoretically
this is the reason for software bloat
and whatnot yes how do you ever worked
in industry and you've seen a piece of
code that you're like why does this work
the way it does where the unit tests why
are there no unit tests who wrote this
chances are that they wrote in OA P and
then didn't test it
it'll probably work and so it's like not
robust it does no no standard behaviour
all of a sudden defined behavior is
undefined and not documented and like as
a stage man and I'm actually huge fan of
like language is a functional languages
tend to pride themselves on being self
documenting so like and I don't know how
true that really is but it is kind of a
nice feature of the language that like
as you write the code it documents what
it does if you kind of follow some
standard sinc function a functional
programming there's written is written
thread declaratively and that's a word
that's was I didn't react and react is
actually a very functional especially
with redux it kind of adopts that sort
of functional paradigm but being able to
write out explicitly what how like the
operations that are taking it's
basically the verbs versus nouns
argument right with program which is it
as a student of CS and you switching
between all these languages doing what
it gets the why does everyone write
things like this yeah so yeah there's a
what you honestly have an entire stream
I'm just discussing these turnings it
just chopping yeah sorry we will we will
hop back in the game in so for a couple
more questions and then so general
flavor
how are you guys I'm 28 yeah I'm 20 and
Nick's already better at cutting the
knife itself
oh that did probably not sure does go
Lane have parallel programming I'm
inclined to say almost certainly yes
yeah that is kind of a pride of doing
that is kind of it's one they are one of
its greatest features I have is it's
very solid parallel programming all
right cool yeah let's let's get an we're
gonna happen to k-means see lettuce I
created a little file here there are
some things in the file I promise this
not much and we'll go from there so I
think we did yeah that's the one and I'm
gonna use Visual Studio code for those
of you that have watched streams before
I've kind of camped up my Visual Studio
code a little bit like just the Rocky
Horror Picture Show
sort of a semi back there every time I'm
gonna make this little bigger I was
actually doing this a little bit last
night with my glasses
well actually I couldn't wear my glasses
I had like stuff on my face and I had to
like make this an enormous screen I
couldn't read it I was like oh this will
be the same same size everyone sees
tomorrow so our guys so you can have
anything on the right side of your head
être probably not obviously I'll keep
things off the right side of my head of
it so that way people can consider
so we have this import numpy as NP very
standard paradigm in Python we also
import matplotlib pipe lot I'm a huge
fan of pipe lots very easy simple to use
and these tools are typically assist not
necessarily machine learning specific
tools but tools numericals kind of
generally yeah scientific computation
without another one you might sees like
sci-fi that one's pretty common too
there's a lot of just like these
standard libraries that are very easy to
use and you should become familiar with
them the other one is pandas for like
data scientists I can't learn as well
yes it's it's another big ml one and if
you wanted to hop into like even lower
level and manami not lower level but
other ml there's like PI torch is really
common one and tensorflow
zoo network right you will be using
tensorflow at some point yes yeah we
will actually see at the end of this
stream we'll see something that's built
entirely in tensor flow and I'll kind of
walk through what the code does I'll
point you to the guy who built it I
didn't actually build that one but
that's okay
and and then next stream we'll build a
version of that ourselves that is a
little bit more customizable
and then we this is just kind of an
example of like how I can load in an
image and display it so that people are
a little bit familiar with that and then
we're going to hop into generalizing
this and then we'll walk through my kind
of steps for like when I'm working with
images shout out to David J Malan in the
chat if anybody is branded to the stream
doesn't know David is the instructor for
cs50 here at Harvey University so shout
him out if you are new or and if you're
not new and you've been here before
still say hi yep thank you for thank you
for stopping by and I see up there
journal flavor says have you guys been
coding since you were kids since I was
like 14 15 yeah I've actually I started
here I started in cs50 so about two and
a half years ago or so well we're
getting the like three we're getting a
little but yeah I messed her with a lot
of hardware though as a kid I really
liked like putting transistors that's
got the spark yeah and that was kind of
the start and then I was kind of
introduced to cryptography and secrets
and puzzles and then I was like well you
know what I'll take cs50 and I loved it
and now it's half my degree
look I'm like David started when he was
like in I like computers and stuff but
he wasn't like set on CS till he came
writer and then
instructor and many end up teaching that
yeah it's a while time it's always
interesting to ask people their stories
for anything that they do
alright so we grab an image I'm gonna
use my own standard naming conventions
but if you have a problem with them feel
free to let us know I mean who wants to
write you know that pot lab - yeah you
know this looks ugly as heck and I stick
to conventions instead of just my own
conventions but like people think
conventions are really not conventional
ironically oh the CWC that's getting
height width and something channel yeah
okay so I I don't think people actually
read that a see very often I did come on
Lizzie oh so generally don't use single
variable single letter variable names
list though like extremely obvious like
I for index is pretty clear
I was thinking color but I mean color
channel it's kind of related its they're
related topics so it like kind of works
but if you want to be really clear you
type out channel and with tap complete
it's really not that bad but I'm super
lazy and then agent W you probably
should leave those as like heightened
with those are pretty soon they're
pretty safe gonna be pretty standard and
you can into it we're talking about
images it's like okay so this grabs a
single image so I use the Matt plot
library to pull up that image it's a PNG
and then I grab its shape and I print
that out to us so we can see what they
look like at least as far as shape goes
and then I wanted to demonstrate dumpees
reshaping parameters because that's
extremely useful to us a lot of times
when you're working with images it's not
super convenient to use like the 28 by
28 by 5 image dimensions it's much
simpler to just use like every row is an
entire image and it's just laid out and
I mean in a way that's meaningful to the
machine but not to us you know we don't
care so this reshapes it into exactly
that it's just a single string or in a
string in a single list or numpy array
of just every piece of data that was in
the original image really maybe it's
Leah and everything she's been laid out
single dimensional list yeah and then
this just demonstrates putting that back
into the original shape and then we
display the thing that has been we
picked back up so I can run this we use
Python 3 k-means I run this you'll
notice for this kind of the image
original shape is 298 pixels by 630 2 by
4 so that last parameter
plotline blows it loads its channels in
last if you're using another library
sometimes they load in channels first it
kind of depends on how you like how its
organized but just like keep in mind
what your data looks like every time you
use it so the images are originally
loaded in like that I kind of linearized
this image so that's what that second
print statement does is we have this
just a single just boom numpy array of
was at seven hundred and fifty three
thousand three hundred and forty four
just entities and then when we reshaped
it we get back to the original shape and
then I went and displayed that using
matplotlib and you can see that that is
our first image there's some axes here
and you can you can remove those if
you'd like but we don't we don't
particularly care now if I wanted to
kind of remap this a little bit so that
everything was a little bit easier maybe
four computers to deal with I could do
something like this
whoops our color mapping doesn't work
here because our image pixels are not
translated into a1 on scale but we can
do that shortly so essentially what's
going on here as I saw someone asked in
the in the chat
what does channel mean in context of an
image that's a great question so if we
have images that are in like black and
white or grayscale those are single
channel images so every pixel the
intensity of gray is represented as some
number from zero to one usually or zero
to 255 and that allows us to just scale
across one thing but if I had an
equivalent RGB image or color image it's
1/3 the size because you can think of an
image is really being a composite of and
there are different ways of organizing
this this is just a very classically
taught one where I have actually three
versions of the image kind of overlaid
and the first version is the red version
the second one is the amount of green
and the third one is the amount of blue
and then when I combine those on a
computer screen I can then see what the
actual colors originally were actual
colors being an approximation there are
other ways of doing it using like hue
and I think that one's for channel so
this is what we're using but it doesn't
really matter in our case we can kind of
shuffle everything over and then
readjust accordingly
kind of as needed so hopefully that
clarifies what reshaping things does
here I am just going to try and stick to
these two libraries I might also throw
in the tea qdm import TQ DM which I
don't actually know what that stands for
but it's a really like pretty way of
printing out as you iterate over
something what it looks like and it's
just kind of a good way of keeping track
of stuff so we know we're working with
images we now have a way to reshape
remake readjust images depending on what
we need to do but we don't necessarily
have a script that does anything
meaningful so I'm going to start my
scripts using kind of the if name main
paradigm oh thank you someone in the
chat says TQ DM means progress in Arabic
I don't want to butcher that word but I
would otherwise try and pronounce it
that's really cool didn't know that so
it displays pretty project progress bars
it's really useful uh-huh it just helps
me kind of mentally keep track of where
we are in the execution of code and then
you'll see that the paradigm that we're
using is kind of like if this is
intended to be a script let's use it as
a script so what we're gonna do is if
name is main then what I want to do is I
want to say my data is some method where
I call like load data and then I'll
probably have to give this some
directory so if I go into my terminal
and I list out which directories I have
let's have it load data from the stream
all directory so it assumes relative
pathing and that loads in my data I want
to pre-process my data so we might be
very clear about this and say pre
process of data and from there I now
have all of my pre-processed data so
well I might want to say is okay we have
our pre-processed data
let's build a k-means classifier so
we'll call that k will call that k-means
and that's going to be equivalent to
some k-means classifier which is
instantiated with some number of classes
so we'll say k equals I don't know we'll
have it be a binding
classifier to start but we'll change
that shortly already we're starting to
see it as a magic numbers there's some
functions that we really need all sorts
of interesting things that you need to
know so from there we have this k-means
classifier we're going to use kind of
the standard like SK learn methods and
we'll say have it fit on the processed
oops
data so once it has been fit to that
process data we should have some set of
images so we're gonna do this one
deviates a little bit from the standard
API but we're gonna say k-means display
means so this will display the mean
images to us and then yeah actually
we'll stop from we'll stop there so
let's work through this a one-line at
atomic yeah so load data stream also
where what is it loading the images from
the stream all direct yeah so I handed
the directory and it assumes that
there's some set of images in that
directory and snags all of them for us
okay as numpy arrays so this this kind
of way of coding things is something
that I really like doing it kind of lets
me template out my code I kind of know
how things will work and it gives me
kind of a set of objectives to then
complete so I say okay load data and
basically it's all documents or writes
it for me so this should take in some
like directory and it should return to
us some numpy array containing sort of
happily so syntax yeah so I am writing a
little bit in kind of functional style
language over here in our comments to
clarify these comments make them a
little bit easier to read we'll do that
about the code time extension ah yeah
and it's also an extension I'm a huge
fan of using extensions to kind of I
don't know make it feel a little bit
more homey and someone asked which ID is
that that is an incendiary question for
some people but this is Visual Studio
code I'll leave it to you guys to hash
out well how much of an IDE it is mine
functions very much like an IDE I would
count it from tons of purposes as an ID
it's definitely more
a lot of this like competitors like Adam
is sublime which I mean item s per se
yeah editor's so some
IDE features that the insta code gives
you yeah especially I'm a huge fan of
the way the terminal integrates where as
an atom you have to like drag the
terminal and I haven't quite figured out
when I install it as an extension to its
native this is really so I'm a
huge fan of Visual Studio code very well
built I have not any problems pretty
easy to use and so then we have this
pre-processing step and so anytime
you're working with images or audio
files or really any kind of data but
we're gonna say just kind of those two
in particular because they're easy to
contextualize we might want to like
manipulate that data somehow so in the
case of images a lot of times that means
extending our data set because it is
kind of difficult to obtain images that
are meaningful but as we've mentioned a
little bit before and we're gonna
mention probably a couple more times an
image of like Colton and I here and we
actually pobably not going to do this in
this stream but it would just take a
little more time an image of Colton and
I sitting here as opposed to an image of
us in Reverse we're still in the same
stream right so there are ways for me to
extend the amount of data that I got out
of something even though I never I never
like acquired new data and so that's
kind of an image augmentation process
audio files it's a little bit it's a
little bit more difficult for us to kind
of imagine what is a meaningful like
audio file mentation versus not
but images is pretty easy for us to
understand but like if I swap us around
if I make this black and white versus
color they're all still the same so and
there are different arguments as to
which ones are more the same than others
and so pre-processing is usually a
pretty important step on your data now
there is a huge need and kind of problem
in the data science world at the moment
where we have many different versions of
datasets and it can be really difficult
for us to keep track of each of those
versions know who created it how it was
created what it means and so there's
actually a white paper out there I
believe it's called mystique that talks
about the different versioning systems
oh sorry no it's the something DB Bolton
versioning system for pipelines or data
pipelines
if you google around enough you'll find
it but there are very many cool papers
out there on all sorts of ways to like
handle
the new data that you create Susan
original is this pre process like an
optimization for our machine learning
yeah so we'll actually the step that
we'll take is to Center our data and
normalize it so standard normal is
something that is it's a distribution
it's kind of what people mean when they
say normal distribution they actually
mean standard normal which is something
that is a Gaussian distribution about
the center of mean that's a zero right
yeah it's our bell curve looks like this
it's centered on zero so on out well not
on average but kind of like at the
halfway point you're at zero and it has
the standard deviation and variance of
one and that's the standard normal
distribution so in order to get any set
of data into a standard normal you can
subtract the mean and then divide by the
standard deviation and that puts you at
a standard normal so we will do that in
our pre process step we're also going to
reshape our data so given a data set
that is like an end by image parameters
set of data we're going to actually take
each of those and then pop them into an
actual like the single line versions of
those images which will make it a little
bit easier for us to do some of the math
on them I'm turning into a matrix
basically yeah we want like an end by
however long our image is our matrix
instead of like a n by two 98 by 6 95 by
4 and like this 5 dimensional major in
Japan so we're going to do that we will
pre process a little bit and kind of
keeping with our documentation we want
this to take in some and numpy will say
n by like height by width by channel
data and convert that will be more
specific numpy array and we'll convert
that into an N by height times width
times channel numpy array possibly
normalized so what this should do is
normalize our data possibly will
actually run this first without the
normalization and then we'll add it in
but it at the very minimum should
perform this transformation and that is
kind of how I write my documentation
this is easier for computers to perform
these matrix by matrix operations too
right so it's very easy P dimensional
may 12 matrices use a single
matrix exactly so then our k-means
classifier is going to is literally just
like it the classifier object itself so
this is where we're moving away from our
functional paradigm and we'll just do
that
yeah creating an object at K music our
object is what is this from again the
k-means classifier
we'll be building it oh so we're doing
this yeah yeah we're doing a full full
full thing there's no library to
postpone this P process not exist in
either cuz yeah so we we are basically
designed what we want everything to flow
like it's my my top-down approach to
building I like this for game
development too it works really well it
gives you a kind of direction you have
some goals you know what things need to
work and in concept this is complete
like the actual script part of it works
fine yeah so that's that's used it's
kind of like what you see in test-driven
development a lot to see people build
their actual application and XS these
individual functions as they're writing
them after they've been created it's
actually a very cool paradigm I'm a huge
fan I think it just makes my life easier
I'm gonna say of data and this goes to
so once we fit this doesn't return
anything so I'll say goes to none we
just want to fit the data and again this
is where we deviate oh you have anything
return done yeah channel where it is
pure sighs thanks so now our k-means
classifier has some state which is what
we kind of mean by our company over here
is like it just doesn't any print
statement is kind of like that too and
this is effectively a print statement so
essentially what we say is that display
means is actually going to do nothing it
doesn't take in anything and it does
nothing
there's there is some degree for a lot
of applications we still need some bit
of like state some bit of yeah including
outside world and error function is kind
of a pain I might an enclosure for input
and output you do not use still print
okay
need ya interfaces humans which is kind
of where we end up deviating a little
bit from functional even Oh camel you
have like print statements and stuff
like you just some parts but you have to
yeah and I guess the argument that you
don't have to is like you could write it
to file or something I don't know
they're like a couple of people who are
like no it has to be pure and I'm like
yeah I'm super new using like a mostly
kind of functional inspired pretty
imperative to me and yeah it's pretty
bad we read Eevee ated far from
functional pretty much after this point
functional core with oh the edges are
imperative yeah there's a functional bit
at the beginning so yeah we now have
kind of set up all of our stuff so now
let's go start building some of these
functions I'm gonna leave this template
code up here for a little bit but it'll
go away eventually it'll actually go
away very shortly so we have to define a
function load data that has to take in a
data directory and it's going to return
to us some numpy array and so this is
kind of my template code for that I'm
going to remind myself of how it works
by copying over my documentation comment
and what we're gonna do is for loading
in our data we're assuming something
about the data directory which is that
it has a bunch of files that are pngs in
it and in order to access that in a
really convenient way we're gonna just
import OS so that's me interface with
the operating system pretty conveniently
I'm going to comment out all of these
lines so that I can run this code excuse
me in order to test it one step yeah one
piece at a time so I just want to verify
the stubs and write and then get it to
like mostly work and I think that
actually is generally a little bit
better practice but we're gonna we're
gonna kind of go from there and so once
I want to load my data
I actually am going to just print out
what OS dot list derp of data directory
looks like so I know that I'm going to
list out the data somehow and I know
that in some variant of this I'm going
to need OS that list door and this is my
running of k-means pi sorry I'll stop
displaying that image forgot about that
it's
that's a very nice image as a student so
here are all the things in that first
data directory you'll notice that there
are approximate I mean maybe you
wouldn't notice this but I happen to so
about 40 images in there there's no way
you would notice that that's literally
impossible but unless maybe if you're
like isn't on there they're about 40
images in there they're exactly 40
minutes in there for sorry 40 images in
there and that's all I really need to
know now what I could do is kind of like
a list comprehension on that and say oh
actually I want like oh s dot path not
absolute path loops of f4f in that list
directory and now when I rerun this it
now tells me whoa let's go down there it
now tells me the absolute path of each
of those things so if you wanna be a
little bit more paranoid about like
which actual file are you using you can
do this live list comprehensions it is
people have you know described them as
being very functional in their design
essentially map filter yeah I know they
don't have the reduce capability and it
was not I mean you could and then apply
that right and there is the lambda
calculus like you can create lambda
functions
yeah you can't yeah I hate how limited
Linda's are in Python they're much less
completely it's it's really just like a
kind of essence that lambda is it's
basically like a like a J s like an es6
like return statement which is exact but
it's gonna be my own ignorance
I'm sure you could eventually kind of
like you can build your own kind of
library around it I'm sure people have
but we're gonna
okay well we'll kind of just generally
all agree that it oh I see I own one
says our all CS majors our Harvard this
well-rounded Brian would stick also this
Web Harvard doesn't bring a job of like
forcing you to do a bunch of different
things does it look for like
extracurriculars oh well it does a lot
because applications right oh yeah so my
heart is slightly careless I actually
was like mainly a musician at martial
artists in high school
and then I kind of switched here and
flipped over to all sorts of new things
and I think that'll
can't speak for the entire student body
obviously but I think from the people
that I know and there's enough of them
that it's like people are pretty diverse
they have pretty diverse interests like
I actually have a fairly major interest
in like government economic policy which
is very unrelated and I have a major
interest in music theory but all of
those things do not show up in my actual
major I'm also a pretty major student in
like Chinese although that deals a
little bit of my heritage and so for my
personal story that's where all over the
place and a lot of students I know like
Brian's linguistics and CS and he does
all sorts of like things with the
intersection of those two but he's also
I think pretty musically inclined very
nice guy does all sorts of things with
like the newspaper and stuff like that
so all sorts of interesting things out
there and I there are many people who
are just kind of like oh you know I'm
like a world-class ice skater and I also
study your math and I'm like this is
very diverse intellectual pursuits Dean
honorees yeah so that is roughly if I
speak Chinese because I recognized that
as being Chinese those two years at the
end between Japanese and Chinese right
Oh which I know a little bit Japanese
this one means to meet I believe in that
in Chinese as well but in this case it's
like they have the ability to or oh yeah
actually like it's ability to Chinese is
basically this asking that's roughly
what that reads in English a lot of
times if you were speaking it in English
and then translating the Chinese you
would add another verb between kind of
those two middle characters and that
would allow you to indicate from like an
English speakers perspective like do you
have the ability to speak Chinese or the
ability to understand and so on compared
to Japanese and Chinese English is very
bloated with all of our Bertoni words
and other also others our language
they're very sort of implicit they're
just kind of like booms they're speak
two things and then it's like you
understand by you know which is kind of
cool very interesting although they also
at least in Chinese there's a lot of
these at like kind of particles at the
ends of sentences and I never get them
right like it's just I'm always a mess
and they're all just like single
syllabic kind of like la no bond and so
on and it's very very difficult to for
me as a native English speaker
well should I put that there should I
not there's a lot of those particles
that start sentences and they're like
meant to imitate sounds I mean a lot of
it is probably difficult like pretty mm
like you learn it through exposure
constant right here I get that those
contacts right like the word even like
do you even do something right right
that's exactly like there's a lot of
words like that in English that we just
put into sentences right that are having
a native sounding they make a lot of
sense but to people learning English as
a second language it's not clear it's
just like whatever you I okay so I'm
positive that when I speak Chinese I
sound very much like an American I
haven't been have an accent as well
but when I was in China people were
still able to figure out what I was
saying and it was it was very nice to be
able to make friends with people who
didn't speak English that was pretty
cool
none of my co-workers spoken English at
least Facebook's um I think my P I
actually spoke the most but everyone
else was pretty pretty my own English
and it was fine they're all great people
I learned a lot about their lives slim
very cool so if you ever get the chance
to learn another language go for it it's
very cool I think the moral of the story
is that it's nice to be curious about
things yes anything that's you know if
you have general intellectual curiosity
will naturally lead you down many
branching paths people here have that
and not just exclusive to Harvard
obviously but just in general people
that are interested about the way the
world works and the way things work and
are you know want to explore that yeah
you'll just develop interests and
hobbies all over the place yeah exactly
you hop all over all sorts of different
things and if you really like something
you can get good at it roughly you can
try yeah so yeah go and go and be and
don't bully English as our robot listen
I would never do such a thing we would
never do that
English is an interesting language but
yeah it's very interesting all right so
we're gonna plot that in read of f4f in
image files the reasoning for me
splitting these two it's not major I
just wanted to make it a little bit
easier for people to read as you can
pretty clearly into it I could throw a
plot that em read you could make it a
mess your comprehension or yeah or
it's not not terrible but I just wanted
to make this a little bit I don't know I
guess pedagogically it makes a little
bit more sense for me it's already
pretty bulky as it is yeah there's kind
of a lot of information in there I think
this is fairly reasonable for people to
read yeah sorry so now roughly we think
that this function returns what we think
it does given its input we're gonna just
double check that oh that's rough users
Nick that's rough yeah there's all sorts
of areas here which is I think couldn't
find the path which doesn't make sense
yeah especially hearing that it's an
absolute path let's uh let's debug well
do some debugging Blackie buckin is kind
of our favorite thing to do here yeah no
it's fine it's not really really true oh
right yeah so absolute path is a little
bit of a liar we're gonna do something a
little bit funnier here and you guys may
recognize what I'm doing it's this
little I think work yeah
and basically what was going on was it
was emitting the actual data directory
so absolut pounds a little bit silly I
guess and how I see it works given the
absolute path to where it was called and
then it just depends files on there as
is so you could do like an OS that path
that absolute path of this it's time to
get real nutty that looks a little wild
so for readability sake I'm gonna remove
that we will sorry also we will put this
on a github
oh yeah that'll be good so you guys will
be able to access this later but I'm
actually gonna remove that just for
readability sake yeah so hopefully
there's all make sense i hain't no bulky
oh it's not path calls get and Python it
just like they span like there's seven
line take huge payment so yeah we're
gonna try to avoid having too many like
enormous line functions but you know
here yeah all right so our data loads
correctly we end up with a numpy array
that is this n by H by W diet by C
and we can verify that here Alex I
printed out that shape we have 40 images
so that's n we have our W which isn't
actually the dhobi it's actually our
height book or we don't really care
oh I actually did call it eight years so
my head before hand was correct the way
that I'm speaking right now is off so
that is the height of the image and then
the width is 632 pixels and then we have
four channels so four things below I
always forget the other names of them I
know he was one of them I don't know the
other one hue-saturation yeah I'm not a
huge expert in that I think that's
that's a macro in image macro of each
meta right it's not a per pixel thing
it's a great point so yeah there's a if
you just go google like a way to convert
from like hue to RGB it'll fill in the
rest of them for you so you can go
google it that way to process our data
so we're assuming that we have some data
I'm gonna copy my comment over and we're
not going to normalize at first but we
will eventually and I now wants to
define some pre process function hue
saturation luminance oh thank you there
we go that's a few saturation luminance
that's that Oh your color that's cool
those of you who are just kind of
listening casually I guess I knew that
we need to be sometimes pac-man said
that hue saturation and luminance are
the three things for converting from
like the hue version to a color version
and they end up in the same image and
then they said that they're a filmmaker
and colorist and we were like yeah that
makes 100 percent so there's there's
some really cool stuff like with you and
use the HSL or HSV l model for color red
modeling and in games like puff you can
actually like I used to be really into
procedural generation actually you know
this from the class yeah but you can say
if you wanted to dynamically text you're
like let's see of a sword and you want
to dynamically recolor the sword doing
that with RGB
looks horrendous if you're just writing
random algorithms because you get these
crazy nasty colors are using hue you can
just modify the heal a little bit while
keeping the saturation right and whatnot
and it actually looks much more coherent
it's a very good point yeah so these
different color models work well in
different it was a great point yeah that
is a very good point yeah and then the
true key nice points out that our fourth
channel in our version is also is a
passive II and so you'll notice that our
data is actually not RGB
data it's this huge saturation luminance
and opacity in data which is way less
catchy sorry
so HLS Oh slow hey yeah we're probably
I'm not gonna attempt to say that any
more on this yeah you know this little
weird so I'm gonna just leave that one
as as y'all can read the chat if you
want so we want to pre-process data and
we're basically just flattening the data
is this the first step that we're gonna
do so what we want to do is just kind of
well I guess flattened equals NP tree
shape and this is what we do the movie
now we're a function and so all of our
dad's up here we're essentially just
doing this right now I do have an extra
parameter in that I have like the
dimensionality of the data and so we're
gonna say n hwc is equivalent to data
shape and we assume that we're given a
numpy array
sorry no error handling no malicious
input or malevolent input device to take
ages to finish if it's like an action
this isn't an you re just not going to
be the way that everyone wants it to be
and then we do H times W times C and
this is now hopefully and we're gonna
just kind of as we build each of these
we're gonna just double check that we
get the things that we think we get and
this is going to be our new data so at
the moment I'm going to just really is
this so new data equals flattened and
this allows us to put like another
pre-processing step in the middle which
I happen to know we will be doing later
otherwise it's just slightly less
convenient so we go ahead and do that
and essentially what we get is this here
at the end which makes perfect sense to
us there are 40 images and then there's
just the things at the end right each
pixel so once we've done that we now
have our flattens data and this makes
perfect sense hopefully to everyone
watching we this is our pre-processing
steps or data has now been pretty
processed which basically turned her
into a bunch in number is it to convert
our house into a flat line of enough
fudge now we just have like 40 lines of
numbers that can informally math on to
to do magical things exactly and so
we're gonna delete these pre lines that
we had
here so now that we've pre-processed our
data we look down and we recognize that
the actual k-means classifiers what we
have to implement next
and so that's an actual class k-means
classifier got a little autocomplete it
knows a bit of object-oriented
programming in today's eleven and we're
gonna define an init method so for those
of you who aren't familiar oops an init
method is the thing that gets called
when a object or a class is initially
instantiated and so what we can do is we
can set its default class to ten as if
we were classifying mni as T data which
is pretty convenient Oh as is pointed
out by Cirque F path live is very
convenient on handling paths yeah that's
a great point I use OS because it's
built in iPad live might also be
built-in I'm just not familiar with it
but I will probably look at it right
after a stormy dragon says that
according to pet 572 yes Wow assignment
expressions can be used in lambdas will
be in Python 3 point have you seen that
you know like in a in Python now you
can't assign a variable you know I can
see it's very common or Java to assign
like in an if statement I'll be like if
K equals blah blah blah addition your
assignment check hey it's the value and
then because it you can't do in a Python
them right but they're going to add them
through point a and you can use a no
lambda expression it's almost like being
inspired by C++
go figure and I'm sure the Python
developers are like oh my god that's
fall like what are you talking about
yeah well Guido left and I'm pretty sure
a lot of these hang-ups we're telling
the reasons that he wanted to leaves
myself what occurs it could be good
incorrect we don't you're on the stream
website okay you think all right so we
have this in it's like we don't announce
in this terrible this is horrible you
guys suck so we're gonna define this
we're also going to just define a mean
images parameter which is going to be a
list eventually but it's gonna be none
at the moment I kind of in my init
statements I like to just include all
the like things that I want to be global
there are naming conventions for these I
will admit I'm not the best at following
them like things that are internal
should really be prefaced with this or
end with a underscore and so on but I'm
gonna just pretend everything's public
in our object
Center a Spanish question mark is very
very loose object or a orientation your
principles compared to language Java
exact thing is very protective in C++ is
actually even more stringent I believe
really kids out with its classes oh yes
it specifically rims for it's been a
long time since it was possible I just
remember it being tougher to do what you
want to do compared to Java which is
like miles tougher than Python would you
news do whatever you want yeah exactly
you just do this dynamic languages
versus static languages it's a good
point no circuits say these strongly
supported this idea
oh that's cool okay I'm glad to know
because I think it's I've actually
thought that's a cool idea we actually
ran into it being an issue with the C++
are with a cs50 codebase then assignment
Express right and we needed to find a
way to work around the daily so the fact
that they vary timing lis are adding it
to the language is kind of cool I'm
sorry we just had it yeah so I was just
out of adding some template codes and
things I can now run this entire file
and nothing will break okay nice an
empty class right now for the most does
nothing's but kind of works you know
like our data all of our methods kind of
function ish like they'll they won't
fail as that ignites points out
everything is public and Python there is
no private they just pretend to force it
using the underscore underscore paradigm
and you can kind of see that if you
render on something like if you just we
could print out durab something print
dur of k-means and what this will
eventually tell us is there's a bunch of
attributes you'll notice a lot of them
start with this double underscore
comment under usually dunder methods or
dunder exactly or for those coming from
like that Java C++ paradigm those are
your kind of private methods but they're
not like strictly enforced private like
I can still access them they're still
available it's part of the API it's just
not intended to be a lot of these are
meant to give
objects default like like default length
abilities default
yeah ster so if you wanted to be at an
object you always have a method to do
like a unified API for Python objects
which is like white dictionary says
lists and all sorts of the things can
use the same set of functions exactly
and actually just to kind of highlight
that will redefine the stur
for our k-means classifier and all this
has to do is return a string once that's
the sole requirement so we'll just say
this is a k-means classifier object k
equals and we'll just format that was so
you still use the dot format I do here
we'll switch over as s : has pointed out
I'm a little on getting old you guys
very good point I should say so what
this does is essentially the same as
when I originally typed but it's a lot
more readable I agree
where are you just kind of if you work
at all in like jQuery JavaScript you can
do the same thing with template literals
and so I just add an F to the beginning
of my string and that lets me use these
curly braces to indicate something where
I just write out Python code to your
credit I use the percent I use not I
don't use it now but I used it a lot
like even as three point six was still
out it was still like two point seven
mode right so I didn't really get to use
the dot format and when you see that
only the time when format strings came
out I was like this is nirvana it was
like this is they're so nice this is how
you write strings in Python it's so much
nicer and I can't wait for that to be
the like standard in all the tutorials
everywhere yeah but yeah there's all
sorts of like funny things with this so
instead of now printing out a derp of
k-means we're actually noseprint k-means
and you'll notice that essentially when
I run this it'll print out the object
that I told it to print so essentially
what's going on here is it just took
this and printed it instead of whatever
it would have printed by default which
would have been like some k-means
classifier object at whatever yeah this
is what it would have been which is kind
of a literal version but the address of
the object
yeah which is and then store we can
actually mentioned that methods donors
on both sides like ster are not hidden
like methods with prefix writer
prefixing will obscure the name by
adding the class name as a suffix that's
kind of cool that was a good point like
I got sort of sloppy privatization yeah
it works
I'm sure does but it's
but it's not as clean as a C++ enforced
class or a Java oh and actually map book
was mentioning the preservation of dick
order with three point six that was such
a nice right thank you
obviously their hash so they don't have
order defined order area like an order
Sunstone yeah Steve Python by default as
a three point six like makes that I
think so you don't need the ordered dict
class anymore that would be a thing that
used to need oh my god yeah instantiate
I don't know if I've ever used the order
dick class I feel like I always just
worked around it I I was always just
like yeah okay it's out yeah Jim I'm
going to use it when using a dictionary
you're like not always concerned about
the ordering but surprise it's important
they can matter and a lot of times it's
not worth reinventing as is going on in
chat people are clarifying the dunder
methods refer to these with double
underscores on either side we were kind
of just skating over the fact that
private methods have only a single
underscore set I was kind of implying
that but using an example that actually
has both so keep that in mind we won't
we won't mess around with private or
public methods at all here but it's it's
important for you to know yeah so we now
have defined our kind of in it roughly
we have this stir and now we just have
to define how fit is going to work now
fit is kind of an interesting one
because we have to define like the
number of iterations that this has to go
through to fit we also have to
initialize our fit so it may be worth
our while to have another method that is
like in it k-means or something like
that basically to avoid it
conflicting in our heads with the init
method I'm not quite going to put that
here but it is kind of the right idea
we're actually going to just put it
within the fit method just for
convenience so we do need to initialize
given our set of data what each of our
clusters is so what that might mean for
us is we have to say okay our clusters
are going to be some set of kind of
lists and each list has all of its
individual things you'll notice that
we're gonna actually kind of take a hit
to performance in terms of what we're
doing here there are ways to do this
that are much more performance even in
Python we're going to avoid those for
clarity just as a disclaimer
so the clusters is going to be some
of each of our clusters now I could
initialize that list and actually we're
going to I guess it actually doesn't
really matter and because of the way
we're gonna do this later we're not
going to initialize those yet but we are
going to shuffle our data so MP dot
random Dutch shuffle lets us shuffle our
data sequence so it's going to shuffle
our data such that things that were
originally in one order are no longer in
that original this is good because the
images currently are like grouped by
stream basically right exactly I'm
assuming that's why you're doing which
is why we're gonna do it this way okay
so we're gonna shuffle our data and make
sure that everything shuffles here's
some of the documentation if you'd like
it oh actually this is a documentation
for the random one and we're going to
see if there's any important things
this doesn't return anything by the way
so that's why I don't actually have to
like have it capture anything kind of
things very non-functional and it's kind
of annoying to me by then like there's
no real standard I'm like do things
return stuff or not or maybe there is
and they've like migrated towards that
standard but for a while you could like
there were some things that you shuffled
something that you didn't do things like
yeah there are stream there are sorry
those are streams there are functions
yeah Nick native functions that differ
like what's in the standard library like
we look like reverse and sort or an
example is there one doesn't in place
and one does it return yes it's weird
yes it's like why is consistent started
ask way more extreme earlier than usual
we're trying to gravitate a lot of our
streams earlier due to Nick's schedule
has tend to have to be a little bit
later in the day but mine are gonna be
probably around noon most of the time so
keep an eye out this is a new shuffle by
default who did say oh I'm not sure it's
probably something yeah you can go
google their documentation
see which stuff he might have a sense
here the pin is also possible I don't
think it's NP random got split it's oh
maybe there's just splittin
yeah okay cool so oh and I don't really
remember the syntax for split so we're
gonna we're gonna use my fancy IDE and
it's going to say split our area into n
sections cool so we're gonna split is
our now shuffled data into K parts so
self dot K part
for this and for using split you'll want
to make sure that like your data number
of data items is divisible by K
otherwise I believe split throws an
error so if I throw in like a random
prime number actually be like 40 data
items there's not that many things it's
divisible Y so if I threw in something
like 9 it would freak out
I know potential way to get around it ya
know no solution you just freak out and
crying okay there are ways to solve that
but we're gonna we're gonna kind of use
this because it's convenient so we're
just doing a randomization of our first
meet of our first factors yet so comment
this this is a random in it for the
first clusters and this just sets us up
so that we have a bunch of random images
now it might be really entertaining for
us to like print out these random images
before they've actually been fit so we
could we could kind of show that but now
we just iterate over so basically what
we do is each step we say okay here are
our clusters for each item in our data
see which cluster it is closest to and
assign that to a new cluster and the way
that you kind of check which cluster
it's closest to is you look at the mean
of that cluster and you say okay well
you know what what what mean are you
what is our distance by some method and
then we go from there and we assign it
to that question be a little bit of a
bulky line of code here yeah where to
line I kind of break it apart into a
little bit of some new pieces so we also
need to like calculate the cluster means
how are we doing are we gonna do a
recalculation of the mean as weary sort
the data or we're gonna like pre
calculate the mean do all the fitting
and then like rinse and repeat that
we're gonna do the second part where
we're going to calculate a mean and then
we're going to iterate through all the
data and be like all right you know what
things do you have it close to you like
remember you assign recalculate a mean
I'm gonna do that again and how many
iterations if I do it typically so
actually we don't do that many a lot of
machine learning algorithms you like
iterate constantly and you're kind of
like iterating forever and it takes a
while and now if you have a lot of data
this can still take a while but we're
actually only going to iterate me will
display the results of different
iteration numbers and kind of look at
what they look like but we'll probably
only iterate around ten times
we can kind of measure that difference
by measuring the loss across or some
random random but a choice of things as
far as like what breaks down or what is
costing us so we have our clusters now
we want to get those like cluster means
and we know that like mean images loops
mean images or those cluster means and
we want to assign that for to the mean
of everything sorry we want to assign
the means of each cluster to these
meanings so we do like cluster not mean
for cluster in clusters yep very close
of like cluster axes oops access equals
zero for cluster again close so what's
this axis equals zero right so the zero
with access is the first dimension and
the first dimension is like the each
image is like one of those pieces of
that first dimension and so I'm just
saying I want the mean across images not
the overall global mean as if I just
sampled across the whole thing and took
a mean I actually just want the mean per
image so what this ends up resulting in
is if I have a group of images then it
actually creates a mean like what that
mean image might look like which is very
useful to us and so actually what we're
gonna do is display the images right
after this step so essentially what
happens is now we have in self dot mean
images we have an mean for each of our
clusters for each of the K clusters so
we're going to do is self dot display
means and then we'll stop execution
there we haven't finished editing or
done any fitting whatsoever
but we'll do the plot top figure and
then we'll do for K it well we'll say K
and image in to do so if that mean
images plots dot M show of two to do
image will reshape it so it looks a
little bit better reshaped for the D to
make our variables a little bit cleaner
is NP dot reshape of
the image itself and you'll notice we
actually don't have the dimensions
established here which would be very
convenient for us so we're going to kind
of establish the height oops
width oh my god I can't type I swear
that's the stream under channel it
really is every time right with channel
just telling Python that we want to use
the global versions of those you know
like what global versions
great point we'll go create them in a
sec and then we'll just plot that image
show and plot touch show we'll give it a
title so we know which one we're on mean
so this can show a like a slideshow but
like a list of images that you click an
arrow to go back and forth I'm not too
familiar with Matt puzzle exactly it
actually well I'll use control-c n just
exist so how do they using format
strings then yeah we're I'll switch
everything over to format strings just
for everyone's convenience and that will
be kind of the way that we display all
of our mean images so for those of you
who aren't familiar i take oh sorry this
should be enumerate it's like that
doesn't work so anyway it just takes in
an iterable and then it gives me an
index as the first thing out of that
interval it's really convenient you get
a little next my default when you just
use no you would just get the image
itself and it's it's really convenient
for my title to have that iterable
there's no real reason otherwise for
those of you who scream lack of
performance yeah well that's true okay
and basically what we do is we have our
self up mean images which is a list of
each of those images and what we're
gonna do from there is we're going to
grab the image and its index which is
whichever cluster it represents I guess
it's for like human readability will be
plus one and then we have our reshaped
version of the image which is what we
did kind of way earlier where what we
said was we're going to reshape the
image which is just kind of a random
like store not real it's not random I be
good I was right
I don't know that we're going to show
you a whole lot but it looks pretty it's
just a list of the images pixels and
items kind of ran I got to stop saying
random stuck out there and we're just
we're gonna reshape that into what a
conventional image might look like and
then stick that into a Michelle this is
putting it basically that single
dimension thing back to an actual like
image pack eggs like that format that we
need to render an image and someone Eric
was asking question from our Python
white k-means is that of DB scan right
so I think that k-means is just very
easy to explain and also it helps us
motivate our requirement or our desire
to generate images so in my head when
our actual original intent was to build
a generative classifier to show you guys
and I was like well that might be a
little bit too big of a stuff to start
so why don't we start with k-means which
is really easy to kind of Intuit and
explain and then from there we'll
motivate our generative classifier by
saying well k-means can't really
generate new images it's not super it's
not built for that so then we might go
well but what could and then we'll say
generative classifiers and that leads
into the next stream pretty well so
there was a bit of a pedagogical purpose
to that there is I'm not I don't know
much about writing but this is I can
understand everything you talked about
so far sources this is great and I would
classify myself as a machine learning
beginner so hopefully this is hoping the
other folks that aren't necessarily that
familiar with it as well
exactly and I think that that is a great
point hi sorry I'm also my typing and
talking at the same time have single
thread a little bit yeah we're on the
same where I can type it I can't really
do much of any two things I understand
more honestly all right
so we've now reef unction to this so
that we now have a global height width
and see our channel if some of you may
note that it would be actually more
convenient for me to just initialize it
here in this like self in kind of the
init part of it and then use that I'm
using global variables because I'm lazy
I would actually recommend having this
be like a Auto config version which it
is now we're like if you hand it some
uniform size set of images this will
stood sizing accordingly but global
variables are generally not a great
practice now there is a reason for that
which is that if someone then imports
this is a module they then have a bunch
of random cool variables that are
floating around and they're not sure why
or they might accidentally mess up
naming and screwed things up so there's
like things and reasons why you might
not want to do that generally I would
recommend having like a self dot like
image shape or something and this is
just a tuple with like height width and
channel you could vote that excuse me um
I can convey what you said right and
that would be a better practice or
generated some of the input size exactly
and actually since that is kind of a
substantially better practice we we're
gonna do this equals zero or actually
we'll set it for the mm ist data set so
we've now given our k-means classifier a
couple of extra parameters I'm going to
nin not use this and our pre process
step is actually also going to return to
us the parameters sizes of the data and
so this will return to us an HWC version
of the data so it returns the shape
shake memos my favorite type yes imagine
numbers well I mean so this has a mat
like a meeting to its magic but this is
also just by default so you can override
those defaults at will but yes they are
magic numbers okay to be clear I think
they're a reasonable default because
it's a common training step but that's
it this is a new high Nick following
good coding practices equals H width
equals W channel equals channel and some
of you may be asking why would you not
just do that
standard Li and that's a great question
we'll and just takes energy yeah it's
well I could I got the tuple as a return
I could have returned the tuple and
passed it in but we're not
alright so there's that let me just make
sure that I'm not running any code that
I don't really feel like running we're
not gonna fit our data yet I just want
to make sure that didn't break
everything I'm gonna values to unpack of
course does not do do oh well okay that
makes perfect sense and since you guys
are very quick and spotting errors I'm
positive someone has already said this
in the chat but that's okay we're gonna
there you go cool so now I have those
parameters they get passed back to me
very functional style from pre-process
and that's even not probably the best
place to pass it back you would actually
probably make more sense to put it in
load data it's a great point so that's
okay well we'll figure it out but I
might rearrange that before I throw this
on the github but for times sake this
works and we'll move on so we now have
an image shape which is a little bit
cleaner way of designing this that way
we don't have to have this global height
width and channel and then we just want
to replace that with self dot image
shape which is beautiful and now we
should be able to display our means even
after the initial kind of randomized
distribution so we're going to uncomment
each of these now now the display means
on its own is actually going to take
care of this for us so we're not gonna
have that there and we'll do this and it
hangs which is perfect because that
means we have images here oh man Wow we
look lovely so here's cluster one you'll
notice that you're kind of F yeah I'm
early theory here yeah we're kind of in
like a dreamlike state you see LF python
is like hard core just stick it out yeah
there's like a produce over here oh and
we're freaking out every time I post the
image I'm right with that's the image
anywhere and if we go to cluster two
it's not a whole lot better I think I
have about four thousand different pairs
of eyes in your hair is I can see you
can see my oh yeah it's so like you see
my face is kind of looking in
it ghosts yeah exactly so you're kind of
like hey and it's almost like if you
ever see those images or sorry movies
where someone like moves really quickly
and yeah
focus or they someone's tripping a heart
on some LSD background so these are all
randomized initializers drug stimuli
yeah if you're seeing this you might
want to go see that guy like Julie so
those are our two images are our two
clusters sorry now you'll notice that we
actually have by default four clusters
so I can change my initialization to
four and when we go back over here we
get some kind of interesting results it
is still scrambled but there's a little
bit sharper definition to each of us
yeah
you'll notice they have glasses still
exactly now this one's all over the
place so there's that's adamantine by
target site in the chat says it looks
normal to me doesn't everyone sees it
and I hate to break it to you you may
wish to go see a doctor and then we can
kind of scroll through here my smile is
very defined and then your and whatever
that monstrosity is on the right I
always love doing it this is very
entertaining this is just this is just
from one mean Rio right this isn't this
yeah this is just random they just threw
a bunch of random images in there and
calculated the mean image and the Stewie
I like keep doing this over and over
again to actually good yeah yeah we have
to do the bidding step oh yeah
which will make this a little bit less
horrifying oh we haven't actually had
this really any calculated the mean but
we haven't actually like really lab and
they've done the redesign doesn't mean
so for I in range people are gonna shout
at me to use X range sorry and we're
gonna does how I using Python 3 we are
so it's actually X ray in the skin
working range is good we're chillin
I always forget which Python amusing to
do is X range yeah yes yeah so we now
have this like iterations that's cool
like a read don't hard-code waiting
extension is that is the comment
coloring there's a great question I
think it's prettier comments let me see
Oh better comments
nice I like that I might investigate
yeah there's a lot of really cool
extensions for VSC and I just hadn't had
the time to really like sit down and do
stuff I have another time this weekend
either but I did anyway and it's it's
really nice I'm a huge fan I mean you
can make your own custom like colors and
comments and things so I'm sure I will
do that and I will showcase them here
probably a lot but for now I just have a
couple I have a handful and I really
like using them so yeah there's that one
I think I also have a one in red that's
like to do oh sorry this is orange
question mark yeah and there's all these
ring like color combinations for
comments they support a ton of languages
we're advertising all sorts of things
here as an FYI X okay so we have 4i and
range - I told myself don't hard-code it
but you know well we'll figure it out
later
and essentially what we want to do here
is we want that kind of iterative steps
so now what we're gonna do is iterate
over the data and find which cluster
each image is closest to then assign to
that cluster and each cluster will say
new clusters is actually just going to
be a list an empty list for I actually
doesn't matter what this is called
right oh and you guys I think the
standard pet doesn't use those anymore
all right so essentially we create some
new clusters we initialize this just to
be empty lists and this is where we're
really non performant but it
demonstrates very clearly in the
algorithm that we're doing and we only
40 images it's not too bad if you wanted
to do this a lot more performant lis you
would you would recognize that like you
can actually just kind of label which
cluster they should have been and then
use operations to calculate the new
means without moving everything I sorry
I haven't watched the chat I've not seen
body parts site says Doug ignite it's
too late for that you've already
insulted him that's me
okay it's not like now don't worry about
ignite United soul to me I appreciate it
but you're all good everyone's good I do
like to customize stuff I'm positive you
have that kind of talent
I just I really like to like feel at
home with the things I build so that's
why yeah I've never I don't think we've
ever encountered a bad no this is not a
challenge we've never encountered like a
bad nature to comment in these streams I
think everyone's pretty pretty friendly
and kind so we appreciate that all right
so we I'll comment this initialize new
clusters so now we're gonna iterate over
the data and every time we it's not a
challenge so every time we find a given
image we're gonna calculate using some
distance parameter or it's re distance
function the difference between it and
the cluster that we're currently looking
oh yeah okay well it was you know what
you're getting in - I really should have
kept my mouth shut
can you do that I can use to a list
literal instead of saying the list
delisting structure there by the way or
is that close a bug oh you can yes you
can very reasonably do this I just
prefer the list constructor in the sense
that instead of doing this I prefer dict
or set do you really yeah well because
if you do and initialize it like this I
don't necessarily know if you mean an
empty dictor an empty set oh well for
that that makes sense yeah and then I
just because of that I ended up getting
into lists well actually is an empty set
there's an empty set a literal behave as
an empty big literal I believe so now
really I don't quote me on that I know I
said that because I think Merlin says
there is no empty settler constructor
that was a good point and then you would
just have a set constructor that works
like for blah well in that case you it
wouldn't work you'd have to say set
something equals that no yeah good point
so I actually I guess I just use the
constructor because it clarifies to me
on quick reading um ever maybe it's just
a bad habit that I've gotten into good
point
different strokes different thoughts
yeah good and you got a good point okay
but yeah you could flip out or swap out
that one and it would work just fine all
right so then we say for image in data
everything that I want to do is I want
to check how close it is so I want to
get a list of the distances so I'm gonna
call this distances and I want a list of
the distances
from this image to each of the clusters
and so we're gonna do that in cluster
order and we're going to say self dot
distance of image and cluster will say
cluster mean for oh man this might not
work super cleanly as a lose confidence
I like to use the whatever possible but
I know bloated they're they're a little
out there well I also you're zoomed in
to which to make it read right so
actually this is kind of hard to I guess
is a little bit of a mess but that's
okay
and this does what we want it to do so
sorry hopefully it's easy to read for
everybody my text wrapper wrapped it
around hopefully it's not too bad too
big of a pain if it is you can download
the code after and it'll be a little bit
clearer to read where's the distance
function yeah gratefully right next
question
you haven't defined it so we do need a
distance function we have our fit
function we have all these other
functions we need to find part of me is
worried that I'm not flowing along with
what you're doing and then a later that
you're right interesting functions we're
writing things that we just imagined
exist and they do certain things that we
think exists but they don't actually do
anything are they so this is gotta
calculate the geometric the the distance
formula yeah where our image our mean of
our image versus
whatever current image were sampling
versus the mean image in that cluster
yep exactly
and so this is going to take in to numpy
arrays i always forget this 102 it's
like x2 minus x1
yeah like the square root of all
actually we just gonna call it image 1
image 2 so that's the general version of
this and all we have to do is return a
single number now you'll notice that I
kind of like went away from my like way
of documenting these down below sorry if
I have time I'll clean it up
welcome to code so just laziness
alright so we'll break this function
apart even though you could condense it
into one kind of ugly line it's actually
not terrible so what we're gonna say is
like Delta's is equivalent to really
just image 1 minus image 2
it's not too bad then squares equals
deltas squared and then
somes okay maybe I should have just put
this on one I don't know I think this is
useful and then the distance is
equivalent to envy that square root of
sums and so what we then do is just
return distance and yes you can combine
many of these fairly reasonably into one
but we're not going to in this case so
first I look at the difference between
each of the coordinates within the image
so if I'm at like pixel number 79 I
subtracted from the corresponding pixel
number 79 and then I call that Delta 4
difference and then from there I take
all of those deltas and I square them so
now they're all positive distances or
distances this is just kind of like the
standard Euclidean way of measuring the
difference between two points importing
in existence right it's not meaningful
in our context so we're not going to use
them so we square everything and then I
calculate the sum of each of those
squares so I'm just adding up each of
those kind of like individual point wise
distances and then the distance is the
square root to kind of compensate for
the fact that I have this squared term
here now you'll notice mathematically if
it's not exactly what it does but it
kind of roughly puts us back down he's
like a normal difference and so now
distance will tell us what the distance
defined by us between two images is now
there are other metrics for distance so
maybe let's say I wanted to do like the
minimum distance between two things then
I might actually just take the like the
minimum of Delta's for example where
each pixel has some distance and then I
take the minimum distance between any
two pixels except what I would actually
have to do is calculate this Delta's for
all pairs of pixels so like pixel zero
has some set of distances from all the
other pixels and then pixel one has a
some set of pixel differences from all
the other pixels so it's a little bit
more computationally intense but either
way you're performing some computation
and what this lets us do the reason we
abstract out this distance function is
of course to allow us to swap out
distances pretty conveniently and there
are many many versions of distance
functions even having
recommended Manhattan distance we have
no idea what that mean all super
familiar with it but yeah there are many
so we now have a way of getting the
distance and if you look down here in
our kind of iterative step we're
iterating over each image within our
data let's just verify we have to have
yeah cool
and what we're doing from there is we're
saying okay how far are you from the
cluster mean for each cluster and then
the one that we want to assign it to is
the least element is yeah the minimum
one so we're gonna do NP arg min of
distances and this tells me which index
has the smallest distance cool so
Manhattan is actually just the l1
distance and Euclidean is l2 you can go
all the way through l infinite what is
El not sure L came from it's really just
kind of like a kind of standard I'm like
the Wikipedia page it's like l1 space l2
space and so on l infinite space
I believe the l1 space is kind of like a
unit square l2 is like the circle and
then L infinite I don't oh sorry maybe L
infinite is the square l1 is this like
diamond shape and then l2 is our circle
for you yes it's something I mean it's
kind of worth looking through but it's
not super not super important for us
we're just gonna use l2 slash Euclidean
distance sounds alright so we now know
which index we're going to assign this
this image to we're gonna just say new
clusters at index dot append image and
people are like wait but you have
horrifyingly like created new things
every time and it's ugly ingress and
you're right sorry
we yeah we now create new pieces every
time and it's not super for performance
and things but I forget why are we
reassigning new clusters to the main
clusters in the class so then after all
of these images have been reassigned
we're then going to recalculate our mean
and so now what we want to do is just
kind of copy this
[Music]
madam I think I did check out that
website at one point it looked like it
had a lot of notes on it I I've been not
spending a ton of time on math I do want
to get my math up I need to get to
calculus which means I'm reviewing
prekow at the moment so Crick I'm not
exactly as up to the skill level of most
Harvard grads or Harvard undergrads but
it looked okay I haven't spent a lot of
time on the website but thank you for
the suggestion oh sweet yeah and more
math you know it tends to get a little
bit easier to do a lot of things
actually so it was pretty pretty
important hated math growing up but I
get more appreciation for it the more I
see it in the context of CS yeah exactly
here about this is a good point so
essentially this is it
we've now fitted our data so what happen
is here twice or she asked Inferno slice
it'll fit over two iterations and
actually we'll just do it once just to
like see what it looks like after only
one iteration okay and actually let's
make that a parameter for fit is ITER's
equals 1 by default and the most
headless bee-eaters and that way I can
control it using the command line or not
command line but other things and
essentially what this does now just to
recap for everybody we did say that we
were going to repeat what we're doing
over and over again so you will hear it
many times and have heard it several
times so we initialize some set of new
clusters every time we want to go over
some iteration it's just a bunch of
empty lists and then what we do is we
say okay now we iterate over every image
in our data set we calculate the
distance between that image and every
other clutter all clusters using our
self defined distance function from
there we know which index we should
reassign it to or which cluster we
should assign it to by taking the
argument of those distances so just
which argument provides the minimum of
distance so if I ate over distances and
the minimum number sits at index number
three argument will return three it does
not return the minimum distance and then
our new clusters index is going to at
that index we're going to just append
that individual image and then from
there after we've kind of iterated over
all of the data we're going to say okay
now recalculate the mean given our new
clusters
and then we're going to iterate and just
do that over and over again this is
gonna garbage select the prior mean
images it will garbage collected I could
delete it in certain ways and build this
a little bit better but we're being kind
of clumsy for the sake of education
luigia actually made an appearance in
today's family jcu which is a long time
cs50 follower on the Facebook and we've
met in person in the UK it was pretty
cool that's wild oh that's crazy well
thank you for joining us and so we now
have our fit function and we have the
ability to display our means after we've
done it so now we've gotta put
everything together and so we're saying
that K equals four we're gonna try and
run fit once so it only run through one
iteration oh and I did want to pretty
display this as people might have
pointed urn no one really pointed this
out but we're gonna do it anyway because
it makes things really user friendly TQ
DM yeah so adding those two very simple
tags to the developers of take you DMS
benefit it allows us to have this like
pretty iteration so you'll notice after
we go through all 40 of them our little
progress bar filled up and if we had
many of these steps this would make
things a little bit more convenient it's
pretty cool wow it's starting to look
and it's looking a little bit cleaner
hearing right and this scepter only one
step we haven't really done anything but
the image for cluster one is actually
fairly reasonable we look a little bit
more like people this one still has my
face kind of ghosting in the background
but it does look a little bit more solid
we seem to have some kind of steps taken
to make us all crazy tan and then like
quick fall off they're much paler colton
actually bathed under a torn piece of
aluminum foil yeah sunbathe sorry and in
this one I look like I might be a
multi-dimensional being but we are
mostly in the right realm and then
colton also looks like a
multi-dimensional being who's again
basically in the lake isn't that of
existence like mostly in the right
universe and then here we have wow
Colton's like a pensive look is very
clear if that one whereas mine is just
like I am all over the place but we are
seeing some clarity so now let's run it
I don't know ten times you're sort of
okay
we're gonna run that around ten times
and see what happens
and we're gonna just fit with ITER's is
equivalent to ten I'm very curious and
well we'll see what ends up happening
here it's probably gonna take a while
that's a she doesn't know so not too
fast sorry not too slow now this looks
pretty clear of course there is some
kind of natural variance as Colton runs
around and as you can tell I move around
a lot when I talk and Colton moves a
little less when he talks because his
image is a little bit more centered on
him and I'm all over the place and we
didn't normalize these for like making
sure their faces were in the same place
or anything we just couldn't want to get
a mention on photograph yeah yeah it
really does uh-huh and then if we kind
of move here this is don't think I'm
just like looking left and right like
which makes perfect sense because some
of those images have you looking at me
some of them at the screen it makes
actually a lot of sense and you can kind
of see a faint outline of my arm from
one image oh yeah yeah and my fate my
hands come up all the time and things
like that and then from here this again
looks like it's about one stream which
is not terrible okay and from here we
also have again about one strength so
what if we did it a hundred because
we're in pretty fast right so we'll kind
of let it skim through them you will and
actually to kind of quantify what's
going on here we're gonna calculate kind
of the overall average distance of each
image from its respective cluster and so
we basically want to see at each
iteration what is the distance of that
image from its like current cluster
actually there's a number of ways of
calculating this we're gonna do kind of
a hoc ad-hoc method for doing this so
for each image we're also gonna
calculate its distance from whichever
cluster it is closest to so we're gonna
do this argument distances and then
we're gonna say the min distance is
equivalent to just NP that min of
distances and we're gonna use this as a
measure of like the distance it will be
from the next cluster it gets assigns to
and what we're going to do do do is
we're actually going to just take that
and compute an average minimum distance
across so some men
distances equals zero and so on each
iteration will have a version of this
and then we're going to just sum min
distances plus equals and then after
that iteration we will sum min distances
divide equals I was I'm going to divide
that by length of data and oh actually
what we can just do is calculate I'm
gonna do this
sorry we're messing around a little bit
and this gives us a way to kind of like
numerically quantify how bad we're doing
so higher this number is the worse off
we're not normalizing against anything
we're not doing anything fancy it's just
like a simple like average minimum
distance across all the images we have
and I'm not going to print this but I am
going to store it in kind of a
self-taught average distances equals
some list you know sorry no default
constructed a and we're just going to
self dot bridge distances append that
there's my corresponding parenthesis and
so essentially after each iteration we
will add the average distances of the
images to this array and then we can
display that in kind of a similar way we
did here define display averages we got
some weird notation going on we're gonna
essentially follow this format where
we're gonna just do a plot not figure
and then we will do a plot dot scatter
no we don't we just go what the plot
there we go the clutter classic average
distances and then plot that show and
we'll do plot that title average
distances
over iterations and plot that X Lopes X
label iteration number and average
distance and this allows us to then a
show after everything has gone through
what it actually looks like and so what
we'll do here is we'll copy that
notation and up display averages
displays the average distances over it
variations and k-means dot display
averages cool okay so hopefully
everybody follow there's no way and
that's not necessarily us like bragging
or anything we just that was kind of a
messy walk through a quick build of a
function but essentially what Colton was
asking me was as can we run this a
hundred times and let's see how it kind
of goes and yes we can't run a hundred
times but what we'll end up seeing
hopefully knock on wood is that the kind
of loss or error that we have and this
isn't necessarily a great measure of
loss but it works in this case we'll go
down exponentially like this and so
after you're like I think around ten
iterations might even be fewer because
we're data so small we actually won't
see a whole lot of improvement from
there on and so now if we run this oh I
didn't run it with 100 sorry let me let
me run this with a hundred iterations
and we'll watch that ping through for a
sec and now as that's painting through
I'm actually gonna add a try I'm gonna
try and do a little bit of a we'll do a
little bit of everything
[Music]
and this way we have a little bit
cleaner exiting if I decide to cut it
off in the middle now we ran over a
hundred iterations and we will see that
things got pretty clean is this not the
same no but yeah there's not a huge
difference right and so everything's
pretty pretty much as clean as it can
get in that these images the average is
that within a stream I took a bunch of a
series of photos and those kind of
screenshots are photos in each of those
frames we're actually not doing the same
exact thing you know like I've kind of
moving around I have my hands raised or
whatever and because of that you'll see
that our kind of average distance at the
very beginning after zero iterations is
enormous we have this kind of it's high
but once we drop down to like around the
first iteration we hit pretty low and
then we achieve our lowest after only
five iterations give or take so we
really don't need too many iterations in
order to get ID the images that you saw
there after that it's actually probably
just assigning the same images over and
over again and so you'll notice if we do
like ten or so now that we have kind of
a quantifiable metric for this once it
goes through we actually have exactly
that oh sorry so after - after exactly
two iterations we achieve our minimum
distance and it's basically telling us
that it has classified everything as
correctly as it can possibly get and so
this extra bit where we're at like a
hundred instead of a zero distance is
really just kind of due to the variance
within each cluster
now speaking of variance and a little
bit of Statistics
let's do a normalizing step to kind of
clear things up a little bit so in our
I'm going to keep collapsing these so I
can read them in our pre-processing step
we flattened things and I mentioned that
maybe in the middle here might be a good
time to normalize our data and what that
is going to mean for us and it does help
k-means a little bit although in our
case they tell us that's pretty pretty
simple because we're gonna subtract out
the mean and then divide by the standard
deviation and that will give us a
standard normal distribution roughly
well what did it really do is it'll set
our mean to zero and it'll set our
standard deviation to one and so we're
gonna do that we're going to get the
image mean which is actually just NP
mean of flattened axis is again equal to
0 and then we're going to get the image
STD which is NP dot I believe STD yeah
flattened axis again equals 0 and we can
say normalized is actually equal to
flattens - image mean divided by image
standard deviation and now instead of
swapping new data for flattened we're
actually going to just call that the
normalized version and so essentially
what we've done here is we've converted
everything into the normalized versions
of what they are we're gonna see how
that kind of changes what our means look
like so we go through here oh yeah okay
well sorry we do have invalid scalars um
to do so in this value we did have some
kind of like invalid conversions as far
as numbers go because of the way that we
normalized that so let's go ahead and
double check that we're doing things
correctly these standard operations
should work across matrices here I have
a feeling it's because everything has
not been normalized by like pixel value
that wouldn't make sense
oh some of the standard deviations of
pixels are zero yeah so hmm what we
could do is replace every standee like
in the standard deviation there are some
at like numbers that are zero you can
fix that by just swapping them out with
like one or not dividing by them there's
essentially we divided by non numbers or
we created in on numbers so this is like
I guess there's actually bang
semi-normal last this is the right mean
it has the wrong standard deviation
which is not going to help me prove my
point a whole lot but that's okay so we
run through ten of these oh right and it
has to be the right range of numbers so
when we divided everything out in order
to get it to be back in the right valid
range for M so which is what this error
is saying it's going to flip it to the
correct parameter set basically from
zero to one
if we have floats or a zero to 255 if we
have integers in our pixels and you can
actually see I think if you do normalize
dot d type possibly and print that out
and that's a key well that's okay
it's floats but if you were to then
print out like a small sampling of
normalized I look at kind of the ranges
of those values you could end up with
these weird like negative values and so
on which is not quite what we want
so essentially what you'd end up wanting
to do is actually kind of either
unnormalized them or before you go to
like try and imageshow them you should
actually convert it back to the proper
range which is not particularly
difficult but we may actually be out of
time to do that so I'll leave the
normalization code in there to be
adjusted later and then we'll spend the
rest of our time actually talking about
what this would have done instead of
continuing to do it just because it is
important but we're gonna we're gonna
leave it alone for a second because I
think it's not super important to what
we're actually doing so well we're going
to do there and what we really will end
up doing eventually is normalizing the
data it makes it a little bit easier for
k-means to not be as I guess responsive
to random variance within your data and
so as Colton pointed out we do kind of
look like we're from like a new century
or so
every time we display these and the
reasoning for that is essentially that
within our data set for each cluster
I've done all sorts of messy things now
within our data for each cluster we have
essentially ended up creating these
kinds of like allowing for the variance
where I mean it's a mean image right so
it's kind of just a representative image
for each stream and you'll notice that
when we did create it I've made a small
mistake somewhere it actually ends up
working out okay as far as like what we
want it to be which is to have each
cluster representative of one stream I
do seem to have a issue now in my
functions but it shouldn't be a problem
here so we're gonna just verify that we
have working code before we leave you
yes cuz that would be rough I don't see
why that's a problem oh okay okay I
guess those work problems
that's strange whatever okay so
essentially what you can see here is
like things that didn't change at all
wow I can really not touch that so it's
just kind of the background of the image
are going to be pretty consistent there
was almost no variance in those pixels
and so when I said oh we want to
normalize this by subtracting the mean
and subtract it and dividing by the
variance well a lot of those pixels for
example have zero variance they don't
change at all and in fact in all of our
streams this like upper corner is
probably always white it's literally
always the same pixel and I think that's
pretty much always true which means that
the variance is zero and if I tried to
divide by zero I end up with not a
number when I try to print that not a
number it freaks out so we can't quite
do that there's a little bit of an extra
step that we would need to take there
Batman saying use a try yes so as nm
points out variance cannot be zero
that's a great point has to be oh
there's positive my intent
basically in saying that is that
actually yeah they shouldn't have any
variance in this case so actually I do
believe variance can be zero I'll
double-check that so that I don't say
anything crazy
mu0 question mark yeah so actually it is
zero in this case and the standard
deviation is also zero
a variance or standard deviation of like
one would be like what you see in like
standard normal but in this case it is
actually zero and that's just because
the pixels don't change at all variance
cannot be negative as an FYI from there
what we would have normally seen is kind
of a kind of ugly looking version of
these images they would have already
they're already kind of there but it
would have given our k-means classifier
a little bit easier time doing this
classification which we could have then
shown with this next figure which shows
the kind of average distance over
iterations I don't think we could get a
whole lot faster than just in two
iterations were there but that's okay
it's some really talented programming
right yeah you know it's just uh as a
result of the careful selection of data
set so if we were to extend our data set
to something like the MN is T data set
then you end up seeing this actually
perform a little bit more real-world
like and even then they've done a lot of
normalization for you also your data is
all binary and it's in the right format
and everything so there's a lot of stuff
where like in the real world you would
have to do a lot of pre-processing
yourself generally but some of our
pre-processing steps which we shouldn't
take are to like normalize our data we
should extend our data so like flipping
us to should actually still be part of
the same stream like if I swap the
literal image around that's still part
of the same stream if I include
black-and-white images that are
represented under a color scheme but
they like visually are constructed to be
black and white those are also part of
the same stream and so on and we as
human beings can deduce that so I want
the machine to be able to do that and
that's just kind of like an image
augmentation step or data set
augmentation so there are many other
like steps and things that we would take
there but the main part that we wanted
to get to by the end of this stream was
well but what if I gave you a data set I
had to train your model on it and I want
you to build a new image that would have
fit within that data set and that is
what we're going to kind of leave
as today actually is so we've built this
classifier it lets us divide everything
into these beautiful clusters and they
are all representative of some class
that we have defined house meaning but I
also wants to be able to wasn't gonna
say generate new images and if I wanted
to do that then we might use something
like a broker sorry a recurrent neural
net or in this case it's actually a CNN
and so what we do here and I actually
pretty ran this because it takes a lot
longer to run and this K in this case
this actually is from the MN ist data
set and this doesn't look particularly
beautiful it's actually pretty hard to
read there we go yeah it really does if
we go down here this is the sample that
was generated using only fifty training
epochs and kind of on my own CPU and
very simple I mean this is kind of where
we're headed next week except it'll be
with Colton and I instead of the MN ist
data set is that those are hand drawn
digits right yeah sorry if the hand
drawn zero through zero through nine
digits and they've been normalized in
many ways and kind of formatted prettily
they're also very small it's pointing it
428 pixels
ours are like six hundred by two hundred
thousand to be massive although we were
actually still able to get through very
many of them very quickly
yeah because numpy is pretty powerful he
writes for a sci-fi game that because
you really do kind of look like them
you'll notice that some of them do have
clarity it's like this for there's I
think a seven here it's clearly a two
they're like a five here maybe like a
zero and a one and essentially what this
did was the program is actually this
code is not mine I borrowed it just for
the sake of having something to show you
guys it's probably it's called pixel RNN
the link to that could think you the
link to that code if you wanted to do
like a generative version of what we
designing today is here and that's just
from this guy Phil cuz or at least
that's his github username or there you
get up his name and they built a really
cool article that demonstrates how like
pixel art ends are really slow and
computationally intensive but you can
retain a lot of key features and points
by using a.cian
and a convolutional neural net and then
he provides some code I cleaned up some
of it and modified it to fit what I
wanted it to fit but yeah you can
essentially go there if you'd like and
try and figure out how to use their code
and it's not particularly difficult it's
pretty well built at least in my humble
opinion and it lets you kind of play
around with them and I has t data what
we're going to be doing next week is
kind of building our own version of this
from scratch kind of like we did today
with convolutional neural net actually
so we will be building using tensorflow
a full-on convolutional neural net for
you guys to play around with and yes
this will go and they github your
yurippe oh let me put that up right now
actually so yeah saves k-means
clustering so next we have Wednesday do
you know we can't do Monday Tuesday and
I'm sure Provost Wednesday would
probably be ok ok I'll double check on
that well confirm that so today was kid
part one of what will be probably a two
or three maybe four parts training on
neural networks and machine learning but
this is fascinating I really enjoyed
this and it's actually pretty simple
like ya know what the k-means clustering
algorithm is not not terribly complex I
trusted it will get more complex as we
dive into the actual neural nets already
complex kind of things so yeah stick
around for that next week we'll talk
about convolutional neural networks as
you said yes generating stream images
generating images from the same data set
that you just saw and that'll be
fascinating I can't wait to see that
we'll put a github link actually well if
you're gonna create it right now I can
just plug the Lincoln yeah so that is
the link HTTP guitar is it really so
k-means I should work just K means right
yep okay cool so that's today's code
that we wrote in adherent yep okay yeah
that and then we'll link you saw today
was the other link to the other person's
CNN or RNN oh right I will throw that up
there in one moment also pointing out ad
is not a great add dot is not a great
practice but I do yeah I do it all the
time
oh well you're okay with your images
being displayed to the public yes
I also can have it spoken you have
spoken 10 cents people are saying your
timing wasn't too bad today oh thank you
yeah it has improved a little bit I
think it's cuz I was I don't know I
think I was paying attention okay like I
think I wasn't talking quite as my I
don't know I have to realize explanation
maybe I'm just less sleepy I don't know
I have no real reasoning for that all
right so there github is here the
article that they used is the first link
I feel cuz yeah and this pixel RNN dot
git yep RN n caps cool so check that out
what was the article and the article is
also oh well is there a link to the
article only github repo I believe there
is so you should be able and I mean you
can also just type in Phil Oh Phillip
cut no I'm not gonna browse that
kuznetsova
yep cool guy this is a really cool
article it's pretty easy to walk through
doesn't require too much math and you
can kind of understand why this kind of
RNA sorry CNN has some advantages over
an RNN at least for like playing around
but yeah I would recommend kind of
reading through that article we'll talk
through a lot of the same things that he
talks are in his article we're gonna
probably spend a lot of time dealing
with like what a convolution is because
that is something from like signals
theory and processing but it is very
useful to understand what they are we
will build probably a convolutional
neural net because we'll want to
demonstrate it in the stream there will
actually probably be a bit of template
code that we'll put up just to make it a
little easier for the people at home to
follow along we'll build some helper
functions to make things a little
simpler and we'll put that all up and
did it so yeah thank you for listening
to the k-means image vomit I guess that
came out yes today no this is good this
is good stuff somebody asked what is
chat codes Python so nice segue to
tomorrow stream I'm just gonna be me
tomorrow I'm going to basically have
chat open and we're gonna write some
Python scripts but I'm not gonna write
any of the lines myself people in the
chat are gonna write the lines I'm gonna
sort of call on people and we'll
assemble a Python program line by line
based on what people say
in the chat and we'll have I think what
I'll do is I'll bring a few like target
exercises to the table like like let's
print hello world like let's create a
function that will sum two numbers or
something like that and what we'll do is
we'll mark each individual line with
that user name of that person so we can
see who's contributed to the script all
at once and we're gonna use we're gonna
use only icons for people when I call on
people so we can either do gootecks on
the stream or Kapla in the trip and the
stream and that'll let me know that I
can call on you and we'll see where it
goes it's an experiment sparked one a
big experiment last week we do the same
thing with a text adventure where the
chat wrote the whole text adventure out
and we sort of assembled it live on
camera
so tomorrow it's gonna be that but with
code so sweet it'll be another thing if
we don't know Python can we do int x
equals 5 yeah no you know if I call on
you and you give me a line I'll try and
help you fix it I will try not to put
too much of my sort of influence on the
script because I want it to fail and
succeed at the same time for comedic and
educational attainment yeah so we'll see
how it goes
it'll be it'll be interesting so tune in
tomorrow that's a 12:00 p.m. Eastern
Time and I will be curating all of the
lines so we will not be we will not
accidentally wipe your own machine ya
know we're sort of be automated like I'm
actually gonna be calling on people and
then we'll be sort of all together
writing it and curating it as we go so
yeah let's do super fun I'm very excited
yeah eval Evo no malicious code should
be possible unless I'm very susceptible
to being duped they kind of like do like
a hieroglyphic style malicious code they
send you characters you don't know
Kamino it short cuts in power I would be
a little bit skeptical of that so what I
assumed to be safe but you know make
sense plus it's gonna be multiple people
typing so everybody has to get together
and have a perfectly executed which
everyone knows this is that'll be really
fun yeah I'm excited
that's the experiment we'll see where
that goes see if people like it we'll
see if we can make it better
maybe we'll charge for languages but
it'll be in Python tomorrow and
yeah it's just experiment just for fun
oh sweet well thanks very much for today
scream thank you for as always I'm
always happy to be here I now understand
what a pleasure means clustering now if
anyone asks you like whoa k-means
clustering gave me like i'm literally
watched one get built in front of me
yeah and you could probably build one
yourself
confidence you could figure I think that
sort of seeing this stuff being built
from the ground up it makes it a lot
easier I mean it's kind of like a
selfish I think that's like common sense
but yeah but when you see it on like an
online tutorial all the codes already
been there you see the thought process I
see that child I got to see says Luigi I
don't know got C means bye Noah ciao
ciao means fire actually me talk about
okay like maybe some maybe someone who
besides the wheat you or Luigi himself
clarify that yeah good point yeah but
yeah thanks so much so we'll see you
next week
yes I'll be back next week I I think
probably on Wednesday okay figured out
thanks so much to everybody who's been
today this is no fun our first in a
probably a sequence of many videos on
machine learning you will see our faces
many many times a million different ways
screenshots hopefully in some hopefully
in some generative ways thank you see
what the computer thinks we might
horrified to think we're going to look
like a like a hybrid between you and me
that's gonna be oh yeah it'll be a time
yeah I think it'll be pretty good until
then have a good rest of the evening
depending on where you are may receptive
ear wait some are really far and really
far away from Boston this has been cs50
live with neural nets part 1 so today we
didn't really do anything that this is
the great work for it k-means cluster
you know why we wanted a neural net yeah
yeah it's the motivate I think we depth
right in and they're all gnats people
would like what the heck is going yeah
I'm pretty glad that my feel at all yeah
but no I'm very excited this has been
great thanks again so much for this
thank you guys CDs Nick Wong my name is
cold long to see you live enjoy the rest
of your day and we will see you next
time bye bye