ML101

Francis Tseng (@frnsys)


let's start setting up b/c it will probably take awhile ⏳

git clone https://github.com/frnsys/ml101
cd ml101/altai
pip install -r requirements.txt
jupyter notebook

what we'll cover today


<http://work.caltech.edu/library/>
http://work.caltech.edu/library/

machine learning is a massive field!

what ties it together?


we'll only be able to go into depth with a couple techniques, but the intuition you'll gain will make it easier to approach other parts.


the techniques we'll learn today are behind neat things like...


to do that kind of stuff, we'll learn how to use these tools:

  1. scikit-learn for supervised linear learning
  2. keras for neural networks
  3. pandas for handling data
  4. matplotlib and seaborn for visualizing data

assumptions


What do you know about machine learning? πŸ’¬


ok, so what the heck is machine learning doing? what is it even for?

πŸ’»πŸ€”


✨~modeling the world~✨


we can model phenomena, both natural and artificial, as mathematical functions

y ⟢ $f(x)$

πŸ’« ⟢ $f(βš›)$

πŸ’Ή ⟢ $f(πŸ“°)$

🎼 ⟢ $f(🎼)$

🎼 ⟢ $f(🎼,😢)$


which basically means we want to find a mapping of some input to some output in a way that reflects reality

http://mathinsight.org/function_machine
http://mathinsight.org/function_machine

machine learning is all about finding these mappings (i.e. functions) from data (which is assumed to reflect reality)


so why would you even want to learn one of these functions?


once you learn your mystery function, there's so much you can do with it...


make predictions



Colornet



thingscoop


automate decisions


World Leader Tips (Brian Clifton)


Neuroaesthetics in Fashion (Edgar Simo-Serra, Sanja Fidler, Francesc Moreno-Noguer, Raquel Urtasun)


gain insight into a system



Social Distance in the United States: Sex, Race, Religion, Age, and Education Homophily among Confidants, 1985 to 2004 (Jeffrey A. Smith, Miller McPherson, Lynn Smith-Lovin, University of Nebraska - Lincoln, 2014)


emulate a system


e.g. modify this image to match this person's style


A Neural Algorithm of Artistic Style (Leon A. Gatys, Alexander S. Ecker, Matthias Bethge)


Geocities Forever (Aanand Prasad)


let's work through a concrete example to better understand this.

(this example may feel a bit disconnected, but I promise it will make the more interesting stuff easier to understand)


you're a fashion company and you want to create a line of on-demand perfect-fit pants.

the existing waist-size system is imprecise and inconsistent, and you're convinced you can figure out the exact centimeter waste size based on someone's weight alone.

i.e. we want to predict someone's waist size given their weight.


to put this another way, you want to find a function that maps someone's weight (input) to a waist size (output).

$$ \text{size} = f(\text{weight}) $$

what is $f$??


how could we come up with such a function? πŸ€”

there are infinitely many of them


first, we can collect some data and try to narrow things down.


we go out and survey a bunch of people, getting their weight and then measuring their waist size.


we plot the data:

what does it look like?


(kind of like a line)


remember that a line can be described in the general form of

$$ y = mx + b $$


if we assume that a line fits this data well, and it looks like it does, then we only need to look at functions that take the form $y=mx+b$.


remember that lines vary depending on what the values of $m$ and $b$ are:

Lines
Lines

we say that $m$ and $b$ parameterize the function ($m$ and $b$ are called "parameters").


these parameters define a unique function, so what we're actually looking for is values for $m$ and $b$.

once we get those, we just plug them in and we get our function πŸ™ƒ



but...because $m$ and $b$ could literally be any value, there are also infinite possible lines!


anyway, we've at least narrowed things down to the point where we can "guess-and-check" different values of $m$ and $b$.

we can eyeball it and seems like if the resulting line is a good fit and stop when we're content.

(to the notebook πŸ—’)


that was pretty tedious...

what's the best way to deal with tedious processes?


(have a computer do it)

πŸ€–


this is in essence what "machine learning" is:

using computers to learn functions from observations (data)


so now let's try this with a computer

(example in notebook πŸ—’)


now that we have this function, a customer can give us their weight and we can tailor our pants size to them!


that worked pretty well didn't it? can we just stop there? why do we even need πŸŽ‰~neural networks~πŸŽ‰?


