\documentclass[pdf]
{beamer}
\mode<presentation>{}
%% preamble
\title{COMS 4995 Lecture 1: Introduction}
\author{Richard Zemel}
\date{}
\usetheme{CambridgeUS}

\usepackage[round]{natbib}

\input{../../Misc/macros.tex}


\begin{document}

%% title frame
\begin{frame}
\titlepage
\end{frame}


\begin{frame}{Course information}
  \begin{itemize}
  \item Second course in machine learning, with a focus on neural networks
    \begin{itemize}
    \item This is an advanced machine learning course following Intro to ML with an in-depth focus on cutting-edge topics
    \item Assumes knowledge of basic ML algorithms: linear regression, logistic regression, maximum likelihood, PCA, EM, etc.
    \item First 2/3: supervised learning
    \item Last 1/3: unsupervised learning and reinforcement learning
    \end{itemize}
  \item Two sections
    \begin{itemize}
    \item Equivalent content, same assignments and exams
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Course information}
  \begin{itemize}
  \item Formal prerequisites: % or equivalent in the tri-campus system:
    \begin{itemize}
    \item {\bf Multivariable Calculus} %:} MAT235/MAT237/MAT257/equivalent
    \item {\bf Linear Algebra} % MAT221H1/MAT223H1/MAT240H1/equivalent
    \item {\bf Machine Learning} % CSC311/STA314/equivalent
    \end{itemize}
  \item Prerequisites will be enforced, including for grad students. See details on the FAS calendar.
  \end{itemize}
\end{frame}


\begin{frame}{Course information}
  \begin{itemize}
  \item Expectations and marking {\bf (undergrads)}
    \begin{itemize}
    \item Written homeworks (30\% of total mark)
      \begin{itemize}
      \item Due Thurs nights at 11:59pm 
      \item first homework will be out 1/17, due 1/24
      \item 2-3 short conceptual questions
      \item Use material covered up through Tuesday of the preceding week
      \end{itemize}
    \item 4 programming assignments (40\% of total mark)
      \begin{itemize}
      \item Python, PyTorch
      \item 10-15 lines of code
      \item may also involve some mathematical derivations
      \item give you a chance to experiment with the algorithms
      \end{itemize}
    \item Exams
      \begin{itemize}
      \item midterm (10\%)
      \item final project (20\%)
      \end{itemize}
    \end{itemize}
  \item See Course Information handout for detailed policies
  \end{itemize}
\end{frame}

\begin{frame}{Course information}
  \begin{itemize}
  \item Expectations and marking {\bf (grad students)}
    \begin{itemize}
    \item Same as undergrads:
      \begin{itemize}
      \item Written homeworks: 30\%
      \item Programming assignments: 40\%
      \end{itemize}
    \item {\bf Final project: 30\%}
    \end{itemize}
  \item See Course Information handout for detailed policies
  \end{itemize}
\end{frame}

