func (fft *FastFtrlTrainer) Train(
	alpha float64,
	beta float64,
	l1 float64,
	l2 float64,
	dropout float64,
	model_file string,
	train_file string,
	test_file string) error {

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

	if !util.FileExists(train_file) || !util.FileExists(test_file) {
		fft.log4fft.Error("[FastFtrlTrainer-Train] Train file or test file is not exist.")
		return errors.New("[FastFtrlTrainer-Train] Train file or test file is not exist.")
	}

	feat_num, line_cnt, _ := read_problem_info(train_file, fft.CacheFeatureNum, fft.NumThreads)
	if feat_num == 0 {
		fft.log4fft.Error("[FastFtrlTrainer-Train] The number of features is zero.")
		return errors.New("[FastFtrlTrainer-Train] The number of features is zero.")
	}

	err := fft.ParamServer.Initialize(alpha, beta, l1, l2, feat_num, dropout)
	if err != nil {
		fft.log4fft.Error(fmt.Sprintf("[FastFtrlTrainer-Train] Parameter server initializing error.%s", err.Error()))
		return errors.New(fmt.Sprintf("[FastFtrlTrainer-Train] Parameter server initializing error.%s", err.Error()))
	}

	return fft.TrainImpl(model_file, train_file, line_cnt, test_file)
}
Beispiel #2
0
func StreamRun(model_file string, instances []string) (string, error) {
	log := util.GetLogger()
	if !util.FileExists(model_file) || len(instances) == 0 {
		log.Error("[Predictor-StreamRun] Model file or instances error.")
		return fmt.Sprintf(errorjson, "[Predictor-StreamRun] Model file or instances error."), errors.New("[Predictor-StreamRun] Model file or instances error.")
	}

	var rtstr string
	var model solver.LRModel
	model.Initialize(model_file)
	for i := 0; i < len(instances); i++ {
		res, _, x := util.ParseSample(instances[i])
		if res != nil {
			break
		}

		pred := model.Predict(x)
		pred = math.Max(math.Min(pred, 1.-10e-15), 10e-15)
		if i == len(instances)-1 {
			rtstr += strconv.FormatFloat(pred, 'f', 6, 64)
		} else {
			rtstr += strconv.FormatFloat(pred, 'f', 6, 64) + ","
		}
	}

	return fmt.Sprintf(streamjson, rtstr), nil
}