let's consider one more example problem.

we have some data about how many pants we sell throughout the year. we've been pretty wasteful with our production - we'll make a lot of pants some months and sell very few.

it would be nice if we could predict how many pants we'd sell that month so we produce only that much.


(example in notebook πŸ—’)


essentially every machine learning technique (e.g. linear regression, neural networks, SVMs, naive Bayes, etc) learns what these parameters are.

algorithms are differentiated by:


the example problems we have seen are called regression problems - they involve predicting some continuous value.

but there is another type of problem - classification - that also comes up a lot. in these problems, we want to predict which category some input belongs to.


Deep Learning for Data Scientists (Andrew B. Gardner)


we can have more than just two categories:


ImageNet Classification with Deep convolutional Neural Networks (Alex Krizhevsky, Ilya Sutskever, Geoffrey E. Hinton)


with classification problems, we are still trying to find a function, but instead of having it fit "on top" of the data, we want it to weave through the different categories in the data.


(example in notebook πŸ—’)


one particularly fun kind of neural network is a recurrent neural network (RNN)

these are great for modeling sequences, e.g. textπŸ“, music🎢, time seriesπŸ•, and so on


examples:


the problem we'll look at is something like:

given the character "f", what character do you think comes next?


ok, what if I give you the character sequence "fu".

now what do you think comes next?


ok, what if I extend that sequence to "fuc".

now what do you think comes next?


what if I told you the text that preceded it was "my favorite color is"?



you were very unlikely to get the right answer given just "f" or "fu" or "fuc" - you needed the context.

if instead I had given the context of "what the", you'd have guessed something different.


so context is really important, and RNNs are great at learning it.


sounds like a lot of fun, but there's one hangup...

πŸ™


what if the inputs we want to use aren't numbers? πŸ€”

for example, how do you put a piece of text into a function?

πŸ“– ⟢ πŸ”’

how do you represent it as a number?


this is the problem of representation, and it is key to machine learning.


for text, one option is to map each word to a unique number.

πŸ™ƒ


(example in notebook πŸ—’)


one last kind of machine learning that I really enjoy



how would you program an agent to play that well?


what we've seen so far is pretty cool, but things really get fun when we start designing agents that can act independently

where they can interact with an environment on their own, learn from it, and develop new behaviors πŸ€–

reinforcement learning agents can learn optimal behaviors given new and unfamiliar environments. this can be games, or business management, or controlling robots, etc!


in Humans of Simulated New York we used reinforcement learning to simulate the government and businesses who try to respond to demands of the market, the needs of their workers, desire to increase profits (in the case of businesses) or the welfare of their citizens (in the case of the government).

e.g. when a business has leftover stock, they learn to produce less, if they have excess, they try to increase profit margins, etc


reinforcement learning is behind neat stuff like Google DeepMind's AlphaGo πŸ† and their Atari-playing AIs πŸ•Ή


the behavior of the agent can be described by (you guessed it) a function, and of course we want to learn it πŸŽ“


the basic idea:

this is given to us by a function, usually called $q$, which, given a state, maps actions to values - so this is the function we want to learn.


a very simple set of agent states and actions. this agent will eventually spend all of its time sleeping πŸ’€.


a very simple environment for a RL agent is a "grid world"


(example in notebook πŸ—’)


one main problem in RL is "exploration vs exploitation".

do you stick with certain reward, or risk the unknown for possibly greater reward?

e.g. ordering food at a restaurant, choosing a major in college, etc.


another problem is "credit assignment" - if something good happens, is it because of the last thing you did? or is it because of a decision you made a long time ago?


now let's try combining what we've learned:

reinforcement learning + neural networks

(we'll make a simple version of the Atari player)


it's a lot like a classification problem.

this is the architecture of the Atari player:


(example in notebook πŸ—’)


through this process you've seen how much decision making we as people had to make with regards to what goes into the algorithm, which one we use, and so on.

πŸ€”

hopefully it is clear that machine learning can't be claimed to be "fully objective" 🚽 or anything of the sort


thanks!

if you want to go deeper: frnsys.com/ai_notes/

questions? comments?
message me: @frnsys

homework: explain a concept you learned today that you found
interesting to someone from a non-technical background
let us know how it goes!