Skip to main content

Python package for Mathematical Statistics with a View Toward Machine Learning, by John Myers

Project description

Utility functions for "Mathematical Statistics with a View Toward Machine Learning", by John Myers

A Python package for all utility functions used in the book and programming assignments. To install, do the usual:

pip install math_stats_ml

All other materials are contained here.

Table of contents:

  1. gd submodule: Gradient descent utilities

gd submodule: Gradient descent utilities

Contains all utilities for the gradient descent algorithms used in the book.

GD_output class: Container class for output of algorithms

class GD_output

A class holding the outputs of both the gradient descent (GD) and stochastic gradient descent (SGD) algorithms. All attributes below are optional and default to None.

Attributes

Name Type Description
parameters dict A dictionary containing the parameters of the objective function passed to either GD or SGD. Each value in the dictionary is a tensor whose zero-th dimension indexes the number of gradient steps.
per_step_objectives torch.Tensor A tensor containing the running objective values, per gradient step.
per_epoch_objectives torch.Tensor A tensor containing the running mean objective values, per epoch.
epoch_step_nums torch.Tensor A tensor containing the number of each gradient step on which an epoch begins/ends.
grad_steps iter An iterable ranging from $0$ to one less than the total number of gradient steps. (This is convenient for plotting purposes.)
lr float Learning rate.
num_steps int Number of gradient steps to run the gradient descent (GD) algorithm.
decay_rate float Learning rate decay.
batch_size int Mini-batch size for the stochastic gradient descent (SGD) algorithm.
num_epochs int Number of epochs for the stochastic gradient descent (SGD) algorithm.
max_steps int Maximum number of gradient steps after which we terminate the stochastic gradient descent (SGD) algorithm.
type_flag str Either None or gd. In the latter case, indicates whether the GD_output object was obtained from the gradient descent (GD) algorithm.

GD function: Gradient descent

GD(J, init_parameters, lr, num_steps, decay_rate=0)

Implementation of gradient descent. The notation below is intended to match the notation in the description in the book.

Output

The output type is an object of the GD_output class.

Parameters

Name Type Description
J function Objective function to be minimized. The parameters of the function are either a single tensor or a dictionary of tensors (in the case that the parameters fall into natural groups, e.g., weights and biases).
init_parameters torch.Tensor or dict Initial parameters.
lr float Learning rate, corresponding to $\alpha$ in the book.
num_steps int The number of gradient steps after which the algorithm should halt, corresponding to $N$ in the book.
decay_rate float Learning rate decay, corresponding to $\beta$ in the book. Defaults to 0.

SGD function: Stochastic gradient descent

SGD(g, init_parameters, X, lr, batch_size, num_epochs, y=None, decay_rate=0, max_steps=-1, shuffle=True, random_state=None)

Implementation of stochastic gradient descent. The notation and terminology below is intended to match the book.

Output

The output type is an object of the GD_output class.

Parameters

Name Type Description
g function Target function for the algorithm. (The notation and terminology is intended to match the book---see the link above.) The call signature of the function is of the form g(parameters, x) or g(parameters, x, y), where x is a feature vector and y is an (optional) ground truth label of a single instance in the dataset, and parameters is either a single parameter tensor or a dictionary of parameter tensors (in the case that the parameters fall into natural groups, e.g., weights and biases). We assume that g is "vectorized," so that it may accept a design matrix X in place of x and an entire vector of ground truth labels for y.
init_parameters torch.Tensor or dict Initial parameters.
X torch.Tensor Design matrix. The rows are the feature vectors that are fed into the function g.
lr float Learning rate, corresponding to $\alpha$ in the book.
batch_size int Mini-batch size, corresponding to $k$ in the book.
num_epochs int The number of epochs after which the algorithm should halt, corresponding to $N$ in the book.
y torch.Tensor Vector of ground truth labels for the data in the design matrix X. Optional, defaults to None.
decay_rate float Learning rate decay, corresponding to $\beta$ in the book. Defaults to 0.
max_steps int Maximum number of gradient steps after which the algorithm should halt. Defaults to -1, in which case the algorithm will complete all num_epochs many epochs.
shuffle bool Determines whether to shuffle the dataset before looping through an epoch. Defaults to True.
random_state int If not None and shuffle=True, random seed to be passed to torch.manual_seed. Defaults to None.

plot_gd function: plot the output of gradient descent

plot_gd(gd_output, w=5, h=4, plot_title=True, parameter_title=True, show_xlabel=True, xlabel='gradient steps', show_ylabel=True, ylabel='surprisal', alpha=1, color=None, ax=None)

Later...

plot_sgd function: plot the output of stochastic gradient descent

plot_sgd(sgd_output, w=5, h=4, plot_title=True, parameter_title=True, show_step=True, show_epoch=True, show_xlabel=True, xlabel='gradient steps', show_ylabel=True, ylabel='cross entropy', legend=True, per_step_alpha=0.25, per_step_color=None, per_step_label='cross entropy per step', per_epoch_color=None, per_epoch_label='mean cross entropy per epoch', s=10, ax=None)

Later...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

math_stats_ml-0.0.9.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

math_stats_ml-0.0.9-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file math_stats_ml-0.0.9.tar.gz.

File metadata

  • Download URL: math_stats_ml-0.0.9.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for math_stats_ml-0.0.9.tar.gz
Algorithm Hash digest
SHA256 fa63ac212600ec0ac08e7137440ad8fbf0b563671802802c1a0874f047150b7d
MD5 7080dcc3f06fb9df375567ee56f73f28
BLAKE2b-256 0009eb3038261fcdc8cd75da4508e1a4efb48def36594202bc6d41d6ce565adb

See more details on using hashes here.

File details

Details for the file math_stats_ml-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: math_stats_ml-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for math_stats_ml-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 95ad1f31d79e6865c6b111f32cd57b421f6a42b5874c3ef6bf9c996e7d6669ec
MD5 6d505958a6c5cdaf488b8cc7e56b7574
BLAKE2b-256 08134c5b60f6aef4ba242567addd3b0ec9ffd7d7c8304b544975fb98f6a589e9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page