예제 #1
0
파일: linreg.go 프로젝트: c4e8ece0/ml
// LearnWeightDecay computes the following formula and update WReg.
// (Z'Z+λI)^−1 * Z'
// WReg = (Z'Z + λI)^−1 Z'y
//
func (lr *LinearRegression) LearnWeightDecay() error {
	lr.Lambda = math.Pow(10, float64(lr.K))

	var err error

	// compute X' <=> X transpose
	var mX ml.Matrix = lr.Xn
	var mXT ml.Matrix

	if mXT, err = mX.Transpose(); err != nil {
		return err
	}

	// compute lambda*Identity
	var ID ml.Matrix
	if ID, err = ml.Identity(len(lr.Xn[0])); err != nil {
		return err
	}

	var mLID ml.Matrix

	if mLID, err = ID.Scalar(lr.Lambda); err != nil {
		return err
	}

	// compute Z'Z
	var mXP ml.Matrix
	if mXP, err = mXT.Product(mX); err != nil {
		return err
	}

	// compute Z'Z + lambda*I
	var mS ml.Matrix
	if mS, err = mLID.Add(mXP); err != nil {
		return err
	}

	// inverse
	var mInv ml.Matrix

	if mInv, err = mS.Inverse(); err != nil {
		return err
	}

	// compute product: inverseMatrix Z'
	var XDagger ml.Matrix
	if XDagger, err = mInv.Product(mXT); err != nil {
		return err
	}

	// set WReg
	lr.setWeightReg(XDagger)
	return nil
}
예제 #2
0
파일: linreg.go 프로젝트: c4e8ece0/ml
// Learn will compute the pseudo inverse X dager and set W vector accordingly
// XDagger = (X'X)^-1 X'
//
func (lr *LinearRegression) Learn() error {

	var err error

	// compute X' <=> X transpose
	var mXn ml.Matrix = lr.Xn
	var mXT ml.Matrix

	if mXT, err = mXn.Transpose(); err != nil {
		return err
	}

	// compute the product of X' and X
	var mXP ml.Matrix
	if mXP, err = mXT.Product(mXn); err != nil {
		return err
	}

	// inverse XProduct
	var mXInv ml.Matrix
	if mXInv, err = mXP.Inverse(); err != nil {
		return err
	}

	// compute product: (X'X)^-1 X'
	var XDagger ml.Matrix
	if XDagger, err = mXInv.Product(mXT); err != nil {
		return err
	}

	lr.setWeight(XDagger)

	return nil
}