The pysgd
package is structured as a general gradient descent algorithm that accepts data, an objective function, a gradient descent adaptation and hyperparameters as its arguments. Below is the file structure of the package:
pysgd/
|--__init__.py
|--adaptations/
| |--__init__.py
| |--adagrad.py
| |--adam.py
| |--constant.py
|--objectives/
| |--__init__.py
| |--linear.py
| |--logistic.py
| |--stab-tang.py
|--tests/
The intention of this package is to present reasonbly efficient, working algorithms that are easy to understand.
The package is structured to make it easy to add additional objective functions and gradient descent adaptations, by following the basic form of the existing ones and adding them into their respective folders.
Gradient descent is a method for minimizing an objective function. In machine learning applications the objective function to be minimized is the error, $J$, (or cost) of a predictive model. A predictive model consists of a parameters, $\theta$, that are applied to inputs, $X$, (also called training samples, features, observations or independent variables) in order to estimate an output, $\hat{y}$ (also called a label or dependent variable). Gradient descent attempts to determine the parameters that when applied to a set of inputs result in the lowest total error (the difference between the actual outcome and the one predicted by the model). Below is the basic predictive formula.
$$H(X,\theta)=\hat{y}$$Below is an illustrative formula for determining the cost of a model.
$$J(\theta) = \sum_{i=1}^m\mid{h_i(\theta,x_i) - y_i}\mid$$There are different formulas for computing cost depending on the application, but the formula above expresses the essence of predicting actual outcomes as closely as possible.
In order to minimze $J$ with respect to $\theta$, the algorithm starts with an abitrary value of $\theta$, determines the "direction" that would result in the fastest decrease in cost (called the gradient), updates $\theta$ in that direction by a small amount (called the learning rate or $\alpha$) and then repeats until cost $J$ has been minimized.
$$\theta_j := \theta_j - \alpha\triangledown\theta_jJ(\theta)$$or
$$\theta_j := \theta_j - \alpha\frac\partial{\partial\theta_j}J(\theta)$$The package has one main function, sgd
, that returns a $j$ x $(n + 2)$ array, where $j$ is the number of iterations and $n$ is the number of features in the data. $\theta_j$ is in the first $n + 1$ columns and the cost $J_j$ in the last column.
Argument | Definition |
---|---|
theta0 |
Starting value of $\theta$ ($\theta_0$) in the form of an $1$ x$(n + 1)$ array. |
obj='stab_tang' |
Objective function to be minimized in the form of a string with a value of stab_tang , linear or logistic . stab_tang is for the Stablinsky-Tang function, included for testing and illustrative purposes. |
adapt='constant' |
Gradient descent adaptation in the form of a string with a value of constant , adagrad or adam .
|
data=np.array([]) |
Data in the form of an $m$ x $(n+1)$ array, including ones in the first column, if necessary, where $m$ is the number of training observations. |
size=50 |
Batch size in the form of an integer between $1$ and $m$. Batches are generated contiguously over the data. Data is shuffled between cycles. |
alpha=.01 |
Learning rate $\alpha$ in the form of a floating point integer. |
epsilon=10**-8 |
Hyperparameter used by adagrad and adam for smoothing. |
beta1=0.9 |
Hyperparamter used by adam that controls the decay rates of the moving gradient averages. |
beta2=0.999 |
Hyperparamter used by adam that controls the decay rates of the moving gradient averages. |
delta_min=10**-6 |
Maximum change in $\theta_n$ to establish convergence in the form of a floating point integer. |
iters=1000 |
Maximum number of batches evaluated unless convergence is achieved in fewer iterations. |
Tests are in the tests
folder using pytest, with 100% coverage.
In addition to sample data sets, we also use the Stablinsky-Tang function for testing, which is non-convex, suitable for testing, with straightforward gradient computation. This allows us to compare the value of $\theta$ produced by each algorithm and its associated $J$ with values we can calculate directly. By using a known function with two dimensional inputs we can plot $J$ as a surface for a given range of $\theta$ values and then $J_\theta$ for each iteration of the algorithm to visualize the progression of the algorithms.
The Styblinski–Tang function with respect to $\theta$ is:
$$J(\theta) = \dfrac{\sum_{i=1}^n\theta_i^4-16\theta_i^2+5\theta_i}{n}$$where $n$ is the number of dimensions in the data. For two dimensions, we can also express our cost function as:
$$J(\theta) = \dfrac{\theta_1^4-16\theta_1^2+5\theta_1+\theta_2^4-16\theta_2^2+5\theta_2}{2}$$The global minimum of this function is $-78.33233$ at $\theta = (-2.903534, -2.903534)$
The Styblinski–Tang gradient function is:
$$\frac\partial{\partial\theta_n}J(\theta) = 2\theta_n^3-16\theta_n+2.5$$The color scale of the surface plots corresponds to the z-axis value, which represents cost $J$ for all values of $\theta$ in the displayed range. The color scale of the points on the surface, which represent the cost $J_{\theta_j}$ as a function of $\theta_j$ at each iteration of the model, corresponds to the iteration.
import pysgd
import inspect
# Define function to display code.
def disp_mod(mod):
code, line_no = inspect.getsourcelines(mod)
from IPython.display import Markdown, display
def printmd(string):
display(Markdown('``` python\n' + string + '```'))
printmd(''.join(code))
disp_mod(pysgd)
import numpy as np
import importlib
from pysgd.objectives import Objective
# Define general gradient descent algorithm
def sgd(
theta0,
obj='stab_tang',
adapt='constant',
data=np.array([]),
size=50,
alpha=.01,
epsilon=10**-8,
beta1=0.9,
beta2=0.999,
delta_min=10**-6,
iters=1000):
# Initialize gradient adaptation parameters
params = dict(
alpha=alpha,
epsilon=epsilon,
beta1=beta1,
beta2=beta2
)
# Initialize cost and gradient functions
obj_fun = Objective(obj, data, size)
# Initialize gradient adaptation.
grad_adapt = importlib.import_module('pysgd.adaptations.' + adapt).adapt
# Initialize theta and cost history for convergence testing and plot
theta_hist = np.zeros((iters, theta0.shape[0]+1))
theta_hist[0] = obj_fun.cost(theta0)
# Initialize theta generator
theta_gen = grad_adapt(params, obj_fun.grad)(theta0)
# Initialize iteration variables
delta = float("inf")
i = 1
# Run algorithm
while delta > delta_min:
# Get next theta
theta = next(theta_gen)
# Store cost for plotting, test for convergence
try:
theta_hist[i] = obj_fun.cost(theta)
except:
print('{} minimum change in theta not achieved in {} iterations.'
.format(delta_min, theta_hist.shape[0]))
break
delta = np.max(np.square(theta - theta_hist[i-1,:-1]))**0.5
i += 1
# Trim zeros and return
theta_hist = theta_hist[:i]
return theta_hist
The Objective
class in sgd/objectives/__init__.py
is where the objective functions, data and gradient adaptations are handled.
disp_mod(pysgd.objectives)
import importlib
import numpy as np
from os.path import dirname, basename, isfile
import glob
modules = glob.glob(dirname(__file__)+"/*.py")
__all__ = [ basename(f)[:-3] for f in modules if isfile(f)]
class Objective(object):
def __init__(self, obj, data, size):
obj = importlib.import_module('pysgd.objectives.' + obj)
def batches_gen(data=data, size=size):
i = 0
while True:
index = slice(i*size, min((i+1)*size, data.shape[0]), 1)
if data.shape[0] - i * size > 0:
yield (data[index,:-1], data[index,-1])
i += 1
else:
np.random.shuffle(data)
i = 0
self.batches = batches_gen()
def grad_from_data(theta):
return obj.grad_fun(theta, next(self.batches))
def cost_from_data(theta):
return obj.cost_fun(theta, data)
if data.size > 1:
self.grad = grad_from_data
self.cost = cost_from_data
else:
self.grad = obj.grad_fun
self.cost = obj.cost_fun
Run gradient descent algorithm.
import numpy as np
theta_hist = pysgd.sgd(theta0=np.array([-0.2, -4.4]))
Plot $J_j$ for each $\theta_j$.
import plotly.offline as py
py.init_notebook_mode()
import plotly.graph_objs as go
# Prepare plot
x = np.arange(-4.6, 4.6, 0.1)
y = np.arange(-4.6, 4.6, 0.1)
X, Y = np.meshgrid(x, y)
Z = 1/2.0 * (X**4 - 16*X**2 + 5*X + Y**4 - 16*Y**2 + 5*Y)
# Prepare surface contours
contour = dict(
show = True,
color = 'DodgerBlue', #'#0066FF',
highlightcolor = 'DeepSkyBlue',
highlightwidth = 1.5,
width = 1
)
# Add surface to plot
surface = go.Surface(
name = 'J surface',
x = X,
y = Y,
z = Z,
colorscale = 'Rainbow',
showlegend = False,
contours = dict(
y = contour,
x = contour,
z = dict(
show = False,
color = contour['color'],
highlightcolor = contour['highlightcolor'],
highlightwidth = contour['highlightwidth'],
width = contour['width']
)
)
)
# Add theta_hist to plot - set up as a function for future plots
def spec_theta_hist_trace(theta_hist):
theta_hist_trace = go.Scatter3d(
name = 'theta_hist',
x = theta_hist[:,0],
y = theta_hist[:,1],
z = theta_hist[:,2],
mode = 'markers',
showlegend = False,
marker = dict(
color = np.arange(theta_hist.shape[0]),
colorscale = 'Blackbody',
showscale = False,
size = "5"
)
)
return theta_hist_trace
# Specify layout options
layout = go.Layout(
title='Constant Alpha',
autosize=False,
width=700,
height=700,
scene=dict(
xaxis=dict(
title = 'theta1',
ticks = "outside",
dtick = 0.25,
showticklabels = False
),
yaxis=dict(
title = 'theta2',
ticks = "",
dtick = 0.25,
showticklabels = False
),
zaxis=dict(
title = 'J',
),
camera=dict(
up=dict(x=0, y=0, z=1),
center=dict(x=0, y=0, z=0),
eye=dict(x=0.25, y=1.25, z=1.15)
)
)
)
# Execute plot
fig = go.Figure(data=[surface, spec_theta_hist_trace(theta_hist)], layout=layout)
py.iplot(fig, filename='constant_alpha_gradient_descent')
The core gradient descent algorithm functions as expected. With a starting value of $\theta=(-0.2,4.4)$, it produces a final $\theta$ that minimizes total cost, $J$. If you spin the graph around (it's actually easiest to see looking at the bottom of the surface), you can see the steps in $\theta$ the algorithm produced.
Gradient descent's ability to solve for the global minimum of $J$ depends on the learning rate, $\alpha$ and the starting value of $\theta_0$. You can try different values of $\alpha$ and $\theta_0$ to see how they affect the algorithm's ability to successfully solve for the $\theta$ that achieves the global minimum cost.
from pysgd.adaptations import adagrad
disp_mod(adagrad)
import numpy as np
# Define theta generator function for Adagrad
# [Duchi, J., Hazan, E., and Singer, Y. Adaptive Subgradient Methods for Online Learning and Stochastic Optimization](http://stanford.edu/~jduchi/projects/DuchiHaSi10_colt.pdf)
def adapt(params, grad_fun):
def theta_gen_adagrad(theta):
# Initialize hyperparameters and gradient history
alpha = params['alpha']
epsilon = params['epsilon']
grad_hist = 0
# Generate adapted theta
while True:
# Get gradient
gradient = grad_fun(theta)
# Perform gradient adaptation
grad_hist += np.square(gradient)
theta = theta - alpha * gradient / (epsilon + np.sqrt(grad_hist))
yield theta
return theta_gen_adagrad
Run gradient descent algorithm.
theta_hist = pysgd.sgd(
theta0=np.array([-0.2, -4.4]),
alpha=0.3,
adapt='adagrad',
delta_min=10**-9
)
print(theta_hist.shape[0], theta_hist[-1])
273 [ -2.90353403 -2.90353404 -78.33233141]
Plot $J_j$ for each $\theta_j$.
layout.title = 'Adaptive Gradient Descent'
fig = go.Figure(data=[surface, spec_theta_hist_trace(theta_hist)], layout=layout)
py.iplot(fig, filename='adagrad_gradient_descent')
from pysgd.adaptations import adam
disp_mod(adam)
import numpy as np
# Define theta generator function for Adam
# [Kingma, D. P., & Ba, J. L. (2015). Adam: A Method for Stochastic Optimization. International Conference on Learning Representations.](https://arxiv.org/pdf/1412.6980v8.pdf)
def adapt(params, grad_fun):
# Define theta generator
def theta_gen_adam(theta):
# Initialize hyperparameters, moment and iteration variables
alpha = params['alpha']
beta1 = params['beta1']
beta2 = params['beta2']
epsilon = params['epsilon']
moment1 = np.zeros(theta.shape[0])
moment2 = np.zeros(theta.shape[0])
t = 1
# Generate new theta
while True:
# Get gradient
gradient = grad_fun(theta)
# Update moment estimates
moment1 = beta1 * moment1 + (1 - beta1) * gradient
moment2 = beta2 * moment2 + (1 - beta2) * np.square(gradient)
moment1_hat = moment1 / (1 - beta1**t)
moment2_hat = moment2 / (1 - beta2**t)
# Yield adapted gradient
theta = (theta - alpha * (1 - beta2**t)**0.5 / (1 - beta1**t)
* moment1 / (epsilon + np.sqrt(moment2)))
# theta = theta - alpha * moment1_hat / (epsilon + np.sqrt(moment2_hat))
yield theta
t += 1
return theta_gen_adam
Run gradient descent.
theta_hist = pysgd.sgd(theta0=np.array([-0.2, -4.4]), alpha=0.03, adapt='adam')
print(theta_hist.shape[0], theta_hist[-1])
241 [ -2.90357641 -2.90354339 -78.33233137]
Plot $J_j$ for each $\theta_j$.
layout.title = 'Adaptive Moment Estimation'
fig = go.Figure(data=[surface, spec_theta_hist_trace(theta_hist)], layout=layout)
py.iplot(fig, filename='adam_gradient_descent')
from pysgd.objectives import linear
disp_mod(linear)
import numpy as np
def grad_fun(theta, batch):
X, y = batch
return X.T.dot(X.dot(theta) - y) / X.shape[0]
def cost_fun(theta, data):
X = data[:,:-1]
y = data[:,-1]
return np.append(theta, np.sum(np.square(X.dot(theta) - y)) / (2 * X.shape[0]))
Generate sample linear regression data.
data = np.ones((100,3))
obj_theta = np.array([100, 10])
np.random.seed(4)
data[:,1:-1] = np.random.randint(101, size=(100,1))
data[:,-1] = data[:,:-1].dot(obj_theta) * np.random.uniform(0.85, 1.15, 100)
mu = np.mean(data[:,1:-1], axis=0)
sigma = np.std(data[:,1:-1], axis=0)
data[:,1:-1] = (data[:,1:-1] - mu) / sigma
Run gradient descent algorithm.
theta_hist = pysgd.sgd(
theta0=np.array([-0.2, -4.4]),
alpha=0.05,
obj='linear',
data=data, delta_min=10**-3
)
print(theta_hist.shape[0], theta_hist[-1])
0.001 minimum change in theta not achieved in 1000 iterations. 1000 [ 616.21128784 265.36208775 1646.94767019]
Plot sample regression data with model parameters.
plot_data = go.Scatter(
name = 'Training data',
x = data[:,1],
y = data[:,-1],
mode="markers"
)
x_norm = np.ones((2, 2))
x_norm[:,1] = (np.array([0, 100]) - mu) / sigma
y = x_norm.dot(theta_hist[-1,:-1])
plot_theta = go.Scatter(
name = 'Model',
x = x_norm[:,1],
y = y,
mode='lines'
)
layout = go.Layout(
title='Linear Regression'
)
fig = go.Figure(data=[plot_data, plot_theta], layout=layout)
py.iplot(fig, filename='linear-data')
With logistic regression, the dependent variable is classified, meaning it can only assume a limited number of finite values. To account for this, the cost function is modified such that predictions are always between 0 and 1.
$$z = \theta^T x$$$$g(z) = \dfrac{1}{1 + e^{-z}}$$$$0< h_\theta (x) = g (z) < 1$$from pysgd.objectives import logistic
disp_mod(logistic)
import numpy as np
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def grad_fun(theta, batch):
X, y = batch
return (sigmoid(X.dot(theta)) - y).T.dot(X) / X.shape[0]
def cost_fun(theta, data):
X = data[:,:-1]
y = data[:,-1]
sigm = sigmoid(X.dot(theta))
return np.append(theta, np.sum(-y.dot(np.log(sigm)) - (1 - y).dot(np.log(1 - sigm))) / data.shape[0])
Generate sample logistic regression data.
data = np.ones((100,4))
data[:,1:] = np.loadtxt(open("pysgd/tests/logistic.txt","rb"),delimiter=",")
mu = np.mean(data[:,1:-1], axis=0)
sigma = np.std(data[:,1:-1], axis=0)
data[:,1:-1] = (data[:,1:-1] - mu) / sigma
Run gradient descent algorithm.
theta_hist = pysgd.sgd(theta0=np.zeros(3), alpha=0.05, obj='logistic', data=data, delta_min=10**-6)
print(theta_hist.shape[0], theta_hist[-1])
1e-06 minimum change in theta not achieved in 1000 iterations. 1000 [ 1.00304487 2.4903719 2.27781043 0.22446381]
Plot training data and decision boundary.
plot_data = go.Scatter(
name = 'Training data',
x = data[:,1],
y = data[:,2],
mode="markers",
marker=dict(
color=data[:,-1],
colorscale='Bluered',
)
)
# Plot decision boundary
x1 = np.ones((2,2))
x1[:,-1] = np.array([-1.8, 1.8])
x2 = x1.dot((theta_hist[-1,:-2] / -theta_hist[-1,-2]))
plot_db = go.Scatter(
name = 'Decision boundary',
x = x1[:,-1],
y = x2,
mode="lines"
)
layout = go.Layout(
title='Logistic Regression'
)
fig = go.Figure(data=[plot_data, plot_db], layout=layout)
py.iplot(fig, filename='logistic-regression')