[Music]
[Applause]
[Music]
[Applause]
[Music]
[Applause]
[Music]
[Applause]
[Music]
[Applause]
[Music]
[Applause]
[Music]
[Applause]
[Music]
[Applause]
[Music]
[Applause]
[Music]
[Applause]
[Music]
[Applause]
[Music]
[Applause]
all right hello world this is cs50 live
where we do all kinds of things we've
programmed things from scratch look at
technical concepts we looked at kali
linux last week yeah that was super
exciting long time ago my name is called
on 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 yeah I
see that Gunkel 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 shaded chance
very poppin they're gonna caption my
incessant laughs yeah it's just haha
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
L true true we feel different very wise
to me 17 MSG's is freezing bad by the
way no they want yeah
but sorry sorry dude
beautiful so we talked about
classification that ties into sort of a
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 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 learning uh
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 four or five classes
and those four or five classes are the
different streams that we've done that
has the same little box so you take
multiple screen shots 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 any 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 its 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 write the linear just the other
your favor great like there like some of
the great concepts these first comes
back Oh
yes actually a lot games at home would
be a circle not spheres yeah 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 memes
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
your situation it depends on the way
you've built it and come in to
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
labeled your data I mean your intern
labeled half of 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 because 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
500,000 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 say good 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 face to creep
out oh no no 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
I'm proficient in Python but no nothing
Wow 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
going to do this kind of high level
approach like what are we gonna go
through what are kind of the concepts
but then we're gonna repeat those
concepts pretty much throughout the
streams 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 and 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 gonna 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 it's a deep there's a lot
of things a lot of complexity it's very
interesting but I'm very excited to
learn I think a little bit 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 we'll 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 I'm sure and then
potentially in degenerative an
adversarial network 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 one's just a single parts Jane
this one having my brothers have many
parts yeah but yeah I think I think
it'll help people to get a I think it's
groundwork understanding of 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
stream will kind of cover why there
aren't necessarily as many this is a
beautiful screen so here we have for the
home 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 about an 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 special 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
harness on just a tiny little bit of
caffeine exactly I mean just like it's
you know ya know in case I'm not
energetic enough I'm not really on yeah
I also do love root beer but I'm trying
to avoid drinking too many sodas so yeah
it kind of interesting tangent to your
k-means classifier so 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 like there's all I don't know
yeah there is all it is unfortunately
not a separate class 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
literally burn oh we look yeah yeah we
we look great in every image
I unfortunately 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 my
notice is 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 all right
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 our concern is a little
different but you'll notice that the CTF
stream and our Cali stream we were both
wearing black with the exception of the
white we're gonna auction off of the
Machine of my for yeah it might
literally be that
play and your glasses out and that's a
human beings there's a Colton has
glasses there's a very shiny quality to
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 aya as opposed today it looks
like trash by the way everyone never has
those days and you don't use any
suggestions on books about memo that's a
good question so there is what is the
name of that book I have a text book 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 and so there's those places
adamantine bipartite says try scholarly
articles for ml and Google Scholar
that's definitely a great place to start
be aware that there's 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 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
our new viewers the 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 my name is Colton Ogden
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 I'm just a
full-time student correct remind me of
what your I don't remember 100% if you
were doing CS as 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 psets
constantly problems that's 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 little
bit and you've taken an ml course here
yeah and I'm also currently in RM oh 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 to 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 Icee 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 a 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 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 notifies
you know it's kind of just very fun a
lot of times you just build projects and
as you go things kind of pop out of the
woodwork
but yeah the Arras Fiesta nation
questions 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 yeah yeah so it's kind of an
appropriate question uh I actually
really like going and I'm not super
familiar with it yet but I do I do
intend on becoming like that on becoming
much more familiar with it
so going language invented by Google
sometimes referred to as just go not to
be confused with the board game and go
Lang 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 Allah like Python
JavaScript then do like C++ and C know
suppose a garbage collector it isn't it
does it has a very nice garbage to
collector so for those of them 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 civil sports um 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 can Sanchi ate 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 c++ but I spin that up and
it's got its own stack it's doing all
these things and it's pretty heavy it's
heavy a memory used to just have you can
see V usage
whereas go long threads are actually a
little bit and what I mean is it's the
language very much designed for
distributed system so that makes sense
yeah that's like its number one one of
its third 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 pi
on 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 yeah
and I actually in industry were at least
of 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 like growing
popularity I mean I think functional
programming is and always has been I
think since its inception
I think languages have more you know
recently especially they started to
adopt functional features even Java
which is did a thing go figure
c-sharp is like so great great with that
with like link varies too much but but
you know I'm more involved in 2g game
def course here and I'm into game
development recent I've been considering
the idea of making a game completely in
a functional like enclosure yeah which I
can be fascinating 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 that 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 M clock versus closure is about
the best-paying language to know
according to the new SF insights new
about that - that's no cool doesn't
happen to be the main reason that I want
to learn closure but it's a nice little
that's a nice 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
example aversion you don't know what's
manipulating what and especially when
you become so many side effects like the
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 there's your operation so
your entire cluster yeah and back to the
person's point 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
- and very performant ly it takes for
the whole pipeline and put a thousand
worth of machines that's you could
essentially model a neural network with
just your machines right that point 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 probably is 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 see like there's
all things you can do and that's true
but like I mean if you wanted to just
out of the box have a slightly more
performance as well actually a majorly
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 not really continuing
what they're saying will continue but it
will continue at scale it's going to be
a horizontal scaling issue and 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 also trucha nice asses closure and
closure 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 term yeah it is a play on the term
it also uses closure script uses the
Google closure JavaScript library so
there's all these closing close like
yeah Google one or both and you know at
programming language at the end and
Google
like you'll figure that yeah as Bob
McKnight 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 oo p and
then didn't test it and they're like
yeah it'll probably work and so it's
like not robust it does no no standard
behaviors all of a sudden defined
behavior is undefined and not documented
and like is just age pain and I'm
actually a 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 standards
seeing functional programming as written
is written sort of declaratively and
that's a word that's was I didn't react
and react is actually 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 as
a student of CS and you're 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 shocks me yeah sorry we will we
will hop back in the game ins so for a
couple more questions and then so Jenna
flavor how with you guys I'm 28
yeah I'm 20 and it's already better at
coding the not yet so oh that did
probably nothing does going have
parallel programming I'm inclined to say
almost certainly yes yes that is kind of
cool pride of doing that it's kind of
it's one they are one of its greatest
features and is it's very solid parallel
programming all right cool yeah let's
let's get we're gonna happen to kami
it's even let us I created a little file
here there are some things in the file I
promise it's not much and we'll get from
there
so I think we did yeah that's the one
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 this is the
Rocky Horror Picture Show sort of have
you got 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
etre probably not actually I'll keep
things off the right side of my outfit
so that way people can can see what's
going on so we have this import numpy as
NP very standard paradigm in Python we
also import matplotlib PI plot I'm a
huge fan of Pi plots very easy simple to
use and these are typically assists not
necessarily machine learning specific
tools but tools numerics kind of
generally yes thank you computation
without another one you might sees like
sci-fi that one's pretty common -
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 inkwell it's another big ml one
and if you wanted to hop into like even
lower level and may not lower level but
other ml there's like PI torch is really
common one and tensorflow right it 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 here with the code does
I'll point you to the guy who built it I
didn't actually build that one but
that's ok 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 are
going to hop into generalizing this and
then we'll walk through my kind of steps
for like when I'm working with images
show it 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 Harvard 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 yeah 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 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 they're kinda like David Stein
when he was like in I like computers and
stuff but he wasn't like set on CS till
he came writer and then now he's
instructor and many of the teaching yeah
it's a while time it's always
interesting to ask people their stories
for anything that they do all right 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 in that pot lab - yeah this
looks like we as heck and I'ma 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 um so generally don't use single
variable single letter variable names
alone so it extremely obvious so like I
for index is pretty clear I was thinking
color but I mean color channel it's kind
of related it's they're related topics
so it like kind of works but if you want
to be really clear you type out channel
and with tab complete it's really not
that bad but I'm super lazy and then H&W
you probably should leave those it's
like heightened with those are pretty
soon they're pretty HW 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 matte pot
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
sorry no Sarang a single list or numpy
array of just every piece of data that
was in the original image really maybe
Nestle 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 read by
so I can run this we'll 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 map plot live loads 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 it's 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 750 3344 just
entities and then when we reshaped it we
get back to the original shape and then
I went and displayed that using MATLAB
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 for 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 0 to 1 usually or 0 to 255
and that allows us to just scale across
one thing but if I had an equivalent RGB
image your color image it's 1/3 the size
because you can think of an image as
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 color is 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 tea qdm which i don't
actually know 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
dhaba 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 will
have it be a binary classifier to start
but we'll change that shortly already
we're starting to see it as so 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 a 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
processed 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
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
yeah
about the code time extensional no yeah
that is 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 for all intents and purposes is
an ID it's definitely more so than a lot
of this like competitors like Adam is
lime which I mean yeah so some
IDE features that the Ensco 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 that when I
am install it as an extension to its
native this native 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 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're actually not gonna do this in this
stream but it would just take a little
bit 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
augmentation 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
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 system 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 so
you've been original is this pre process
like an optimization for our machine
learning yeah so well 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 be
in 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 just 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 five dimensional major
in Japan so we're not gonna 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
normal
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 to write so it's very
easy to demonstrate all 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 became easy 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
ivory to post pro just pre-process not
exist in either cuz yeah these exist 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 - it works
really well it gives you 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
tend to see people build your actual
application and XS these individual
functions is there 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 it goes to none we
just want to fit the data and again this
is where we deviate oh you have anything
return done yeah you know where it is a
pure size so now our k-means classifier
has some state which is what we kind of
mean by our comedy over here is like it
just doesn't any print statement is kind
of like that - and this is effectively a
print statement so essentially what we
say is that display means is actually
going to do not it doesn't take in
anything and it does nothing there
some degree for a lot of applications
where you still need some bit of like
state some bit of input outside world
and every function was kind of a pain
I'm not an enclosure for input and
output you do not use still print do you
need yeah interfaces humans which is
kind of where we end up deviating a
little bit from function even okay well
you have like print statements and stuff
like you just do 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 out of it
there like a couple of people who are
like no it has to be pure and I'm like
yeah so we're using like a mostly kind
of functional inspired pretty imperative
to me and yeah it's pretty bad we we
deviated far from functional pretty much
after this point functional core with
the edges are imperative yeah a
functional bit at the beginning so yeah
we now have kind of set up all of our
stuff so now let's go and 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 you'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 one piece of
the time so I just want to verify you
could just read stubs and write and then
give 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 dot Lister
and this is my running of k-means pi
sorry I'll stop displaying that I
mention forgot about that student Lee so
here are all the things in that first
data directory you'll notice that there
are approximately I mean maybe you
wouldn't notice this but I happen to so
about 40 images in there's no way you
would notice that that's literally
impossible but unless maybe if you're
like isn't there about 40 images in
there there are 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 dot
absolute path oops
of f4f in that list directory and now
when I rerun this it now tells me whoa
what's going on 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 people
have you know described them as being
very functional in their design is
actually a matte filter yeah they don't
have the reduce capability in them is
not anymore
I mean you could and then apply that
right and there is the lambda calculus
like you can create lambda functions and
yeah you can't yeah I hate how limited
Linda's aren't Python they're much less
complete it's it's really just like a
kind of something set lambda is it's
basically like a like a J s like an es6
like return statement which is exactly
but she'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 what kind of just generally
all agree that it
oh I see Ellen one says our all CSM ages
a Harvard this well-rounded Brian's
linguistic 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 alone it does
a lot went because applications right oh
yeah so meteorologist 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
I 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 do you not
or is this 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 its ability to Chinese is
basically is 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 uh 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 work so many
words and other also others
it's 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 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 to be
like well should I put that there should
I not there's also 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 you learn it through exposure
constant right he really 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
it was very fun 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 do
them 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 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 M read you could make it an S
your comprehension or yeah you wouldn't
necessary 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 got a lot of information in
there I think this is fairly reasonable
for people to read yeah sorry it 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 uh
I just 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
Mikey bucking 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 absolute path is 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 only there's not make sense i hain't
no bulky oh it's not path calls get and
Python it just like they span like
there's Evan line it's a huge pain 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 end by H by W diet by
C and we can verify that here except
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 h here 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 for 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 a
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 hewed RGB it'll fill in the
rest of them for you so you can go
google it is no 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 it 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 hue
saturation and luminance that's that Oh
your color oh that's cool those of you
who are just kind of listening casually
I guess I do that with YouTube sometimes
pac-man said that hue saturation and
luminance are the three things for
converting from like the humours into 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
sound so there's a there's some really
cool stuff like with you and use the HSL
or HSV
el model for color right modeling and in
games like you can actually like I used
to be really in a 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
have 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 by 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 those
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
hue saturation luminance and opacity and
data which is way less catchy sorry
so HLS Oh slow HM yeah
we're probably I'm not gonna attempt to
say that any more on this yeah you know
it's a 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 process
the 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 dot reshape and this
is what we do the movie now we're
putting a function and so all of our
heads 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 a new all right it's 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 code
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 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 the
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 if you convert our house into
a flat line of enough version and now we
just have like 40 lines of numbers that
can examle math onto to do magical
things exactly and so we're gonna delete
these pre lines that we had up here so
now that we've pre-processed our data we
look down and we recognize that the
actual k-means classifier is 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
moment 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 10 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 like might
also be built in I'm just not familiar
with it but I will probably look at it
right after 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
it be like if K equals blah blah blah
some addition your assignment check a
gets the value and then because it you
can't do in a Python them right but
they're going to add them 3.8
using a lambda expression almost like
being inspired by sepals
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 working the
reasons that he wanted to leave myself
what is it could be good incorrect we
don't you're on the stream website okay
so you think all right so you have this
in it's like we didn't announce in this
terrible is like yeah 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 end it
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 of the underscore and so on but I'm
gonna just pretend everything's public
in our objects and raise fine-ish
question mark is very very loose object
orientation principles compared to
language like Java exactly thing is very
protective in C++ is actually even more
stringent I believe I'm really Qatar
with its classes oh yes specifically
friends 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 I thought would you can use
do whatever you want yeah exactly
I'm just do this dynamic languages
versus static languages it's a good
point Oh Sergei says he's strongly
supported this idea oh that's cool
okay I'm glad to know cuz I think it's
on I've actually 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 a way so
the fact that they vary timing lis are
adding it to the language is kind of
cool all right I'm sorry I only just had
it yeah so I was just I was adding some
template codes and things I can now run
this entire file and nothing will break
it okay nice an empty class right now
for the most it does nothing spy what
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 McKnight
points out everything is public in
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
der 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 get 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 other things can use the same
set of functions exactly and actually
just to kind of highlight that will
redefine the stur function 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 kolnas pointed out I'm
a little getting old very good point
I should say so what this does is
essentially the same as what I had
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 bro not I
don't use it now but I used it a lot
like even as three point six was still
out of a soldier still like two point
seven modes right so I didn't really get
too used to 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 and
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 this
is this all sorts of like funny things
with this so instead of now printing out
a derp of k-means we're just gonna
sprint 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 okay and
then store we can actually mention that
methods donors on both sides like stur
are not hidden like methods prefix
writer prefixing will obscure the name
by adding the class name as a suffix
that's kind of cool that is a good point
like I got a sort of sloppy
privatization yeah it works
I'm sure does but it's not but it's not
as clean as a C++ enforced class or a
Java oh and actually map book was
mentioning the preservation of dict
order with 3.6 that was such a nice
right thank you
obviously they're hashed so they don't
have order defined order area like an
order sign stone 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
Sanchi eight I don't know if I've ever
used the ordered it class I feel like I
always just worked around it I I was
always just like yeah okay it's out and
I'm going to use it when using a
dictionary you're like not always
concerned about the ordering butts right
it's important they can matter and a lot
of times is 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
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 performant even in
Python we're going to avoid those for
clarity just as a disclaimer
so the clusters is going to be some list
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 as 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 a
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
does 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 Python 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
some things you didn't yeah there are
street there are sorry those are streams
there are functions yeah that native
functions that differ like what's in the
standard library like you know like
reversing sort or an example is there
one doesn't in place and one doesn't
return yeah it's weird yes it's like why
is consistent started ask why tomorrow
same 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 you
shuffled by default who did say oh I'm
not sure it's probably something Sicily
yeah you can go google the documentation
see which stuff he might have a sincere
algorithm is also possible I don't think
it's n P random got split its 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 parts
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 baliya for you
data items no it's not that many things
it's divisible by so if I threw in
something like 9 it would freak out
I know potential way to get around it
yeah no no solution you just freak out
and cry 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 collectors yeah
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 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 we're gonna 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 like you
like remember you assign recalculate it
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 maybe
we'll display the results of different
iteration numbers and kind of look at
what they look like but we'll probably
only iterate around 10 times and 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 four 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 my
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
I mean for each of our clusters for each
of the K clusters so we're gonna 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 duty 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 MP dot reshape of the image itself
and 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 wit
oh my god i can't type I swear that's
the stream internal it really is every
time right with channel just telling
Python that we want to use the global
versions of those and you're 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 are on mean so this can show a
caustic and 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 a mat fossil exactly it
actually well I'll use control-c n just
exists so how do the easy formats change
them yeah we're all 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 mr. B in numerate it's
like that doesn't work
so anyway it just takes an iterable and
then it gives me an index as the first
thing out of that interval it's really
convenient you would get indexed by
default when you just use me 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 me and 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 it's
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 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 a single
dimensional thing back into 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 a 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 many circles I'm not I
don't know much about writing so but
this is I can understand everything
you talk 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 quit sorry I'm also my
typing and talking at the same time have
single-threaded a little bit yeah we're
on the same where I can't 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
adjust its 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 as 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 screw things up so there's
like things and reasons why you might
want to 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 from 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
men not use this and our pre process
step is actually also going to return to
us the parameters sizes of
data and so this will return to us an
HWC version of the data so it returns
the shape changing number was my
favorite type yes so many numbers well I
mean so this has a mat like a meeting -
it's 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
well 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
doing that all right 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 who's
already said this in the chat but that's
okay we're gonna there you go cool so
now I have those parameters that 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 that 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 1 you'll notice that
you're kind of f yeah I'm really serious
here yeah we're kind of in like a
dreamlike state you see LF python is
like hardcore just sticking out yeah
then there's like a produce over here oh
and we're freaking out every time I post
the image I'm right what that's the
image anymore and if we go to cluster 2
it's not a whole lot better I think I
have about 4,000 different pairs of eyes
your hair is thank you see you can see
my oh yeah it's so much you see my face
is kind of looking in like a ghost 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
on or they someone's tripping a hard on
some LSD background so these are
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 that I 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 say and I
hate to break it to you you may wish to
go see it acting and then we can kind of
scroll through here my smile is very
defined and then your everything
and whatever that monstrosity is on the
right I always love doing it it's very
entertaining this is just this is just
from one mean recalculation though 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 yeah I like keep doing
this over and over again to actually get
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 we
calculated the mean but we haven't
actually like really lab and then the
reassign them I mean so for I in range
people are gonna shadow 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 was forget rich Python
amusing who is X range yes yes yeah so
we now have this like iterations that's
cool like a read don't hard-code waiting
extension is that that does the comment
coloring was a great question I think
it's prettier comments let me see Oh
better comments nice 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
don't know the 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 doing 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 four iron 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 going to do is iterate
over the data and find which cluster
each in
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 forty images it's not
too bad if you wanted to do this well
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 have watch
the chat a few body parts say it says
that ignite it's too late for that
you've already insulted him that's me
Gracie means nothing now don't worry
about ignite you know that 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
alright 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 sorry distance function the
difference between it and the cluster
that we're currently looking oh yeah
okay well it was good you know to you're
getting Unitas I really should have kept
my mouth shut
can you do that I can use to a list
literal instead of saying the list
dallisa instructor 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
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 indict or 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 does an empty set literal
behave as an empty dick literal I
believe so no really I don't quote me on
that I know I said that because it makes
them does it 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
blog well in that case you it wouldn't
work you'd have to say set something
equals that no yeah that's a good point
so I actually I guess I just use the
constructor because it clarifies to me
on quick reading maybe it's just a bad
habit but I've gotten into good point
different folks yeah that's good good
good point 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 going to call this distances and I
wanted 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 distance of
image and cluster will say cluster mean
for oh man this might not work super
cleanly as Alice Kober engines I like to
use the whenever 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 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 didn't need to find part of
me is worried that I'm not
what you're doing and then a laser but
you're right interesting function 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 Jenny
calculated 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 one with two it's
like x2 minus x1 yeah like the square
root of all actually we're 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 time I'll
clean it up uh welcome to code okay just
laziness
all right so we'll break this function
apart even though you could convince 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 sums 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 NP 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 17 I and
I subtracted from the corresponding
pixel number 79 and then I called 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 is recording 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
individual point wise distances and then
the distance is the square root to kind
of compensate for the fact that
the squared term here now you'll notice
mathematically that'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 deltas 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 yep every night recommended
Manhattan distance we have no idea what
that not super familiar with it but yeah
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 almost as 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 common is yeah
the minimum one so we're gonna do n PETA
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 I
don't know what is L that's your L came
from it's really just kind of like a
kind of standard I'm like the Wikipedia
page is 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 yeah
- 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 / euclidean distance out
the name 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 even 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 and 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 pre
Carol at the moment so quick 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
yeah 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 care about which is a
good point so essentially this is it
we've now fitted our data so what happen
is here twice or two dads confer nice
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 be ators
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 in
a garbage select the prior mean images
the old 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 trio Luigi Cu which is a long
time cs50 follow around the Facebook and
we've met this person in the UK it was
pretty cool that's wild oh that's crazy
that's awesome 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 he's gonna 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 out no one
really pointed this out but we can 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 add many of these steps
this would make things a little bit more
convenient it's pretty cool Wow
look and it's looking a little bit
cleaner hearing right and this is after
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 about like crazy
tan and then like heart rate 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 right place isn't that
of existence like mostly in the right
universe and then here we have Wow
Colton's like pensive look is very clear
in 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 or so okay that was again
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 oh 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 found a lot when I
talked 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 of photograph yeah it really
does oh and then if we kind of move here
this is don't think I'm just like
looking left and right 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 and actually to kind of
quantify what's going on here
we're gonna calculate kind of the
overall
Everage 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 as a number of
ways of calculating this we're gonna do
kind of a hawk ad hoc method for doing
this
so for each image we're also going to
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 dot 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 min distances equals 0
and so on each iteration we'll 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
going to divide that by a 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 the 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 lists you know sorry no default
constructor there and we're just going
to self dot average distances append
that
there's my corresponding parentheses 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 that figure
and then we will do a plot dot scatter
no we don't we just carry what the plot
there we go the classic classic average
distances and then plot that show and
we'll do plot the title average
distances over iterations and then plot
that X X label iteration number and
average distance and this allows us to
then 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
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 paying through I'm
actually gonna add a try I'm gonna try
and do a little bit of well do a little
bit of everything 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 I mean 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 I see 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 two 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 it 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 our data sets
pretty pretty simple because we're going
to 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 her mean to zero and it'll set
our standard deviation to one thing
we're going to do that we're going to
get the image mean which is actually
just NP mean of flattened axis is again
equal to zero and then we're going to
get the image s TD which is NP dot I
believe us TD yeah flattened axis again
equals zero and we can say normalized is
actually equal to flattened - 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 and
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 now scalars 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
stand 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 non numbers
so this is like I guess there's actually
bangs semi-normal this is the right mean
it has the wrong standard deviation
which is not going to help me improve 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 show which is what this
error is saying it's going to flip it to
the correct parameters 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 d-type possibly and
print that out 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 uh
normalize them or before you go to like
try an image show 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 going to
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 try to divide by zero
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 I think you should try yes
so as mm points out variance cannot be
zero that's a great point has to be oh
that's positive my intent basically
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 mu zero
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 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 you
guys 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 has meaning
but I also wants to be able to was I'm
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 is T 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 50 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 0 through 0
through 9 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
percent to be massive although we were
actually still able to get through very
many of them very quickly
yeah because numpy is pretty powerful
she 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
there like a five here maybe like a 0
and a 1 and essentially what this did
was the program is actually this code is
not mine and I
it just for the sake of having something
to show you guys
it's from it's called pixel RNN the link
to that canoe thank 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 user
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 CNN 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 get 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 kinda 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 on a github repo 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 okay okay ought to
double-check on that well confirm that
so today was K 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 yeah that's not 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 net is a pretty complex
kind of things so yeah stick around for
that next week we'll talk about
convolutional neural network so you said
yes generating stream image generating
images from the same dataset 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 kisses
sorry didn't release calm / me I'm
worried about
so k-means that should work just K means
right yep okay cool so that's today's
code that we wrote in adherent yep okay
yeah that 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 ad dot is not a great practice but
I do that yeah I do it all the time oh
boy you're okay with your images being
displayed to the public yes I am I also
can have it spoken you've spoken sense
people are saying you're typing 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 I like I think I wasn't
talking quite as much 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 and 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 could also
just type in Phil Oh Phil look cut no
I'm Michael Browns 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 or
it's 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 through 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 will put
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 they 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 then 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 in 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 of
experiment last week we did 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
interesting if we don't know Python can
we do in 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 p.m. Eastern time and I
won't be curating all of the lines so we
will not be we will not accidentally
wipe your own machine yeah no we're not
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 go 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 the mean and
all that sort cuts and power I would be
a little bit skeptical of that so what I
assumed to be safe but you know make
sense it's gonna be multiple people
typing so everybody has to get together
and it's a grammatically executed which
everyone knows that's just that'll be
really fun yeah I'm excited
that's the experiment we'll see where
that goes see people like it we'll see
if we can make it better maybe we'll try
out different languages but it'll be in
Python tomorrow and yeah it's just
experiments just for fun oh sweet well
thanks very much for today scream thank
you for us as far as always I'm always
happy to be here I now understand what
pleasure means clustering now if anyone
asks you like whoa k-means clustering
you me like I'm literally watched one
get built in front of me yeah and you
could probably build one yourself
yeah 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 oh 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 yeah simple thought process
that child I got to see says Luigi I
don't know got C means by know it ciao
ciao means fire actually means how about
like maybe some maybe someone 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 will be back next week I
think probably on Wednesday okay figured
out thanks so much to everybody who's
been in a chat today this is fun our
first in a probably a sequence of many
videos on machine learning you will see
our faces many many times in many
different ways
particular screenshots hopefully in some
hopefully it's some generative ways
thank you yes see what the computer
thinks we might horrified to think we're
going to sort of like a
hybrid between you and me that's gonna
be oh yeah it'll be a time yeah it'll be
a good time who'd be good sound I'm
excited yeah I think it'll be pretty
good until then have a good rest of the
evening depending on where you are I'm a
receptive ear wait somewhere really far
really far away from Boston this has
been cs50 live with neural nets part one
so today we didn't really do anything
there was a neural nets this is the
groundwork for it cain't means cluster
do you know why we wanted a neural net
yeah yeah it's the motivator 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
a little much yeah but no I'm very
excited this has been great thanks again
so much for this thank you sixties 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