浏览代码

Implement benchmark

DricomDragon 5 年之前
父节点
当前提交
8e9fc3c688
共有 1 个文件被更改,包括 23 次插入4 次删除
  1. 23 4
      python/lab/benchmark.py

+ 23 - 4
python/lab/benchmark.py

@@ -1,12 +1,31 @@
 # Measure performance
 
-from lab import neural
+import numpy as np
+from lab import neural, io_mnist
 
-def computePrecision(network):
+def computePrecision(net):
 	"""
 	Test performance of provided network.
 
 	return : ratio of good answers in [0.0 ; 1.0]
 	"""
-	# TODO
-	return 0.0
+
+	np_images, np_expected = io_mnist.load_testing_samples()
+
+	nbSamples = np_images.shape[1]
+
+	nbSuccess = 0
+
+	for k in range(nbSamples):
+		a0 = np_images[:, k]
+
+		z1 = net.layer1 @ a0 + net.bias1
+		a1 = net.activationFunction(z1)
+
+		z2 = net.layer2 @ a1 + net.bias2
+		a2 = net.activationFunction(z2)
+
+		if (np.argmax(a2) == np.argmax(np_expected[:,k])):
+			nbSuccess += 1
+
+	return nbSuccess / nmSamples