goml

package module
v0.0.0-...-1838e86 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2014 License: Apache-2.0 Imports: 3 Imported by: 0

README

goml

A few machine learning algorithms implemented in the go programming language.

Documentation

Index

Constants

View Source
const (
	L1_PENALTY = 0
	L2_PENALTY = 1
)

Variables

This section is empty.

Functions

func Apply

func Apply(A mat.MatrixRO, f SFunction) mat.MatrixRO

Apply applies the function f to each element in A and returns a new matrix with the results.

Input ===== A : a matrix f : a function from scalar values to scalar values

Returns ======= a matrix derived by applying f to each element in A. If f is nil, then this function just returns A.

func SqError

func SqError(a, b float64) float64

SqError computes the squared difference between two scalar values a and b.

Input ===== a : a scalar value b : a scalar value

Returns ======= the squared difference between a and b

Types

type ActivationFunction

type ActivationFunction interface {
	/*
		Eval computes the value of this activation function at x.

		Input
		=====
		x : a scalar value

		Returns
		=======
		the value of this activation function at x
	*/
	Eval(x float64) float64

	/*
		Deriv computes the derivative of this activation function at x.

		Input
		=====
		x : a scalar value

		Returns
		=======
		the derivative of this activation function at x
	*/
	Deriv(x float64) float64
}

ActivationFunction is used to apply a non-linear transformation to the output of a linear function. Activation functions need to be differentiable so that they can be used with gradient descent.

type Function

type Function interface {

	/*
		Predict evalutes this function at the point specified by the given
		vector and returns a scalar value.

		Input
		=====
		instance : a row vector

		Returns
		=======
		a scalar value or an error
	*/
	Predict(instance mat.MatrixRO) (float64, error)

	/*
		PredictM evaluates each row of the specified matrix.

		Input
		=====
		instances : a matrix where each row corresponds to an input vector

		Returns
		=======
		a vector containing one prediction for each row vector in instances
	*/
	PredictM(instances mat.MatrixRO) (mat.MatrixRO, error)

	/*
		InputDims returns the number of dimensions of a valid input vector.

		Returns
		=======
		the number of dimensions of a valid input vector
	*/
	InputDims() int
}

Function is a mapping from a vector space to a float64.

type FunctionApproximator

type FunctionApproximator interface {
	Function

	/*
		Fit fits this function approximator to the training data specified by
		the matrix x and the vector y. Generally this method needs to be called
		before Predict() can be called.

		Input
		=====
		x : a matrix where each row is an input vector
		y : a column vector where each element corresponds to the desired output
		 for the corresponding row of x

		Returns
		=======
		an error if the fitting process fails
	*/
	Fit(x mat.MatrixRO, y mat.MatrixRO) error
}

FunctionApproximator is a function that can be trained given a labeled set of input vector, scalar value pairs.

type LinearFunction

type LinearFunction struct {
	Weights mat.DenseMatrix
	AFunc   ActivationFunction
}

LinearFunction is a Function that evaluates input vectors by multiplying them by a weight vector.

func (LinearFunction) InputDims

func (f LinearFunction) InputDims() int

InputDims returns the number of dimensions of a valid input vector.

Returns ======= the number of dimensions of a valid input vector

func (LinearFunction) Predict

func (f LinearFunction) Predict(x mat.MatrixRO) (float64, error)

func (LinearFunction) PredictM

func (f LinearFunction) PredictM(x mat.MatrixRO) (mat.MatrixRO, error)

type SFunction

type SFunction func(float64) float64

SFunction is a function that maps a scalar value to another scalar value.

type SGD

type SGD struct {
	/*
		The penalty type to apply (L1_PENALTY or L2_PENALTY).
	*/
	PenaltyType int
	/*
		The regularization parameter.
	*/
	Lambda float64
	/*
		The number of iterations to train for.
	*/
	NumIterations int
	/*
		The learning rate used during training.
	*/
	LearningRate float64
	// contains filtered or unexported fields
}

SGD performs Stochastic Gradient Descent.

func NewSGD

func NewSGD(penaltyType int, lambda float64, numIterations int, learningRate float64, afunc ActivationFunction) (*SGD, error)

NewSGD constructs a new SGD instance.

Input ===== penaltyType : the type of regularization penalty to user during fitting (either L1_PENALTY or L2_PENALTY) lambda : the regularization parameter (should >= 0) numIterations : the number of iterations to run during fitting. One iteration corresponds to updating with a single sample. learningRate : the constant learning rate parameter to use during training afunc : the activation function to apply to output (nil is interpreted as the identity function)

Returns ======= a pointer to a new (untrained) SGD instance or an error

func (*SGD) Fit

func (self *SGD) Fit(x mat.MatrixRO, y mat.MatrixRO) error

func (SGD) InputDims

func (self SGD) InputDims() int

func (*SGD) NewCopy

func (self *SGD) NewCopy() (*SGD, error)

NewCopy returns a new instance of *SGD with the same parameters as this instance, but it does not copy any weights. So the new instance can be trained with any (x, y) training samples.

Returns ======= a new instance of *SGD with the same parameters as this instance

func (SGD) Predict

func (self SGD) Predict(x mat.MatrixRO) (float64, error)

func (SGD) PredictM

func (self SGD) PredictM(x mat.MatrixRO) (mat.MatrixRO, error)

func (SGD) Weights

func (self SGD) Weights() mat.DenseMatrix

Weights returns the weight vector obtained during the fitting process. If Fit() has not been executed yet, then the behavior of this method is undefined.

Returns ======= a column vector of weights (including an additional weight for the bias)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL