Esempio n. 1
0
func main() {
	flagAddr := flag.String("addr", ":6060", "Listening address")
	flagCorpus := flag.String("corpus", "", "Corpus file in JSON format")
	flagStates := flag.Int("states", 2, "Number of hidden states")
	flagIter := flag.Int("iter", 20, "Number of EM iterations")
	flagModel := flag.String("model", "", "Model file in JSON format")
	flagLL := flag.String("logl", "", "Log-likelihood file")
	flagPProf := flag.Bool("pprof", false, "Output pprof file")
	flagParallel := flag.Bool("parallel", true, "Run multi-threading")
	flag.Parse()

	go func() {
		log.Println(http.ListenAndServe(*flagAddr, nil))
	}()

	var corpus []*core.Instance
	if f, e := os.Open(*flagCorpus); e != nil {
		log.Fatalf("Cannot open %s: %v", *flagCorpus, e)
	} else {
		defer f.Close()
		corpus = loader.LoadJSON(f)
		// Infer unexported fileds of Instance.
		for i, _ := range corpus {
			corpus[i].Index()
		}
	}

	C := core.EstimateC(corpus)
	baseline := core.Init(*flagStates, C, corpus, rand.New(rand.NewSource(99)))

	f := core.CreateFileOrStdout(*flagLL)
	if f != os.Stdout {
		defer f.Close()
	}

	if *flagPProf {
		defer profile.Start(profile.CPUProfile).Stop()
	}

	if *flagParallel {
		runtime.GOMAXPROCS(runtime.NumCPU())
	}

	model := core.Train(corpus, *flagStates, C, *flagIter, baseline, f)
	core.SaveModel(model, *flagModel)
}
Esempio n. 2
0
func Run(gen Generator) {
	flag.Parse()

	if csv, e := os.Open(*flagCSV); e != nil {
		log.Fatalf("Cannot open CSV file: %v", e)
	} else {
		defer csv.Close()

		if exps, e := LoadCSV(csv); e != nil {
			log.Fatalf("LoadCSV: %v", e)
		} else {
			if corpus := core.CreateFileOrStdout(*flagCorpus); e != nil {
				log.Printf("Cannot create corpus file: %v", e)
			} else {
				defer corpus.Close()
				GenerateJSON(exps, gen, corpus)
			}
		}
	}
}
Esempio n. 3
0
func main() {
	flagModel := flag.String("model", "", "Model file in JSON format")
	flagInstances := flag.Int("instances", 10, "# synthesized instances")
	flagLength := flag.Int("length", 10, "Length of each instance")
	flagCardi := flag.Int("cardi", 4, "Cardinality of multinomial outputs")
	flagCorpus := flag.String("corpus", "", "Synthetic corpus file")
	flagSeed := flag.Int("seed", 0, "Random if negative, seed otherwise")
	flag.Parse()

	var m *core.Model
	var e error
	if m, e = core.LoadModel(*flagModel); e != nil {
		log.Printf("Cannot load %s: %v. Use default model.", *flagModel, e)
		m = new(core.Model)
		if e := json.NewDecoder(
			strings.NewReader(defaultModel)).Decode(m); e != nil {
			log.Fatalf("Cannot decode default model")
		}
	}

	f := core.CreateFileOrStdout(*flagCorpus)
	if f != os.Stdout {
		defer f.Close()
	} else {
		log.Printf("Cannot create file %s. Use stdout", *flagCorpus)
	}

	seed := time.Now().UTC().UnixNano()
	if *flagSeed >= 0 {
		seed = int64(*flagSeed)
	}

	for _, inst := range m.Sample(*flagInstances, *flagLength, *flagCardi,
		rand.New(rand.NewSource(seed))) {
		if e := json.NewEncoder(f).Encode(inst); e != nil {
			log.Fatalf("Cannot JSON-encode corpus, %v", e)
		}
	}
}