Esempio n. 1
0
func (self *Sequential) Train(data [][]float64, labels [][]float64, epochs int, batchSize int) {
	//
	fmt.Println("\n\n *** TRAINING...")
	// Set the regularisation parameter.
	L2REG := 0.0
	// Train for all epochs.
	for e := 0; e < epochs; e++ {
		// Create a random permutation of the data.
		randomPerm := rand.Perm(len(data))
		//
		error := 0.0
		accuracy := 0.0
		// Loop over the dataset.
		for d := 0; d < len(data); d++ {
			self.FeedForward(data[randomPerm[d]])
			error += self.CalculateCost(labels[randomPerm[d]], L2REG)
			self.BackPropagate(labels[randomPerm[d]], batchSize, (d+1)%batchSize == 0, L2REG)
			if utils.Argmax(self.layers[self.numLayers-1].Activations) == utils.Argmax(labels[randomPerm[d]]) {
				accuracy++
			}
			if (d+1)%500 == 0 {
				fmt.Printf("\tEpoch %vth / Progress: %.2f%% / Avg. Loss: %.4f / Accuracy %.2f%% \r", e+1, float64(d+1)*100/float64(len(data)), error/float64(d+1), 100.0*accuracy/float64(d+1))
			}
		}
		fmt.Printf("\r\n")
	}
}
Esempio n. 2
0
func (self *Sequential) Predict(data [][]float64, labels [][]float64) {
	fmt.Println(" *** PREDICTING TEST-DATA")
	accuracy := 0.0
	for d := 0; d < len(data); d++ {
		self.FeedForward(data[d])
		if utils.Argmax(self.layers[self.numLayers-1].Activations) == utils.Argmax(labels[d]) {
			accuracy++
		}
		if (d+1)%100 == 0 {
			fmt.Printf("\tProgress: %.2f%% / Accuracy %.2f%% \r", float64(d+1)*100/float64(len(data)), 100.0*accuracy/float64(d+1))
		}
	}

}