\begin{frame}{How to get free GPUs}
  \begin{itemize}
  \item {\bf \href{https://colab.research.google.com/}{Colab} (Mandatory)} Programming assignments are to be completed in Google Colab, which is a web-based iPython Notebook service that has access to a free Nvidia K80 GPU per Google account. 
  \item {\bf \href{https://cloud.google.com/compute/}{GCE}  (Recommended for course projects)} Google Compute Engine delivers virtual machines running in Google's data center. You get \$300 free credit when you sign up.  
  \item See Course Information handout for the details
  \end{itemize}
\end{frame}

% \begin{frame}{Course information}
%   \begin{itemize}
%   \item Textbooks
%     \begin{itemize}
%     \item None, but we link to lots of free online resources. (see syllabus)
%       \begin{itemize}
%       \item Professor Geoffrey Hinton's Coursera lectures
%       \item the Deep Learning textbook by Goodfellow et al.
%       \item Metacademy
%       \end{itemize}
%     \item Readings are available for some of the lectures.
%     \end{itemize}
%   \item Tutorials
%     \begin{itemize}
%     \item Roughly every week
%     \item Programming background; worked-through examples
%     \end{itemize}
%   \end{itemize}
% \end{frame}

\begin{frame}{Course information}
  
  Course web page: \url{http://coms4995-columbia.github.io/2021/}
  
  \vspace{1em}
  
  Includes detailed course information handout
  
\end{frame}



\begin{frame}{What is machine learning?}
  \begin{itemize}
  \item For many problems, it's difficult to program the correct behavior by hand
    \begin{itemize}
    \item recognizing people and objects
    \item understanding human speech from audio files
    \end{itemize}
    \pause
  \item Machine learning approach: program an algorithm to automatically learn from data, or from experience
    \pause
  \item Some reasons you might want to use a learning algorithm:
    \begin{itemize}
    \item hard to code up a solution by hand (e.g.~vision, natural language processing)
    \item system needs to adapt to a changing environment (e.g.~spam detection)
    \item want the system to perform \emph{better} than the human programmers
    \item privacy/fairness (e.g.~ranking search results)
    \end{itemize}
  \end{itemize}
\end{frame}

% \begin{frame}{What is machine learning?}
%   \begin{itemize}
%   \item It's similar to statistics...
%     \begin{itemize}
%     \item Both fields try to uncover patterns in data
%     \item Both fields draw heavily on calculus, probability, and linear algebra, and share many of the same core algorithms
%     \end{itemize}
%     \pause
%   \item But it's not statistics!
%     \begin{itemize}
%     \item Stats is more concerned with helping scientists and policymakers draw good conclusions; ML is more concerned with building autonomous agents
%     \item Stats puts more emphasis on interpretability and mathematical rigor; ML puts more emphasis on predictive performance, scalability, and autonomy
%     \end{itemize}
%   \end{itemize}
% \end{frame}

\begin{frame}{What is machine learning?}
  \begin{itemize}
  \item Types of machine learning
    \begin{itemize}
      \item {\bf Supervised learning:} have labeled examples of the correct behavior, i.e. ground truth input/output response
    \item {\bf Reinforcement learning:} learning system receives a reward signal, tries to learn to maximize the reward signal
    \item {\bf Unsupervised learning:} no labeled examples -- instead, looking for interesting patterns in the data
    \end{itemize}
  \end{itemize}
\end{frame}


\begin{frame}{What are neural networks?}
  \begin{itemize}
  \item Most of the biological details aren't essential, so we use vastly simplified models of neurons.
  \item While neural nets originally drew inspiration from the brain, nowadays we mostly think about math, statistics, etc.
  \end{itemize}
  \begin{center}
    \includegraphics[width=0.8\textwidth]{figures/neuron.png}
  \end{center}
  
  \vspace{-1em}
  
  \begin{itemize}
  \item Neural networks are collections of thousands (or millions) of these simple processing units that together perform useful computations.
  \end{itemize}
\end{frame}

\begin{frame}{What are neural networks?}
  \begin{center}
    \includegraphics[width=0.8\textwidth]{figures/turing_award.png}
  \end{center}
  
  \vspace{-2em}
  {\tiny (Krizhevsky et al., 2012)}
\end{frame}

\begin{frame}{What are neural networks?}
  Why neural nets?
  \begin{itemize}
  \item inspiration from the brain
    \begin{itemize}
    \item proof of concept that a neural architecture can see and hear!
    \end{itemize}
  \item very effective across a range of applications (vision, text, speech, medicine, robotics, etc.)
  \item widely used in both academia and the tech industry
  \item powerful software frameworks (PyTorch, TensorFlow, etc.) let us quickly implement sophisticated algorithms
  \end{itemize}
\end{frame}


\begin{frame}{What are neural networks?}
  \begin{itemize}
		\item Some near-synonyms for neural networks
    \begin{itemize}
    \item ``Deep learning''
      \begin{itemize}
      \item Emphasizes that the algorithms often involve hierarchies with many stages of processing
      \end{itemize}
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{``Deep learning''}
  
  Deep learning: many layers (stages) of processing
  
  \vspace{0.5em}
  
  E.g.~this network which recognizes objects in images:

  \begin{center}
    \includegraphics[width=\textwidth]{figures/alex_net.pdf}
  \end{center}
  
  \vspace{-2em}
  {\tiny (Krizhevsky et al., 2012)}

  \vspace{1em}
  Each of the boxes consists of many neuron-like units similar to the one on the previous slide!
  
\end{frame}

\begin{frame}{``Deep learning''}
  \begin{itemize}
  \item You can visualize what a learned feature is responding to by finding an image that excites it. (We'll see how to do this.)
  \item Higher layers in the network often learn higher-level, more interpretable representations
    \begin{center}
      \includegraphics[width=0.85 \linewidth]{figures/olah.png}
    \end{center}
    \begin{flushright}
      \begin{tiny}
        \vspace{-1em}
        \url{https://distill.pub/2017/feature-visualization/}
      \end{tiny}
    \end{flushright}
  \end{itemize}
\end{frame}

\begin{frame}{``Deep learning''}
  \begin{itemize}
  \item You can visualize what a learned feature is responding to by finding an image that excites it. 
  \item Higher layers in the network often learn higher-level, more interpretable representations
    \begin{center}
      \includegraphics[width=0.6 \linewidth]{figures/olah2.png}
    \end{center}
    \begin{flushright}
      \begin{tiny}
        \vspace{-1em}
        \url{https://distill.pub/2017/feature-visualization/}
      \end{tiny}
    \end{flushright}
  \end{itemize}
\end{frame}


\begin{frame}{What is a representation?}
  \begin{itemize}
		\item How you represent your data determines what questions are easy to answer.
    \begin{itemize}
    \item E.g.~a dict of word counts is good for questions like ``What is the most common word in \emph{Hamlet}?''
    \item It's not so good for semantic questions like ``if Alice liked \emph{Harry Potter}, will she like \emph{The Hunger Games}?'' 
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{What is a representation?}

  Idea: represent words as vectors

  \vspace{-0.5em}

  \begin{center}
    \includegraphics[width=0.8 \textwidth]{figures/embedding.pdf}
  \end{center}

\end{frame}

\begin{frame}{What is a representation?}
  \begin{itemize}
		\item Mathematical relationships between vectors encode semantic relationships between words
    \begin{itemize}
    \item Measure semantic similarity using the dot product (or dissimilarity using Euclidean distance)
    \item Represent a web page with the average of its word vectors
    \item Complete analogies by doing arithmetic on word vectors
      \begin{itemize}
      \item e.g. ``Paris is to France as London is to \underline{\phantom{xxxxx}}''
      \item France -- Paris + London = \underline{\phantom{xxxxx}}
      \end{itemize}
    \end{itemize}
    \pause
		\item It's very hard to construct representations like these by hand, so we need to learn them from data
    \begin{itemize}
    \item This is a big part of what neural nets do, whether it's supervised, unsupervised, or reinforcement learning!
    \end{itemize}
  \end{itemize}
\end{frame}





\begin{frame}{Supervised learning examples}

  {\bf Supervised learning:} have labeled examples of the correct behavior
  
  \vspace{1em}
  
  e.g.~Handwritten digit classification with the MNIST dataset
  \begin{itemize}
  \item {\bf Task:} given an image of a handwritten digit, predict the digit class
    \begin{itemize}
    \item {\bf Input:} the image
    \item {\bf Target:} the digit class
    \end{itemize}
    \pause
  \item {\bf Data:} 70,000 images of handwritten digits labeled by humans
    \begin{itemize}
    \item {\bf Training set:} first 60,000 images, used to train the network
    \item {\bf Test set:} last 10,000 images, not available during training, used to evaluate performance
    \end{itemize}
    \pause
  \item This dataset is the ``fruit fly'' of neural net research
  \item Neural nets already achieved $>$ 99\% accuracy in the 1990s, but we still continue to learn a lot from it
    % \item Current best algorithm has only 0.23\% error rate on the test set!
  \end{itemize}
  
  

\end{frame}

\begin{frame}{Supervised learning examples}

  What makes a ``2''?

  \begin{center}
    \includegraphics[width=0.5 \textwidth]{figures/what_makes_2.pdf}
  \end{center}
\end{frame}





\begin{frame}{Supervised learning examples}
  Object recognition
  \begin{center}
    \includegraphics[width=0.6 \textwidth]{figures/object_recognition.pdf} \\
    {\tiny (Krizhevsky and Hinton, 2012)}
  \end{center}
  ImageNet dataset: thousands of categories, millions of labeled images
  
  \vspace{0.5em}
  Lots of variability in viewpoint, lighting, etc.
  
  \vspace{0.5em}
  Error rate dropped from 26\% to under 4\% over the course of a few years!
\end{frame}


\begin{frame}{Supervised learning examples}
  Caption generation
  \begin{center}
    \includegraphics[width=0.90 \textwidth]{figures/attn_capgen.png} \\
    {\tiny (Xu et al., 2015)} \\
  \end{center}
  Given: dataset of Flickr images with captions
  
  More examples at \url{http://deeplearning.cs.toronto.edu/i2t}
\end{frame}


\begin{frame}{Supervised learning examples}
  Neural Machine Translation
  \begin{center}
    \includegraphics[width=0.5 \textwidth]{figures/google-nmt-lstm.png} \\
    {\tiny (Wu et al., 2016)} \\
    \includegraphics[width=0.8 \textwidth]{figures/gnmt-results.png} \\
  \end{center}
  Now the production model on Google Translate
  
\end{frame}


% \begin{frame}{Unsupervised learning examples}

%   {\bf Unsupervised learning:} no labeled examples -- instead, looking for interesting patterns in the data

%   \vspace{1em}

%   E.g.~visualization of documents; algorithm was given 800,000 newswire stories, and learned to represent these documents as points in two-dimensional space

%   \vspace{-2em}

%   \begin{center}
%     \includegraphics[width=0.4 \textwidth]{figures/newswire.pdf}
%   \end{center}

%   \vspace{-1em}

%   Colors are based on human labels, but these weren't given to the algorithm
% \end{frame}

\begin{frame}{Unsupervised learning examples}
  \begin{itemize}
    \begin{small}
    \item In {\bf generative modeling}, we want to learn a distribution over some dataset, such as natural images.
    \item We can evaluate a generative model by sampling from the model and seeing if it looks like the data.
    \item These results were considered impressive in 2014:
      \begin{center}
        \includegraphics[width=0.95 \textwidth]{figures/lapgan2.png}
      \end{center}
    \end{small}
    \begin{flushright}
      \begin{tiny}
        Denton et al., 2014, Deep generative image models using a Laplacian pyramid of adversarial networks
      \end{tiny}
    \end{flushright}
  \end{itemize}
\end{frame}

\begin{frame}{Unsupervised learning examples}
  \begin{itemize}
  \item The progress of generative models:
    \begin{center}
      \includegraphics[width=0.7 \textwidth]{figures/gan_progress.jpg}
    \end{center}
  \item Big GAN, Brock et al, 2019:
    \begin{center}
      \includegraphics[width=0.7 \textwidth]{figures/biggan.png}
    \end{center}
  \end{itemize}
\end{frame}

\begin{frame}{Unsupervised learning examples}
  \begin{itemize}
  \item Generative models of text. The models like BERT and GPT-2 perform unsupervised learning by reconstructing the next words in a sentence. The GPT-2 models learns from 40GB of Internet text.
    \begin{center}
      \includegraphics[width=0.45 \textwidth]{figures/gpt_bert.png}
      \includegraphics[width=0.45 \textwidth]{figures/csc413_gpt2.png}
    \end{center}
    \begin{flushright}
      \begin{tiny}
        \url{https://talktotransformer.com/}
      \end{tiny}
    \end{flushright}
  \end{itemize}
\end{frame}



\begin{frame}{Unsupervised learning examples}
  \begin{itemize}
  \item Recent exciting result: a model called the CycleGAN takes lots of images of one category (e.g.~horses) and lots of images of another category (e.g.~zebras) and learns to translate between them.
    \begin{center}
      \includegraphics[width=.88\textwidth]{figures/cyclegan.png}
    \end{center}
    \begin{flushright}
      \begin{tiny}
        \vspace{-1em}
        \url{https://github.com/junyanz/CycleGAN}
      \end{tiny}
    \end{flushright}
  %\item You will implement this model for Programming Assignment 4.
  \end{itemize}
\end{frame}



% \begin{frame}{Unsupervised learning examples}

%   Automatic mouse tracking
%   \begin{itemize}
% 		\item When biologists do behavioral genetics researchers on mice, it's very time consuming for a person to sit and label everything a mouse does
% 		\item The Datta lab at Harvard is building a system for automatically tracking mouse behaviors
% 		\item Goal: show the researchers a summary of how much time different mice spend on various behaviors, so they can determine the effects of the genetic manipulations
% 		\item One of the major challenges is that we don't know the right ``vocabulary'' for describing the behaviors --- clustering the observations into meaningful groups is an unsupervised learning task
% 		\item {\bf video:} \url{http://www.sciencedirect.com/science/article/pii/S0896627315010375}
%   \end{itemize}

% \end{frame}



\begin{frame}{Reinforcement learning}
  \begin{center}
    \includegraphics[width=0.3\textwidth]{figures/breakout2600.png}
  \end{center}
  \begin{itemize}
  \item An {\bf agent} interacts with an {\bf environment} (e.g.~game of Breakout)
  \item In each time step, 
    \begin{itemize}
    \item the agent receives {\bf observations} (e.g.~pixels) which give it information about the {\bf state}  (e.g.~positions of the ball and paddle)
    \item the agent picks an {\bf action} (e.g.~keystrokes) which affects the state
    \end{itemize}
  \item The agent periodically receives a {\bf reward} (e.g.~points)
  \item The agent wants to learn a {\bf policy}, or mapping from observations to actions, which maximizes its average reward over time
  \end{itemize}
  
\end{frame}

\begin{frame}{Reinforcement learning}
  DeepMind trained neural networks to play many different Atari games
  \begin{itemize}
  \item given the raw screen as input, plus the score as a reward
  \item single network architecture shared between all the games
  \item in many cases, the networks learned to play better than humans (in terms of points in the first minute)
  \end{itemize}
  \url{https://www.youtube.com/watch?v=V1eYniJ0Rnk}
\end{frame}

\begin{frame}{Reinforcement learning for control}
  Learning locomotion control from scratch
  \begin{itemize}
  \item The reward is to run as far as possible over all the obstacles
  \item single control policy that learns to adapt to different terrains
  \end{itemize}
  \url{https://www.youtube.com/watch?v=hx_bgoTF7bs}
\end{frame}




\begin{frame}{Software frameworks}
  \begin{itemize}
  \item Scientific computing (NumPy)
    \begin{itemize}
    \item {\bf vectorize} computations (express them in terms of matrix/vector operations) to exploit hardware efficiency
    \end{itemize}
  \item Neural net frameworks: PyTorch, TensorFlow, etc.
    \begin{itemize}
    \item automatic differentiation
    \item compiling computation graphs
    \item libraries of algorithms and network primitives
    \item support for graphics processing units (GPUs)
    \end{itemize}
  \item For this course:
    \begin{itemize}
    \item Python, NumPy
    \item {\bf PyTorch}, a widely used neural net framework with a built-in automatic differentiation feature
    \end{itemize}
  \end{itemize}
\end{frame}


\begin{frame}{Software frameworks}
  Why take this class, if PyTorch does so much for you?
  
  \vspace{1em}
  So you know what do to if something goes wrong!
  \begin{itemize}
  \item Debugging learning algorithms requires sophisticated detective work, which requires understanding what goes on beneath the hood.
  \item That's why we derive things by hand in this class!
  \end{itemize}
\end{frame}

% \begin{frame}{After break}
%   \begin{center}
%   Linear models
%   \end{center}
% \end{frame}
 
\begin{frame}{Linear Models: Overview}
  \begin{itemize}
  \item One of the fundamental building blocks in deep learning are the \alert{linear models}, where you decide based on a linear function of the input vector.
  \item Here, we will review linear models, some other fundamental concepts (e.g.~gradient descent, generalization), and some of the common supervised learning problems:
    \begin{itemize}
    \item \alert{Regression}: predict a scalar-valued target (e.g.~stock price)
    \item \alert{Binary classification}: predict a binary label (e.g.~spam vs.~non-spam email)
    \item \alert{Multiway classification}: predict a discrete label (e.g.~object category, from a list)
    \end{itemize}
  \end{itemize}
\end{frame}


\begin{frame}{Problem Setup}
  \begin{center}
    \includegraphics[width=0.5 \textwidth]{figures/data.png}
  \end{center}
  \begin{itemize}
  \item Want to predict a scalar $\target$ as a function of a vector $\inputVec$
  \item Given a dataset of pairs $\{ (\inputVecI{\dataIdx}, \targetI{\dataIdx}) \}_{\dataIdx=1}^\ndata$
  \item The $\inputVecI{\dataIdx}$ are called \alert{input vectors}, and the $\targetI{\dataIdx}$ are called \alert{targets}.% \TODO{find a figure with 2-D inputs}
  \end{itemize}
\end{frame}

\begin{frame}{Problem Setup}
  \begin{center}
    \includegraphics[width=0.8 \textwidth]{figures/data_space_weight_space.png}
  \end{center}
  \begin{footnotesize}
    \begin{itemize}
    \item {\bf Model:} $\prediction$ is a linear function of $\inputUni$:
      \[ \prediction = \weights^\transpose \inputVec + \bias \]
    \item $\prediction$ is the \alert{prediction}
    \item $\weights$ is the \alert{weight vector}
    \item $\bias$ is the \alert{bias}
    \item $\weights$ and $\bias$ together are the \alert{parameters}
    \item Settings of the parameters are called \alert{hypotheses}
    \end{itemize}
  \end{footnotesize}
\end{frame}

\begin{frame}{Problem Setup}
  \begin{itemize}
  \item {\bf Loss function:} squared error
    \[ \loss(\prediction, \target) = \frac{1}{2} (\prediction - \target)^2 \]
  \item $\prediction - \target$ is the \alert{residual}, and we want to make this small in magnitude
  \item The $\frac{1}{2}$ factor is just to make the calculations convenient.
    
    \pause
  \item {\bf Cost function:} loss function averaged over all training examples
    \begin{small}
      \begin{align*}
        \cost(\weightUni, \bias) &= \frac{1}{2 \ndata} \sum_{\dataIdx=1}^\ndata \left( \predictionI{\dataIdx} - \targetI{\dataIdx} \right)^2 \\
                                 &= \frac{1}{2 \ndata} \sum_{\dataIdx=1}^\ndata \left( \weights^\transpose \inputVecI{\dataIdx} + \bias - \targetI{\dataIdx} \right)^2 
      \end{align*}
    \end{small}
  \end{itemize}
\end{frame}

\begin{frame}{Problem Setup}
  Visualizing the contours of the cost function:
  \begin{center}
    \includegraphics[width=0.45 \textwidth]{figures/residuals.png}
    \includegraphics[width=0.45 \textwidth]{figures/contours.png}
  \end{center}
\end{frame}

\begin{frame}{Vectorization}
  \begin{itemize}
  \item We can organize all the training examples into a matrix $\inputMatrix$ with one row per training example, and all the targets into a vector $\targets$.
    \begin{center}
      \includegraphics[width=0.8 \textwidth]{figures/X_mat.png}
    \end{center}  
  \item Computing the predictions for the whole dataset:
    \[ \inputMatrix \weights + \bias \onesVec = \begin{pmatrix} \weights^\transpose \inputVec^{(1)} + b \\ \vdots \\ \weights^\transpose \inputVec^{(\ndata)} + b \end{pmatrix} = \begin{pmatrix} y^{(1)} \\ \vdots \\ y^{(\ndata)} \end{pmatrix} = \predictions \]
  \end{itemize}
\end{frame}

\begin{frame}{Vectorization}
  \begin{itemize}
  \item Computing the squared error cost across the whole dataset:
    \begin{align*}
      \predictions &= \inputMatrix \weights + \bias \onesVec \\
      \cost &= \frac{1}{2 \ndata} \|\predictions - \targets \|^2
    \end{align*}
  \item In Python:
    \begin{center}
      \includegraphics[width=0.5 \textwidth]{figures/cost_vec.png}
    \end{center}
  \end{itemize}
\end{frame}

\begin{frame}{Solving the optimization problem}
  \begin{itemize}
  \item We defined a cost function. This is what we'd like to minimize.
  \item Recall from calculus class: the minimum of a smooth function (if it exists) occurs at a \alert{critical point}, i.e.~point where the partial derivatives are all 0.
  \item Two strategies for optimization:
    \begin{itemize}
    \item \alert{Direct solution}: derive a formula that sets the partial derivatives to 0. This works only in a handful of cases (e.g.~linear regression).
    \item \alert{Iterative methods} (e.g. gradient descent): repeatedly apply an update rule which slightly improves the current solution. This is what we'll do throughout the course.
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Direct solution}
  \begin{itemize}
  \item \alert{Partial derivatives}: derivatives of a multivariate function with respect to one of its arguments.
    \[ \frac{\partial}{\partial x_1} f(x_1, x_2) = \lim_{h \to 0} \frac{f(x_1 + h, x_2) - f(x_1, x_2)}{h} \]
  \item To compute, take the single variable derivatives, pretending the other arguments are constant.
  \item Example: partial derivatives of the prediction $\prediction$
    \begin{scriptsize}
      \begin{align*}
        \frac{\partial \prediction}{\partial \weightJ{\featIdx}} &= \frac{\partial}{\partial \weightJ{\featIdx}} \left[ \sum_{\featIdxTwo} \weightJ{\featIdxTwo} \inputJ{\featIdxTwo} + \bias \right] \\
                                                                 &= \inputJ{\featIdx} \\
        \frac{\partial \prediction}{\partial \bias} &= \frac{\partial}{\partial \bias} \left[ \sum_{\featIdxTwo} \weightJ{\featIdxTwo} \inputJ{\featIdxTwo} + \bias \right] \\
                                                                 &= 1
      \end{align*}
    \end{scriptsize}
  \end{itemize}
\end{frame}

\begin{frame}{Direct solution}
  \begin{itemize}
  \item \alert{Chain rule for derivatives}:
    \begin{scriptsize}
      \begin{align*}
        \frac{\partial \loss}{\partial \weightJ{\featIdx}} &= \frac{\deriv \loss}{\deriv \prediction} \frac{\partial \prediction}{\partial \weightJ{\featIdx}} \\
                                                           &= \frac{\deriv}{\deriv \prediction} \left[ \frac{1}{2} (\prediction - \target)^2 \right] \cdot \inputJ{\featIdx} \\
                                                           &= (\prediction - \target) \inputJ{\featIdx} \\
        \frac{\partial \loss}{\partial \bias} &= \prediction - \target
      \end{align*}
    \end{scriptsize}
  \item We will give a more precise statement of the Chain Rule next week. It's actually pretty complicated.
  \item Cost derivatives (average over data points):
    \begin{scriptsize}
      \begin{align*}
        \frac{\partial \cost}{\partial \weightJ{\featIdx}} &= \frac{1}{\ndata} \sum_{\dataIdx=1}^\ndata (\predictionI{\dataIdx} - \targetI{\dataIdx}) \, \inputIJ{\dataIdx}{\featIdx} \\
        \frac{\partial \cost}{\partial \bias} &= \frac{1}{\ndata} \sum_{\dataIdx=1}^\ndata \predictionI{\dataIdx} - \targetI{\dataIdx} \\
      \end{align*}
    \end{scriptsize}
  \end{itemize}
\end{frame}

\begin{frame}{Gradient descent}
  \begin{itemize}
  \item \alert{Gradient descent} is an \alert{iterative algorithm}, which means we apply an update repeatedly until some criterion is met.
  \item We \alert{initialize} the weights to something reasonable (e.g.~all zeros) and repeatedly adjust them in the \alert{direction of steepest descent}.
  \item The gradient descent update decreases the cost function for small enough $\alpha$:
    \begin{align*}
      \weightJ{\featIdx} &\gets \weightJ{\featIdx} - \lrate \frac{\partial \cost}{\partial \weightJ{\featIdx}} \\
                         &= \weightJ{\featIdx} - \frac{\lrate}{\ndata} \sum_{\dataIdx=1}^\ndata (\predictionI{\dataIdx} - \targetI{\dataIdx}) \, \inputIJ{\dataIdx}{\featIdx}
    \end{align*}
  \item $\alpha$ is a \alert{learning rate}. The larger it is, the faster $\weights$ changes.
    \begin{itemize}
    \item We'll see later how to tune the learning rate, but values are typically small, e.g.~0.01 or 0.0001
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Gradient descent}
  \begin{itemize}
  \item This gets its name from the \alert{gradient}:
    \[ \nabla \cost(\weights) = \frac{\partial \cost}{\partial \weights} = \begin{pmatrix} \frac{\partial \cost}{\partial \weightJ{1}} \\ \vdots \\ \frac{\partial \cost}{\partial \weightJ{\nfeat}} \end{pmatrix} \]
    \begin{itemize}
    \item This is the direction of fastest increase in $\cost$.
    \end{itemize}
    \pause
  \item Update rule in vector form:
    \begin{align*}
      \weights &\gets \weights - \lrate \nabla \cost(\weights) \\
               &= \weights - \frac{\lrate}{\ndata} \sum_{\dataIdx=1}^\ndata (\predictionI{\dataIdx} - \targetI{\dataIdx}) \, \inputI{\dataIdx}
    \end{align*}
  \item Hence, gradient descent updates the weights in the direction of fastest \emph{decrease}.
  \end{itemize}
\end{frame}

\begin{frame}{Gradient descent}
	Visualization:
	
	\url{http://www.cs.toronto.edu/~guerzhoy/321/lec/W01/linear_regression.pdf\#page=21}
\end{frame}


\begin{frame}{Gradient descent}
  \begin{itemize}
  \item Why gradient descent, if we can find the optimum directly?
    \begin{itemize}
    \item GD can be applied to a much broader set of models
    \item GD can be easier to implement than direct solutions, especially with automatic differentiation software
    \item For regression in high-dimensional spaces, GD is more efficient than direct solution (matrix inversion is an ${\cal O}(\nfeat^3)$ algorithm).
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Feature maps}
  \begin{itemize}
  \item We can convert linear models into nonlinear models using feature maps.
    \[ \prediction = \weights^\transpose \featureVec(\inputVec) \]
  \item E.g., if $\featureMap(\inputUni) = (1, \ \inputUni, \ \cdots, \inputUni^\ndim)^\transpose$, then $\prediction$ is a polynomial in $\inputUni$. This model is known as \alert{polynomial regression}:
    \[ \prediction = \weightJ{0} + \weightJ{1} \inputUni + \cdots + \weightJ{\ndim} \inputUni^\ndim \]
  \item This doesn't require changing the algorithm --- just pretend $\featureMap(\inputUni)$ is the input vector.
  \item We don't need an expicit bias term, since it can be absorbed into $\featureMap$.
  \item Feature maps let us fit nonlinear models, but it can be hard to choose good features.
    \begin{itemize}
    \item Before deep learning, most of the effort in building a practical machine learning system was feature engineering.
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Feature maps}
  \begin{center}
    \begin{footnotesize}
    \begin{minipage}{0.45 \linewidth}
      \[
        y=w_0
      \]
      \centering
      \includegraphics[width=0.7\linewidth]{figures/polynomial_0.pdf}
    \end{minipage}
    \begin{minipage}{0.45 \linewidth}
      \[
        y=w_0 + w_1x
      \]
      \centering
      \includegraphics[width=0.7\linewidth]{figures/polynomial_1.pdf}
    \end{minipage}
    \\
    \begin{minipage}{0.45 \linewidth}
      \[
        y=w_0 + w_1x + w_2x^2 + w_3x^3
      \]
      \centering
      \includegraphics[width=0.7\linewidth]{figures/polynomial_2.pdf}
    \end{minipage}
    \begin{minipage}{0.45 \linewidth}
      \[
        y=w_0 + w_1x + \cdots + w_9x^9 
      \]
      \centering
      \includegraphics[width=0.7\linewidth]{figures/polynomial_3.pdf}
    \end{minipage}
    \end{footnotesize}
  \end{center}
  \begin{flushright}
    \tiny
    -Pattern Recognition and Machine Learning, Christopher Bishop.
  \end{flushright}
\end{frame}

\begin{frame}{Generalization}
\centering
  \alert{Underfitting} : The model is too simple - does not fit the data.

  \includegraphics[width=0.3\textwidth]{figures/polynomial_0.pdf}

\alert{Overfitting} : The model is too complex - fits perfectly, does not generalize.

\includegraphics[width=0.3\textwidth]{figures/polynomial_3.pdf}
\end{frame}



\begin{frame}{Generalization}
  \begin{itemize}
  \item We would like our models to \alert{generalize} to data they haven't seen before
  \item The degree of the polynomial is an example of a \alert{hyperparameter}, something we can't include in the training procedure itself
  \item We can tune hyperparameters using a \alert{validation set}: 
    \vspace{1em}
    \begin{center}
      \includegraphics[width=0.65 \textwidth]{figures/validation.png}
    \end{center}
    % partition the data into three subsets
    % \begin{itemize}
    % \item \alert{Training set}: used to train the model
    % \item \alert{Validation set}: used to evaluate generalization error of a trained model
    % \item \alert{Test set}: used to evaluate final performance \emph{once}, after hyperparameters are chosen
    % \end{itemize}
    % \item Tune the hyperparameters on the validation set, then report the performance on the test set.
  \end{itemize}
\end{frame}

\begin{frame}{Classification}

  {\bf Binary linear classification}
  \begin{itemize}
  \item {\bf classification:} predict a discrete-valued target
  \item {\bf binary:} predict a binary target $\target \in \{0, 1\}$
    \begin{itemize}
    \item Training examples with $\target=1$ are called \alert{positive examples}, and training examples with $\target=0$ are called \alert{negative examples}. Sorry.
    \end{itemize}
  \item {\bf linear:} model is a linear function of $\inputVec$, thresholded at zero:
    \begin{align*}
      \intermediate &= \weights^T \inputVec + \bias \\
      \textrm{output} &= \left\{ \begin{array}{ll} 1 & \textrm{ if } \intermediate \geq 0 \\ 0 & \textrm{ if } \intermediate < 0 \end{array} \right.
    \end{align*}
  \end{itemize}
\end{frame}

\begin{frame}{Logistic Regression}
  \begin{itemize}
  \item We can't optimize classification accuracy directly with gradient descent because it's discontinuous.
  \item Instead, we typically define a continuous \alert{surrogate loss function} which is easier to optimize. \alert{Logistic regression} is a canonical example of this, in the context of classification.
  \item The model outputs a continuous value $\prediction \in [0, 1]$, which you can think of as the probability of the example being positive.
  \end{itemize}
\end{frame}

\begin{frame}{Logistic Regression}
  \begin{itemize}
  \item There's obviously no reason to predict values outside [0, 1]. Let's squash $\prediction$ into this interval.
  \end{itemize}
  \begin{columns}
    \begin{column}{0.7 \linewidth}
      \begin{itemize}
      \item The \alert{logistic function} is a kind of \alert{sigmoidal}, or S-shaped, function:
        \[ \sigmoid(\intermediate) = \frac{1}{1 + e^{-\intermediate}} \]

      \end{itemize}
    \end{column}
    \begin{column}{0.3 \linewidth}
      \includegraphics[width=\linewidth]{figures/logistic_function.png}
    \end{column}
  \end{columns}
  \begin{itemize}
      \item A linear model with a logistic nonlinearity is known as \alert{log-linear}:
        \begin{align*}
          \intermediate &= \weights^\transpose \inputVec + \bias \\
          \prediction &= \sigmoid(\intermediate)
        \end{align*}
      \item Used in this way, $\sigmoid$ is called an \alert{activation function}, and $\intermediate$ is called the \alert{logit}.
  \end{itemize}
\end{frame}

\begin{frame}{Logistic Regression}
  \begin{itemize}
  \item Because $\prediction \in [0, 1]$, we can interpret it as the estimated probability that $\target = 1$.
  \item Being 99\% confident of the wrong answer is much worse than being 90\% confident of the wrong answer. \alert{Cross-entropy loss} captures this intuition:
    
    \begin{center}
      \begin{minipage}{0.3 \textwidth}
        \begin{small}
          \begin{align*}
            \crossEntropyLoss(\prediction, \target) &= \left\{ \begin{array}{ll} -\log \prediction & \textrm{if } \target = 1 \\ -\log (1 - \prediction) & \textrm{if } \target = 0 \end{array} \right. \\
                                                    &= -\target \log \prediction - (1 - \target) \log (1 - \prediction)
          \end{align*}
        \end{small}
      \end{minipage}
      \begin{minipage}{0.4 \textwidth}
        \includegraphics[width=\textwidth]{figures/cross_entropy.png}
      \end{minipage}
    \end{center}
  \item Aside: why does it make sense to think of $\prediction$ as a probability?  Because cross-entropy loss is a \alert{proper scoring rule}, which means the optimal $\prediction$ is the true probability.
  \end{itemize}
\end{frame}

\begin{frame}{Logistic Regression}
  \begin{itemize}
  \item \alert{Logistic regression} combines the logistic activation function with cross-entropy loss.
  \end{itemize}
  
  \begin{minipage}{0.3 \textwidth}
    \begin{align*}
      \intermediate &= \weights^\transpose \inputVec + \bias \\
      \prediction &= \sigmoid(\intermediate) \\
                    &= \frac{1}{1+e^{-z}} \\
      \crossEntropyLoss &= -\target \log \prediction - (1 - \target) \log (1 - \prediction)
    \end{align*}
  \end{minipage}
  \begin{minipage}{0.5 \textwidth}
    \includegraphics[width=\textwidth]{figures/logistic_cross_entropy.png}
  \end{minipage}

  \begin{itemize}
  \item Interestingly, the loss asymptotes to a linear function of the logit $\intermediate$.
  \item Full derivation in the readings.
  \end{itemize}
\end{frame}



\begin{frame}{Multiclass Classification}
	\begin{itemize}
		\item What about classification tasks with more than two categories?
	\end{itemize}
	\begin{center}
	\includegraphics[width=0.3 \textwidth]{figures/what_makes_2.pdf}
	\hspace{0.1 \textwidth}
	\includegraphics[width=0.4 \textwidth]{figures/object_recognition.pdf}
	\end{center}
\end{frame}

\begin{frame}{Multiclass Classification}
	\begin{itemize}
		\item Targets form a discrete set $\{1, \ldots, \numClasses \}$.
		\item It's often more convenient to represent them as \alert{one-hot vectors}, or a \alert{one-of-K encoding}:
		\[ \targets = \underbrace{ \left( 0, \ldots, 0, 1, 0, \ldots, 0 \right)}_{\textrm{entry $k$ is 1}} \]
	\end{itemize}
\end{frame}

\begin{frame}{Multiclass Classification}
	\begin{itemize}
		\item Now there are $\nfeat$ input dimensions and $\numClasses$ output dimensions, so we need $\numClasses \times \nfeat$ weights, which we arrange as a \alert{weight matrix} $\weightMat$. 
		\item Also, we have a $\numClasses$-dimensional vector $\biases$ of biases.
		\item Linear predictions:
		\[ \intermediateK{\classIdx} = \sum_\featIdx \weightKJ{\classIdx}{\featIdx} \inputJ{\featIdx} + \biasK{\classIdx} \]
		\item Vectorized:
		\[ \intermediates = \weightMat \inputVec + \biases \]
	\end{itemize}
	\begin{center}
	\includegraphics[width=0.4 \textwidth]{figures/multiway_network.png}
	\end{center}
\end{frame}

\begin{frame}{Multiclass Classification}
	\begin{itemize}
		\item A natural activation function to use is the \alert{softmax function}, a multivariable generalization of the logistic function:
		%\[ \sigmoid(\intermediateK{1}, \ldots, \intermediateK{\numClasses}) = \frac{1}{e^{\intermediateK{1}} + \cdots + e^{\intermediateK{\numClasses}}} \begin{pmatrix} e^{\intermediateK{1}} \\ \vdots \\ e^{\intermediateK{\numClasses}} \end{pmatrix} \]
		\[ \predictionK{\classIdx} = \softmax(\intermediateK{1}, \ldots, \intermediateK{\numClasses})_{\classIdx} = \frac{e^{\intermediateK{\classIdx}}}{\sum_{\classIdx^\prime} e^{\intermediateK{\classIdx^\prime}}} \]
                \item The inputs $\intermediateK{\classIdx}$ are called the \alert{logits}.
		\item Properties:
		\begin{itemize}
			\item Outputs are positive and sum to 1 (so they can be interpreted as probabilities)
			\item If one of the $\intermediateK{\classIdx}$'s is much larger than the others, $\softmax(\intermediates)$ is approximately the argmax. (So really it's more like ``soft-argmax''.)
			\item {\bf Exercise:} how does the case of $\numClasses=2$ relate to the logistic function?
		\end{itemize}
		\item Note: sometimes $\sigmoid(\intermediates)$ is used to denote the softmax function; in this class, it will denote the logistic function applied elementwise.
	\end{itemize}
\end{frame}

\begin{frame}{Multiclass Classification}
  \begin{itemize}
  \item If a model outputs a vector of class probabilities, we can use cross-entropy as the loss function:
    \begin{align*}
      \crossEntropyLoss(\predictions, \targets) &= -\sum_{\classIdx=1}^\numClasses \targetK{\classIdx} \log \predictionK{\classIdx} \\
                                                &= -\targets^\transpose (\log \predictions),
    \end{align*}
    where the $\log$ is applied elementwise.
  \item Just like with logistic regression, we typically combine the softmax and cross-entropy into a \alert{softmax-cross-entropy} function.
  \end{itemize}
\end{frame}

\begin{frame}{Multiclass Classification}
	\begin{itemize}
		\item \alert{Softmax regression}, also called \alert{multiclass logistic regression}:
		\begin{align*}
		\intermediates &= \weightMat \inputVec + \biases \\
		\predictions &= \softmax(\intermediates) \\
		\crossEntropyLoss &= -\targets^\transpose (\log \predictions)
		\end{align*}
		\item It's possible to show the gradient descent updates have a convenient form:
                  \[ \frac{\partial \crossEntropyLoss}{\partial \intermediates} = \predictions - \targets \]
	\end{itemize}
\end{frame}




\end{document}



