Ejemplo n.º 1
0
func (this *TrainingSet) Variance_sum(i0, i1 int, mean *Matrix.Matrix, res **Matrix.Matrix, sustract *Matrix.Matrix, done chan<- bool) {
	di := i1 - i0

	if di >= THRESHOLD {
		mi := i0 + di/2
		done2 := make(chan bool, THRESHOLD)

		res1 := Matrix.NullMatrixP(1, this.Xs.GetNColumns())
		res2 := Matrix.NullMatrixP(1, this.Xs.GetNColumns())

		go this.Variance_sum(i0, mi, mean, &res1, sustract, done2)
		go this.Variance_sum(mi, i1, mean, &res1, sustract, done2)

		<-done2
		<-done2

		SP, _ := Matrix.Sum(res1, res2)
		*res = SP

	} else {
		for i := i0; i <= i1; i++ {
			xsi := this.Xs.GetRow(i)
			Sustract, _ := Matrix.Sustract(mean, xsi)
			Square := Matrix.DotMultiplication(Sustract, Sustract)

			sustract.SetRow(i, Sustract)

			SP, _ := Matrix.Sum(Square, *res)
			*res = SP
		}
	}
	done <- true
}
Ejemplo n.º 2
0
func DSoftmax(X *Matrix.Matrix) *Matrix.Matrix {
	Total := 1 / X.TaxicabNorm()
	Y := X.Scalar(complex(Total, 0))

	S, _ := Matrix.Sustract(Matrix.FixValueMatrix(X.GetNColumns(), X.GetNColumns(), 1.0), X)

	YD := Matrix.DotMultiplication(Y, S)
	return YD
}
Ejemplo n.º 3
0
func DerivateHalfDistance(T, O *Matrix.Matrix) *Matrix.Matrix {

	r, _ := Matrix.Sustract(T, O)

	return r
}
Ejemplo n.º 4
0
func GradientDescent(alpha complex128, Tolerance complex128, ts *TrainingSet, f func(x complex128) complex128) *Hypothesis {
	n := ts.Xs.GetNColumns()
	m := ts.Xs.GetMRows()

	//Xsc:=ts.Xs.Copy()

	ts.AddX0() // add  the parametrer x0, with value 1, to all elements of the training set

	t := Matrix.NullMatrixP(1, n+1) // put 0 to the parameters theta
	thetaP := t

	//thetaP:=Matrix.RandomMatrix(1,n+1)  // Generates a random values of parameters theta

	var h1 Hypothesis

	h1.H = f
	h1.ThetaParameters = thetaP

	var Error complex128

	Error = complex(1.0, 0)

	var it = 1

	diferencia, diferenciaT := h1.Parallel_DiffH1Ys(ts)
	jt := Matrix.Product(diferenciaT, diferencia).Scalar(1/complex(2.0*float64(m), 0.0)).GetValue(1, 1)

	alpha = 1 / jt

	for cmplx.Abs(Error) >= cmplx.Abs(Tolerance) { // Until converges

		ThetaPB := h1.ThetaParameters.Copy() //for Error Calc

		//diff:=h1.DiffH1Ys(ts)
		_, diffT := h1.Parallel_DiffH1Ys(ts) //h(x)-y

		product := Matrix.Product(diffT, ts.Xs) //Sum( (hi(xi)-yi)*xij)  in matrix form

		h1.Sum = product

		alpha_it := alpha / (cmplx.Sqrt(complex(float64(it), 0.0))) // re-calc alpha

		scalar := product.Scalar(-alpha_it / complex(float64(m), 0.0))

		//println("Delta", scalar.ToString())
		ThetaTemp, _ := Matrix.Sum(h1.ThetaParameters, scalar) //Theas=Theas-alfa/m*Sum( (hi(xi)-yi)*xij)  update the parameters

		h1.ThetaParameters = ThetaTemp

		diffError, _ := Matrix.Sustract(ThetaPB, h1.ThetaParameters) //diff between theta's Vector , calc the error

		Error = complex(diffError.FrobeniusNorm(), 0) //Frobenius Norm
		//Error=diffError.InfinityNorm()              //Infinty Norm

		//println("->", h1.ThetaParameters.ToString())
		//println("Error", Error)
		/*if it > 10 {
			break
		}*/
		it++
	}
	h1.M = m
	return &h1
}