Пример #1
0
func main() {
	flag.Parse()
	gen, err := poem.NewGenerator("data/quantangshi3000.int")
	if err != nil {
		log.Fatalf("%v", err)
	}
	h1Size := 512
	numHeads := 8
	n := 128
	m := 32
	c := ntm.NewEmptyController1(gen.InputSize(), gen.OutputSize(), h1Size, numHeads, n, m)
	assignWeights(c)

	p := [][]string{
		{"红", "", "", "", ""},
		{"春", "", "", "", ""},
		{"愿", "", "", "", ""},
		{"此", "", "", "", ""},
	}
	//p := [][]string{
	//  {"阿", "", "", "", ""},
	//  {"扁", "", "", "", ""},
	//  {"无", "", "", "", ""},
	//  {"罪", "", "", "", ""},
	//}
	//p := [][]string{
	//  {"九", "", "", "", "", "", ""},
	//  {"二", "", "", "", "", "", ""},
	//  {"共", "", "", "", "", "", ""},
	//  {"识", "", "", "", "", "", ""},
	//}
	//p := [][]string{
	//  {"十", "", "", "", ""},
	//  {"四", "", "", "", ""},
	//  {"日", "", "", "", ""},
	//  {"罢", "", "", "", ""},
	//  {"免", "", "", "", ""},
	//  {"蔡", "", "", "", ""},
	//  {"正", "", "", "", ""},
	//  {"元", "", "", "", ""},
	//}
	//p := [][]string{
	//	{"全", "", "", "", "", "", ""},
	//	{"力", "", "", "", "", "", ""},
	//	{"支", "", "", "", "", "", ""},
	//	{"持", "", "", "", "", "", ""},
	//	{"柯", "", "", "", "", "", ""},
	//	{"匹", "", "", "", "", "", ""},
	//	{"文", "", "", "", "", "", ""},
	//	{"哲", "", "", "", "", "", ""},
	//}
	rand.Seed(15)
	pred := predict(c, p, gen)
	showPrediction(pred, gen, p)
}
Пример #2
0
func main() {
	flag.Parse()
	blas64.Use(cgo.Implementation{})

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	http.HandleFunc("/Weights", func(w http.ResponseWriter, r *http.Request) {
		c := make(chan []byte)
		weightsChan <- c
		w.Write(<-c)
	})
	http.HandleFunc("/Loss", func(w http.ResponseWriter, r *http.Request) {
		c := make(chan []float64)
		lossChan <- c
		json.NewEncoder(w).Encode(<-c)
	})
	http.HandleFunc("/PrintDebug", func(w http.ResponseWriter, r *http.Request) {
		printDebugChan <- struct{}{}
	})
	port := 8085
	go func() {
		log.Printf("Listening on port %d", port)
		if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
			log.Fatalf("%v", err)
		}
	}()

	var seed int64 = 5
	rand.Seed(seed)
	log.Printf("seed: %d", seed)

	gen, err := poem.NewGenerator("data/quantangshi3000.int")
	if err != nil {
		log.Fatalf("%v", err)
	}
	h1Size := 512
	numHeads := 8
	n := 128
	m := 32
	c := ntm.NewEmptyController1(gen.InputSize(), gen.OutputSize(), h1Size, numHeads, n, m)
	weights := c.WeightsVal()
	for i := range weights {
		weights[i] = 1 * (rand.Float64() - 0.5)
	}

	losses := make([]float64, 0)
	doPrint := false

	rmsp := ntm.NewRMSProp(c)
	log.Printf("numweights: %d", len(c.WeightsVal()))
	var bpcSum float64 = 0
	for i := 1; ; i++ {
		x, y := gen.GenSeq()
		machines := rmsp.Train(x, &ntm.MultinomialModel{Y: y}, 0.95, 0.5, 1e-3, 1e-3)

		numChar := len(y) / 2
		l := (&ntm.MultinomialModel{Y: y[numChar+1:]}).Loss(ntm.Predictions(machines[numChar+1:]))
		bpc := l / float64(numChar)
		bpcSum += bpc

		acc := 100
		if i%acc == 0 {
			bpc := bpcSum / float64(acc)
			bpcSum = 0
			losses = append(losses, bpc)
			log.Printf("%d, bpc: %f, seq length: %d", i, bpc, len(y))
		}

		handleHTTP(c, losses, &doPrint)

		if i%10 == 0 && doPrint {
			printDebug(y, machines)
		}
	}
}