Пример #1
0
func main() {
	problem := golinear.NewProblem()
	problem.Add(golinear.TrainingInstance{0, golinear.FromDenseVector([]float64{1, 1, 1, 0, 0})})
	problem.Add(golinear.TrainingInstance{1, golinear.FromDenseVector([]float64{1, 0, 1, 1, 1})})

	param := golinear.DefaultParameters()
	model, err := golinear.TrainModel(param, problem)
	if err != nil {
		log.Fatal("Could not train the model: " + err.Error())
	}

	label := model.Predict(golinear.FromDenseVector([]float64{1, 1, 0, 0, 0}))

	fmt.Printf("Predicted label: %f\n", label)
}
Пример #2
0
func ExtractFeatures(dict Dictionary) (*golinear.Problem, ModelMetadata) {
	problem := golinear.NewProblem()
	//featureMap := make(map[string]int)
	featureMapping := make(map[string]int)
	tagMapping := make(map[string]int)

	var norm uint64 = 0
	for _, tags := range dict {
		for _, count := range tags {
			if count > norm {
				norm = count
			}
		}
	}

	for word, tags := range dict {
		featureVec := StringFeatureToFeature(ApplyTemplates(DefaultTemplates, word),
			featureMapping, float64(norm))

		for tag, count := range tags {
			id, found := tagMapping[tag]
			if !found {
				tagMapping[tag] = len(tagMapping)
			}

			id, _ = tagMapping[tag]

			for i := 0; i < int(count); i++ {
				var inst golinear.TrainingInstance

				inst.Features = featureVec
				inst.Label = float64(id)

				if err := problem.Add(inst); err != nil {
					panic(err)
				}
			}
		}
	}

	return problem, ModelMetadata{featureMapping, tagMapping, float64(norm)}
}