Exemplo n.º 1
0
func (fs *FtrlSolver) SaveModel(path string) error {
	log := util.GetLogger()
	if !fs.Init {
		log.Error("[FtrlSolver-SaveModel] Ftrl solver initialize error.")
		return errors.New("[FtrlSolver-SaveModel] Ftrl solver initialize error.")
	}

	file, err := os.Create(path)
	if err != nil {
		log.Error(fmt.Sprintf("[FtrlSolver-SaveModel] Ftrl solver save model error.%s", err.Error()))
		return errors.New(fmt.Sprintf("[FtrlSolver-SaveModel] Ftrl solver save model error.%s", err.Error()))
	}

	fs.Weights = make(util.Pvector, fs.Featnum)
	for i := 0; i < fs.Featnum; i++ {
		val := util.Round(fs.GetWeight(i), 5)
		fs.Weights[i] = util.Pair{i, val}
	}

	b, err2 := json.Marshal(fs)
	if err2 != nil {
		log.Error(fmt.Sprintf("[FtrlSolver-SaveModel] Ftrl solver save model error.%s", err2.Error()))
		return errors.New(fmt.Sprintf("[FtrlSolver-SaveModel] Ftrl solver save model error.%s", err2.Error()))
	}

	_, err = file.Write(b)
	if err != nil {
		log.Error(fmt.Sprintf("[FtrlSolver-SaveModel] Ftrl solver save model error.%s", err2.Error()))
		return errors.New(fmt.Sprintf("[FtrlSolver-SaveModel] Ftrl solver save model error.%s", err2.Error()))
	}

	return nil
}
Exemplo n.º 2
0
func (fs *FtrlSolver) SaveEncodeModel() (string, error) {
	log := util.GetLogger()
	if !fs.Init {
		log.Error("[FtrlSolver-SaveEncodeModel] Ftrl solver initialize error.")
		return "", errors.New("[FtrlSolver-SaveEncodeModel] Ftrl solver initialize error.")
	}

	fs.Weights = make(util.Pvector, fs.Featnum)
	for i := 0; i < fs.Featnum; i++ {
		val := util.Round(fs.GetWeight(i), 5)
		fs.Weights[i] = util.Pair{i, val}
	}

	b, err := json.Marshal(fs)
	if err != nil {
		log.Error(fmt.Sprintf("[FtrlSolver-SaveEncodeModel] Ftrl solver save model error.%s", err.Error()))
		return "", errors.New(fmt.Sprintf("[FtrlSolver-SaveEncodeModel] Ftrl solver save model error.%s", err.Error()))
	}

	return string(b), nil
}