Explorar el Código

Make the code more legible by adding comments

DricomDragon hace 5 años
padre
commit
2a1825c259
Se han modificado 5 ficheros con 28 adiciones y 3 borrados
  1. 3 0
      python/lab/benchmark.py
  2. 16 1
      python/lab/generator.py
  3. 2 0
      python/lab/io_mnist.py
  4. 4 1
      python/lab/neural.py
  5. 3 1
      python/lab/trainer.py

+ 3 - 0
python/lab/benchmark.py

@@ -7,6 +7,8 @@ def computePrecision(net):
 	"""
 	Test performance of provided network.
 
+	net : the network to be assessed
+
 	return : ratio of good answers in [0.0 ; 1.0]
 	"""
 
@@ -25,6 +27,7 @@ def computePrecision(net):
 		z2 = net.layer2 @ a1 + net.bias2
 		a2 = net.activationFunction(z2)
 
+		# Verify index of highest probability
 		if (np.argmax(a2) == np.argmax(np_expected[:,k])):
 			nbSuccess += 1
 

+ 16 - 1
python/lab/generator.py

@@ -5,12 +5,25 @@ import numpy as np
 
 # Random generators
 def uniform(layer):
+	"""
+	To generate network weights
+	with a uniform distribution between -1.0 and 1.0
+	"""
 	return np.random.uniform(low = -1.0, high = 1.0, size = layer.shape)
 
 def gaussUnitDev(layer):
+	"""
+	To generate network weights
+	with a gaussian distribution.
+	"""
 	return np.random.normal(size = layer.shape)
 
 def gaussAdaptedDev(layer):
+	"""
+	To generate network weights
+	with a gaussian distribution
+	where standard deviation is adpted 1 / sqrt(nl - 1)
+	"""
 	nl, _ = layer.shape
 	stdDev = 1 / np.sqrt(nl - 1)
 	return np.random.normal(scale = stdDev, size = layer.shape)
@@ -18,7 +31,9 @@ def gaussAdaptedDev(layer):
 # Network weight initialization
 def generate(activation, derivative, weightGenerator = None):
 	"""
-	Weight generator can be
+	activation : function used on network outputs
+	derivative : the derivative of the activation function
+	weightGenerator : is one of
 	None
 	generator.uniform
 	generator.gaussUnitDev

+ 2 - 0
python/lab/io_mnist.py

@@ -1,3 +1,5 @@
+# Load digit database
+
 from mnist import MNIST
 import numpy as np
 

+ 4 - 1
python/lab/neural.py

@@ -1,8 +1,11 @@
-# Hold the network attributes
+# Store a neuronal network
 
 import numpy as np
 
 class Network():
+	"""
+	Structure holding neural network attributes
+	"""
 
 	def __init__(self, activationFunction, activationDerivative):
 		self.inputLength = 784

+ 3 - 1
python/lab/trainer.py

@@ -4,17 +4,19 @@ import numpy as np
 from lab import neural, io_mnist
 from copy import copy
 
-def train(inputNetwork, learnRate, epochs, batchSize = 10):
+def train(inputNetwork, learnRate = 0.05, epochs = 2, batchSize = 10):
 	"""
 	Create an improved network
 
 	inputNetwork : the network to be trained
+	learnRate : speed of training, lower is slower but more precise
 	epochs : the number of iterations
 
 	return : a trained copy with improved performance
 	"""
 	net = copy(inputNetwork)
 
+	# Load data
 	np_images, np_expected = io_mnist.load_training_samples()
 
 	nbSamples = np_images.shape[1]