Part 3 - AI Tools

Introduction

When dealing with the machine learning life cycle, we will encounter,

  1. Data collection (Collect user ratings).
  2. Data representation & Processing (Preprocess files, movie_reviews.csv).
  3. Modelling (Collaborative filtering, Content-based filtering).
  4. Learning/Optimization (Minimizing empirical risk of predicting ratings).
  5. Evaluation (Online A/B testing 1).
  6. Deployment (git add . && git commit -m "LGTM" && git push origin master -f).

At each of these steps we use different tools, but first let’s define what an AI tool is.

What is an AI tool?

When we are talking about AI tools, we are not talking about AI-assisted tools, rather, tools that helps us implement AI systems.

The specifics depend (obviously) on the nature of the system.

We will focus on (statistical) machine learning for prototyping in Python.

Data Representation & Processing

In typical machine learning systems, our data is stored in matrix and or vector form, e.g., a classical table of features XX and labels YY.

However, more general data usually does not have natural tabular representation. Think of time series, they must cover different time-scales, different lengths and different possible events.

Image data is also not easily stored in (2D) matrices, instead we work with tensors. We usually just think of them as 3D (or higher) matrices, where each depth dimension represents a different color channel.

Graphs are used to represent relations of people, atoms, cities, etc. These can be represented as adjacency matrices.

However (again), despite the different nature of text, images, etc., we can still (usually) store them in matrix and vector form.

But why matrices?

Because (almost) all of machine learning relies on linear algebra,

  1. Linear Regression y=βTx+cy = \beta^T x + c.
  2. Deep neural networks hl+1=σ(Wl+1hl+bl)h_{l + 1} = \sigma(W_{l + 1} h_l + b_{l}).
  3. Linear Programming minAx+bsubject to Bx+c0\begin{aligned} \min & \quad Ax + b \newline \text{subject to } & \quad Bx + c \leq 0 \end{aligned}.

Matrix and vector operations are fast (enough) and well-studied.

Drawbacks & Data Frames

A major drawback of pure matrix and tensor representations is that columns and rows are anonymous. Sometimes we want to know what each row or column represents (explicitly).

Data frames add indices and names, much like a spreadsheet.

Missing Values

Say that we want to predict cardiovascular risk for patients who visit their general practitioner.

We look at electronic health records (EHR) data containing age, sex, weight and cholesterol levels XX and the corresponding cardiovascular risk YY.

When training our machine learning model, inputs are often assumed to be fully observed, but in reality, it is very common for some features to be missing.

Let X~\tilde{X} be the actual observed values (which is a subset of the full feature XX).

We can represent missing values by a missingness mask MM.

Thus, we can do,

