//更新权重方法 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 }
func (lr *LRModel) Predict(x util.Pvector) float64 { if !lr.Init { return 0 } var wTx float64 = 0. for i := 0; i < len(x); i++ { item := x[i] wTx += lr.Model[item.Index] * item.Value } var pred float64 = util.Sigmoid(wTx) return pred }
func (fs *FtrlSolver) Predict(x util.Pvector) float64 { if !fs.Init { return 0 } var wTx float64 = 0. for i := 0; i < len(x); i++ { idx := x[i].Index val := fs.GetWeight(idx) wTx += val * x[i].Value } pred := util.Sigmoid(wTx) return pred }
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 }