コード例 #1
0
ファイル: xor.go プロジェクト: postfix/neural-go
func main() {
	const epsilon = 0.001
	training := [][3]neural.Float{{0.1, 0.1, 0.1}, {0.1, 0.9, 0.9}, {0.9, 0.1, 0.9}, {0.9, 0.9, 0.1}}
	net := neural.NewNetwork(2, 3, 1)
	epoch, worst := 0, neural.Float(epsilon)
	for ; worst >= epsilon; epoch++ {
		worst = 0.0
		for _, sample := range training {
			result := net.Activate(sample[0:2])
			net.Train(sample[0:2], sample[2:], 0.75, 0.5)
			err := neural.MeanSquaredError(result, sample[2:])
			if err > worst {
				worst = err
			}
		}
		if epoch%1000 == 0 {
			fmt.Println("Epoch #", epoch, "@ MSE =", worst)
		}
	}
	fmt.Println("Epoch #", epoch, "@ MSE =", worst)
	// fmt.Println(net)
	for _, sample := range training {
		result := net.Activate(sample[0:2])
		fmt.Println("Sample =", sample[0:2],
			" Expected =", sample[2],
			" Result =", result[0])
	}
	file, _ := os.Create("xor.json")
	net.Save(file)
}
コード例 #2
0
ファイル: mnist.go プロジェクト: postfix/neural-go
func main() {
	sourceLabelFile := flag.String("sl", "", "source label file")
	sourceImageFile := flag.String("si", "", "source image file")
	testLabelFile := flag.String("tl", "", "test label file")
	testImageFile := flag.String("ti", "", "test image file")
	dumpFile := flag.String("d", "mnist.json", "dump file")
	numSamples := flag.Int("n", -1, "number of samples (default=all)")
	flag.Parse()

	if *sourceLabelFile == "" || *sourceImageFile == "" {
		flag.Usage()
		os.Exit(-2)
	}

	fmt.Println("Loading training data...")
	labelData := ReadMNISTLabels(OpenFile(*sourceLabelFile))
	imageData, width, height := ReadMNISTImages(OpenFile(*sourceImageFile))

	var testLabelData []byte
	var testImageData [][]byte
	if *testLabelFile != "" && *testImageFile != "" {
		fmt.Println("Loading test data...")
		testLabelData = ReadMNISTLabels(OpenFile(*testLabelFile))
		testImageData, _, _ = ReadMNISTImages(OpenFile(*testImageFile))
	}

	var net *neural.Network
	if file, err := os.Open(*dumpFile); err != nil {
		fmt.Println("Creating network...")
		net = neural.NewNetwork(width*height, hiddenNodes, numLabels)
	} else {
		fmt.Println("Loading network...")
		net = neural.LoadNetwork(file)
	}

	input := make([]neural.Float, width*height)
	expected := make([]neural.Float, numLabels)

	epoch, worst, overall := 0, neural.Float(epsilon), neural.Float(0.0)
	for ; worst >= epsilon; epoch++ {
		worst, overall = 0.0, 0.0
		for i, labelIndex := range labelData {
			for j := 0; j < len(input); j++ {
				input[j] = pixelWeight(imageData[i][j])
			}
			for j := 0; j < len(expected); j++ {
				expected[j] = 0.1
				if j == int(labelIndex) {
					expected[j] = 0.9
				}
			}
			result := net.Activate(input)
			net.Train(input, expected, learningRate, momentum)
			err := neural.MeanSquaredError(result, expected)
			if err > worst {
				worst = err
			}
			overall += err
			if i%int(len(labelData)/100) == 0 {
				pctDone := int(float32(i) / float32(len(labelData)) * 100.0)
				if *numSamples > 0 {
					pctDone = int(float32(i) / float32(*numSamples) * 100.0)
				}
				fmt.Printf("\rEpoch #%d: %d%%, MSE = %.5f, worst = %.5f", epoch, pctDone, overall/neural.Float(i), worst)
			}
			if *numSamples > 0 && *numSamples == i {
				break
			}
		}

		correct, total := 0, len(testLabelData)
		if total > 0 {
			for i, labelIndex := range testLabelData {
				for j := 0; j < len(input); j++ {
					input[j] = pixelWeight(testImageData[i][j])
				}
				result := net.Activate(input)
				selected, maxValue := 0, neural.Float(-1.0)
				for j, value := range result {
					if value >= maxValue {
						selected = j
						maxValue = value
					}
				}
				if selected == int(labelIndex) {
					correct += 1
				}
				if *numSamples > 0 && *numSamples == i {
					break
				}
			}
		}

		fmt.Printf("\rEpoch #%d: done, MSE = %.5f, worst = %.5f", epoch, overall/neural.Float(len(labelData)), worst)
		if total > 0 {
			fmt.Printf(", correct = %.2f%%", float32(correct)/float32(total)*100.0)
		}
		fmt.Printf("\n")

		file, _ := os.Create(*dumpFile)
		net.Save(file)
	}
}