Xij~={Xijif Mij=0nanotherwise\tilde{X_{ij}} = \begin{cases} X_{ij} & \text{if } M_{ij} = 0 \newline \texttt{nan} & \text{otherwise} \end{cases}

Now, suppose we want to learn to predict YY from our tabular features XX, what can we do with our missing values in XX?

There are two common solutions imputation and informative missingness.

Imputation

Attempt to impute missing values,

  1. Reconstruct XX from X~\tilde{X}.
  2. Predict YY from reconstructed X^\hat{X}.

Any method that works (well) for XX works (well) for our reconstructed X^\hat{X}.

Informative Missingness

Make use of missingness itself,

  1. Predict YY from both X~\tilde{X} and MM.

From here we can do two more common approaches,

  • Method sensitive to MM (e.g., XGBoost 2).
  • Simple imputation + missing indicators.

Single VS. Multiple Imputation

The basic idea of imputation is to predict the missing value from observed values of other variables. In the simplest case, we use a single imputation of each value,

f(Age,Sex,Weight)=Cholesterolf(\text{Age}, \text{Sex}, \text{Weight}) = \text{Cholesterol}

The imputation functions can be learned by regression on complete observations or observations with less missingness,

f=argminfE[(f(Age,Sex,Weight)Cholesterol)2]f^{\star} = \arg \min_{f} \mathbb{E} \left[ \left( f(\text{Age}, \text{Sex}, \text{Weight}) - \text{Cholesterol} \right)^2 \right]

This becomes tricky when all observations have some missing values.

A more popular method for imputation is Multiple Imputation by Chained Equations, or MICE.

Multiple Imputation: Create more than one sample of each missing value to account for variance.

Chained Equations: Impute one value based on imputations of other variables, works without any complete observations.

  1. Give each variable a placeholder imputed value (e.g., mean)
  2. Repeat for a number of iterations
  3. \quad For each variable vv:
  4. \quad \quad Regress observed vv on other variables in the dataset (including other imputed values)
  5. \quad \quad Impute missing vv using regression.

However, imputation methods may fail when data is not missing at random (MAR), i.e., missing values can not be reliably imputed.

A common method in this case is to stitch a simple imputation X^\hat{X} (e.g., 0) and MM, as binary indicators, together for prediction.

When do we use which method?

  1. Will data be missing at test/use time?
    • → Imputation is not necessary to minimize test error.
  2. Are you fitting a parametric model (e.g., linear regression)?
    • → Imputation becomes important to recover parameters.
  3. Is missingness predictable from observed values (MAR)?
    • → If not, imputation will lead to biased results.

Model Development

A big development pattern in machine learning systems is the fit, predict, score pattern.

This standardizes the common machine learning workflow,

  1. fit(x, y): Train model to e.g., predict yy from xx.
  2. predict(x): Predict yy for xx.
  3. score(x, y): Evaluate model on data x,yx, y.

fit(x, y)

The function fit(x, y) is responsible for training and storing model parameters that maximize (or minimize) some objective function.

For example, finding the optimal coefficients in OLS or k-NN.

Our arguments x,yx,y may vary depending on our application, in unsupervised learning there is no yy, but parameters to fit.

predict(x)

The function predict(x) should take a (new) data point xx and predict the corresponding outcome (e.g., label/cluster) yy for it.

score(x, y)

The function score(x, y) assigns a score to the prediction made for xx in comparison to the label yy.

score(x, y) can also be used for hyperparameter selection, for example using cross-validation.

Data Preprocessing

All algorithms are sensitive to the representation of the input data.

A (trivial) example are the coefficients of OLS, which depend on the scale of the covariates,

y=βT(ax)+c=(aβ)Tx+c.y = \beta^T(ax) + c = (a \beta)^T x + c.

A common step is to standardize features, i.e., to make them have zero mean and unit variance,

X(i)Xμ(X)σ(X).X^{(i)} \leftarrow \frac{X - \mu(X)}{\sigma(X)}.

Formally, μ\mu and σ\sigma are parameters, they are functions of our training data.

Differentiable Systems

When we fit a machine learning algorithm on a training dataset, we are solving an optimization problem.

Empirical risk minimization (ERM) is the most common optimization problem in machine learning.

Definition 1 (Empirical Risk Minimization)

Given a loss function LL, a model fθf_{\theta} with parameters θ\theta, and training data {(x(i),y(i))}i=1N\{(x^{(i)}, y^{(i)})\}_{i=1}^{N}, empirical risk minimization is the problem,

minθ 1Ni=1NL(fθ(x(i)),y(i)).\underset{\theta}{\min} \ \frac{1}{N} \sum_{i = 1}^{N} L(f_{\theta}(x^{(i)}), y^{(i)}).

Deep learning and many other AI tools are based on ERM in differentiable systems. Systems where the objective function is differentiable in the parameters θ\theta (this means that learning using gradient descent is possible).

Gradient Descent

Consider the linear model f(x)=θTxf(x) = \theta^T x of a 1D label y.

We can measure our error using the mean squared error (MSE),

R^(θ)=1Ni=1N(θTx(i)y(i))2.\hat{R}(\theta) = \frac{1}{N} \sum_{i = 1}^{N} (\theta^T x^{(i)} - y^{(i)})^2.

How do we find the parameters θ\theta^{\star} that minimize R^(θ)\hat{R}(\theta)?

We can use gradient descent to find a (local) minimum.

Definition 2 (Gradient Descent)

Gradient descent iteratively moves in the direction of R^(θ)-\nabla \hat{R}(\theta),

θkθk1ηθR^(θk1),\theta_k \leftarrow \theta_{k - 1} - \eta \nabla_{\theta} \hat{R}(\theta_{k - 1}),

with step size η>0\eta > 0.

Here the gradient is,

θR^(θ)=2Ni=1Nx(i)(θTx(i)y(i)).\nabla_{\theta} \hat{R}(\theta) = \frac{2}{N} \sum_{i = 1}^{N} x^{(i)} (\theta^T x^{(i)} - y^{(i)}).

However, gradient descent only has one non-trivial operation, computing the gradient itself.

As long as R^\hat{R} is a composition of differentiable functions of θ\theta, differentiation is easy, this is what we exploit in modern machine learning (backpropagation).

Footnotes

  1. A/B testing

  2. XGBoost