Example #1
0
//更新权重方法
func (fs *FtrlSolver) Update(x util.Pvector, y float64) float64 {
	if !fs.Init {
		return 0
	}

	var weights util.Pvector = make(util.Pvector, fs.Featnum)
	var gradients []float64 = make([]float64, fs.Featnum)

	var wTx float64 = 0.

	for i := 0; i < len(x); i++ {
		item := x[i]
		if util.UtilGreater(fs.Dropout, 0.0) {
			rand_prob := util.UniformDistribution()
			if rand_prob < fs.Dropout {
				continue
			}
		}

		var idx int = item.Index
		if idx >= fs.Featnum {
			continue
		}

		//获取w权重值
		var val float64 = fs.GetWeight(idx)
		//建立w权重数组
		weights = append(weights, util.Pair{idx, val})
		//每个样本梯度值默认赋值为样本x本身
		gradients = append(gradients, item.Value)
		//计算仿射函数wT*x的值
		wTx += val * item.Value
	}

	//计算模型预估值
	var pred float64 = util.Sigmoid(wTx)
	//计算p_t-y_t值,为计算每个样本的梯度做准备
	var grad float64 = pred - y
	//计算g_i = (p_t-y_t)*x_i
	util.VectorMultiplies(gradients, grad)

	for k := 0; k < len(weights); k++ {
		var i int = weights[k].Index
		var w_i float64 = weights[k].Value
		var grad_i float64 = gradients[k]
		var sigma float64 = (math.Sqrt(fs.N[i]+grad_i*grad_i) - math.Sqrt(fs.N[i])) / fs.Alpha
		//z_i=z_i+g_i-sigma_i*w_(t,i)
		fs.Z[i] += grad_i - sigma*w_i
		//n_i=n_i+g_i*g_i
		fs.N[i] += grad_i * grad_i
	}

	return pred
}
Example #2
0
func (fw *FtrlWorker) Update(
	x util.Pvector,
	y float64,
	param_server *FtrlParamServer) float64 {

	if !fw.FtrlSolver.Init {
		return 0.
	}

	var weights util.Pvector = make(util.Pvector, fw.FtrlSolver.Featnum)
	var gradients []float64 = make([]float64, fw.FtrlSolver.Featnum)
	var wTx float64 = 0.

	for i := 0; i < len(x); i++ {
		item := x[i]
		if util.UtilGreater(fw.FtrlSolver.Dropout, 0.0) {
			rand_prob := util.UniformDistribution()
			if rand_prob < fw.FtrlSolver.Dropout {
				continue
			}
		}
		var idx int = item.Index
		if idx >= fw.FtrlSolver.Featnum {
			continue
		}

		//获取w权重值
		var val float64 = fw.FtrlSolver.GetWeight(idx)
		//建立w权重数组
		weights = append(weights, util.Pair{idx, val})
		//每个样本梯度值默认赋值为样本x本身
		gradients = append(gradients, item.Value)
		//计算仿射函数wT*x的值
		wTx += val * item.Value
	}

	//计算模型预估值
	var pred float64 = util.Sigmoid(wTx)
	//计算p_t-y_t值,为计算每个样本的梯度做准备
	var grad float64 = pred - y
	//计算g_i = (p_t-y_t)*x_i
	util.VectorMultiplies(gradients, grad)

	for k := 0; k < len(weights); k++ {
		var i int = weights[k].Index
		var g int = i / ParamGroupSize

		if fw.ParamGroupStep[g]%fw.FetchStep == 0 {
			param_server.FetchParamGroup(
				fw.FtrlSolver.N,
				fw.FtrlSolver.Z,
				g)
		}

		var w_i float64 = weights[k].Value
		var grad_i float64 = gradients[k]
		var sigma float64 = (math.Sqrt(fw.FtrlSolver.N[i]+grad_i*grad_i) - math.Sqrt(fw.FtrlSolver.N[i])) / fw.FtrlSolver.Alpha

		fw.FtrlSolver.Z[i] += grad_i - sigma*w_i
		fw.FtrlSolver.N[i] += grad_i * grad_i
		fw.ZUpdate[i] += grad_i - sigma*w_i
		fw.NUpdate[i] += grad_i * grad_i

		if fw.ParamGroupStep[g]%fw.PushStep == 0 {
			param_server.PushParamGroup(fw.NUpdate, fw.ZUpdate, g)
		}

		fw.ParamGroupStep[g] += 1
	}

	return pred
}