Example #1
0
func (fft *FastFtrlTrainer) TrainImpl(
	model_file string,
	train_file string,
	line_cnt int,
	test_file string) error {

	if !fft.Init {
		fft.log4fft.Error("[FastFtrlTrainer-TrainImpl] Fast ftrl trainer restore error.")
		return errors.New("[FastFtrlTrainer-TrainImpl] Fast ftrl trainer restore error.")
	}

	fft.log4fft.Info(fmt.Sprintf(
		"[%s] params={alpha:%.2f, beta:%.2f, l1:%.2f, l2:%.2f, dropout:%.2f, epoch:%d}\n",
		fft.JobName,
		fft.ParamServer.Alpha,
		fft.ParamServer.Beta,
		fft.ParamServer.L1,
		fft.ParamServer.L2,
		fft.ParamServer.Dropout,
		fft.Epoch))

	var solvers []solver.FtrlWorker = make([]solver.FtrlWorker, fft.NumThreads)
	for i := 0; i < fft.NumThreads; i++ {
		solvers[i].Initialize(&fft.ParamServer, fft.PusStep, fft.FetchStep)
	}

	predict_func := func(x util.Pvector) float64 {
		return fft.ParamServer.Predict(x)
	}

	var timer util.StopWatch
	timer.StartTimer()
	for iter := 0; iter < fft.Epoch; iter++ {
		var file_parser ParallelFileParser
		file_parser.OpenFile(train_file, fft.NumThreads)
		count := 0
		var loss float64 = 0.

		var lock sync.Mutex
		worker_func := func(i int, c *sync.WaitGroup) {
			local_count := 0
			var local_loss float64 = 0
			for {
				flag, y, x := file_parser.ReadSampleMultiThread(i)
				if flag != nil {
					break
				}

				pred := solvers[i].Update(x, y, &fft.ParamServer)
				local_loss += calc_loss(y, pred)
				local_count++

				if i == 0 && local_count%10000 == 0 {
					tmp_cnt := math.Min(float64(local_count*fft.NumThreads), float64(line_cnt))
					fft.log4fft.Info(fmt.Sprintf("[%s] epoch=%d processed=[%.2f%%] time=[%.2f] train-loss=[%.6f]\n",
						fft.JobName,
						iter,
						float64(tmp_cnt*100)/float64(line_cnt),
						timer.StopTimer(),
						float64(local_loss)/float64(local_count)))
				}
			}
			lock.Lock()
			count += local_count
			loss += local_loss
			lock.Unlock()

			solvers[i].PushParam(&fft.ParamServer)
			defer c.Done()
		}

		if iter == 0 && util.UtilGreater(fft.BurnIn, float64(0)) {
			burn_in_cnt := int(fft.BurnIn * float64(line_cnt))
			var local_loss float64 = 0
			for i := 0; i < burn_in_cnt; i++ {
				//线程0做预热
				flag, y, x := file_parser.ReadSample(0)
				if flag != nil {
					break
				}

				pred := fft.ParamServer.Update(x, y)
				local_loss += calc_loss(y, pred)
				if i%10000 == 0 {
					fft.log4fft.Info(fmt.Sprintf("[%s] burn-in processed=[%.2f%%] time=[%.2f] train-loss=[%.6f]\n",
						fft.JobName,
						float64((i+1)*100)/float64(line_cnt),
						timer.StopTimer(),
						float64(local_loss)/float64(i+1)))
				}
			}

			fft.log4fft.Info(fmt.Sprintf("[%s] burn-in processed=[%.2f%%] time=[%.2f] train-loss=[%.6f]\n",
				fft.JobName,
				float64(burn_in_cnt*100)/float64(line_cnt),
				timer.StopTimer(),
				float64(local_loss)/float64(burn_in_cnt)))

			if util.UtilFloat64Equal(fft.BurnIn, float64(1)) {
				continue
			}
		}

		for i := 0; i < fft.NumThreads; i++ {
			solvers[i].Reset(&fft.ParamServer)
		}

		util.UtilParallelRun(worker_func, fft.NumThreads)

		file_parser.CloseFile(fft.NumThreads)

		//		f(w,
		//			"[%s] epoch=%d processed=[%.2f%%] time=[%.2f] train-loss=[%.6f]\n",
		//			fft.JobName,
		//			iter,
		//			float64(count*100)/float64(line_cnt),
		//			timer.StopTimer(),
		//			float64(loss)/float64(count))

		if test_file != "" {
			eval_loss := evaluate_file(test_file, predict_func, fft.NumThreads)
			fft.log4fft.Info(fmt.Sprintf("[%s] validation-loss=[%f]\n", fft.JobName, float64(eval_loss)))
		}
	}

	return fft.ParamServer.SaveModel(model_file)
}
Example #2
0
func (lft *LockFreeFtrlTrainer) TrainBatch(
	encodemodel string,
	instances []string) error {

	line_cnt := len(instances)
	if line_cnt == 0 {
		lft.log.Error("[LockFreeFtrlTrainer-TrainBatch] No model retrained.")
		return errors.New("[LockFreeFtrlTrainer-TrainBatch] No model retrained.")
	}

	var fls solver.FtrlSolver
	err := json.Unmarshal([]byte(encodemodel), &fls)
	if err != nil {
		lft.log.Error("[LockFreeFtrlTrainer-TrainBatch]" + err.Error())
		return errors.New("[LockFreeFtrlTrainer-TrainBatch]" + err.Error())
	}

	lft.Solver = fls

	lft.log.Info(fmt.Sprintf("[%s] params={alpha:%.2f, beta:%.2f, l1:%.2f, l2:%.2f, dropout:%.2f, epoch:%d}\n",
		lft.JobName,
		lft.Solver.Alpha,
		lft.Solver.Beta,
		lft.Solver.L1,
		lft.Solver.L2,
		lft.Solver.Dropout,
		lft.Epoch))

	predict_func := func(x util.Pvector) float64 {
		return lft.Solver.Predict(x)
	}

	var timer util.StopWatch
	timer.StartTimer()
	for iter := 0; iter < lft.Epoch; iter++ {
		var stream_parser StreamParser
		stream_parser.Open(instances)

		count := 0
		var loss float64 = 0

		var lock sync.Mutex

		worker_func := func(i int, c *sync.WaitGroup) {
			local_count := 0
			var local_loss float64 = 0
			for {
				flag, y, x := stream_parser.ReadSampleMultiThread()
				if flag != nil {
					break
				}

				pred := lft.Solver.Update(x, y)
				local_loss += calc_loss(y, pred)
				local_count++

				if i == 0 && local_count%10000 == 0 {
					tmp_cnt := math.Min(float64(local_count*lft.NumThreads), float64(line_cnt))
					lft.log.Info(fmt.Sprintf("[%s] epoch=%d processed=[%.2f%%] time=[%.2f] train-loss=[%.6f]\n",
						lft.JobName,
						iter,
						float64(tmp_cnt*100)/float64(line_cnt),
						timer.StopTimer(),
						float64(local_loss)/float64(local_count)))
				}
			}

			lock.Lock()
			count += local_count
			loss += local_loss
			lock.Unlock()
			defer c.Done()
		}

		util.UtilParallelRun(worker_func, lft.NumThreads)

		stream_parser.Close()

		lft.log.Info(fmt.Sprintf("[%s] epoch=%d processed=[%.2f%%] time=[%.2f] train-loss=[%.6f]\n",
			lft.JobName,
			iter,
			float64(count*100)/float64(line_cnt),
			timer.StopTimer(),
			float64(loss)/float64(count)))

		eval_loss := evaluate_stream(instances, predict_func, 0)
		lft.log.Info(fmt.Sprintf("[%s] validation-loss=[%f]\n", lft.JobName, float64(eval_loss)))
	}

	return nil
}
Example #3
0
func (lft *LockFreeFtrlTrainer) TrainImpl(
	model_file string,
	train_file string,
	line_cnt int,
	test_file string) error {
	if !lft.Init {
		lft.log.Error("[LockFreeFtrlTrainer-TrainImpl] Fast ftrl trainer restore error.")
		return errors.New("[LockFreeFtrlTrainer-TrainImpl] Fast ftrl trainer restore error.")
	}

	lft.log.Info(fmt.Sprintf("[%s] params={alpha:%.2f, beta:%.2f, l1:%.2f, l2:%.2f, dropout:%.2f, epoch:%d}\n",
		lft.JobName,
		lft.Solver.Alpha,
		lft.Solver.Beta,
		lft.Solver.L1,
		lft.Solver.L2,
		lft.Solver.Dropout,
		lft.Epoch))

	predict_func := func(x util.Pvector) float64 {
		return lft.Solver.Predict(x)
	}

	var timer util.StopWatch
	timer.StartTimer()
	for iter := 0; iter < lft.Epoch; iter++ {
		var file_parser FileParser
		file_parser.OpenFile(train_file)

		count := 0
		var loss float64 = 0

		var lock sync.Mutex

		worker_func := func(i int, c *sync.WaitGroup) {
			local_count := 0
			var local_loss float64 = 0
			for {
				flag, y, x := file_parser.ReadSampleMultiThread()
				if flag != nil {
					break
				}

				pred := lft.Solver.Update(x, y)
				local_loss += calc_loss(y, pred)
				local_count++

				if i == 0 && local_count%10000 == 0 {
					tmp_cnt := math.Min(float64(local_count*lft.NumThreads), float64(line_cnt))
					lft.log.Info(fmt.Sprintf("[%s] epoch=%d processed=[%.2f%%] time=[%.2f] train-loss=[%.6f]\n",
						lft.JobName,
						iter,
						float64(tmp_cnt*100)/float64(line_cnt),
						timer.StopTimer(),
						float64(local_loss)/float64(local_count)))
				}
			}

			lock.Lock()
			count += local_count
			loss += local_loss
			lock.Unlock()
			defer c.Done()
		}

		util.UtilParallelRun(worker_func, lft.NumThreads)

		file_parser.CloseFile()

		lft.log.Info(fmt.Sprintf("[%s] epoch=%d processed=[%.2f%%] time=[%.2f] train-loss=[%.6f]\n",
			lft.JobName,
			iter,
			float64(count*100)/float64(line_cnt),
			timer.StopTimer(),
			float64(loss)/float64(count)))

		if test_file != "" {
			eval_loss := evaluate_file(test_file, predict_func, 0)
			lft.log.Info(fmt.Sprintf("[%s] validation-loss=[%f]\n", lft.JobName, float64(eval_loss)))
		}
	}

	return lft.Solver.SaveModel(model_file)
}
Example #4
0
func (ft *FtrlTrainer) TrainImpl(
	model_file string,
	train_file string,
	line_cnt int,
	test_file string) error {
	if !ft.Init {
		ft.log.Error("[FtrlTrainer-TrainImpl] Fast ftrl trainer restore error.")
		return errors.New("[FtrlTrainer-TrainImpl] Fast ftrl trainer restore error.")
	}

	ft.log.Info(fmt.Sprintf("[%s] params={alpha:%.2f, beta:%.2f, l1:%.2f, l2:%.2f, dropout:%.2f, epoch:%d}\n",
		ft.JobName,
		ft.Solver.Alpha,
		ft.Solver.Beta,
		ft.Solver.L1,
		ft.Solver.L2,
		ft.Solver.Dropout,
		ft.Epoch))

	predict_func := func(x util.Pvector) float64 {
		return ft.Solver.Predict(x)
	}

	var timer util.StopWatch
	timer.StartTimer()
	var last_time float64 = 0
	for iter := 0; iter < ft.Epoch; iter++ {
		var file_parser FileParser
		file_parser.OpenFile(train_file)

		cur_cnt := 0
		last_cnt := 0
		var loss float64 = 0
		for {
			flag, y, x := file_parser.ReadSample()
			if flag != nil {
				break
			}

			pred := ft.Solver.Update(x, y)
			loss += calc_loss(y, pred)
			cur_cnt++

			if cur_cnt-last_cnt > 100000 && timer.StopTimer()-last_time > 0.5 {
				ft.log.Info(fmt.Sprintf("[%s] epoch=%d processed=[%.2f%%] time=[%.2f] train-loss=[%.6f]\n",
					ft.JobName,
					iter,
					float64(cur_cnt*100)/float64(line_cnt),
					timer.StopTimer(),
					float64(loss)/float64(cur_cnt)))

				last_cnt = cur_cnt
				last_time = timer.StopTimer()
			}
		}
		ft.log.Info(fmt.Sprintf("[%s] epoch=%d processed=[%.2f%%] time=[%.2f] train-loss=[%.6f]\n",
			ft.JobName,
			iter,
			float64(cur_cnt*100)/float64(line_cnt),
			timer.StopTimer(),
			float64(loss)/float64(cur_cnt)))

		file_parser.CloseFile()

		if test_file != "" {
			eval_loss := evaluate_file(test_file, predict_func, 0)
			ft.log.Info(fmt.Sprintf("[%s] validation-loss=[%f]\n", float64(eval_loss)))
		}
	}

	return ft.Solver.SaveModel(model_file)
}