Beispiel #1
0
def knn(image, method='euclidean', k=3):
	train_samples = MNIST.data['train']
	np.random.shuffle(train_samples)
	image = MNIST.flatten_image(image)

	nearest_dist = [np.inf for _ in range(k)]
	nearest_lbls = [-1 for _ in range(k)]

	for sample in train_samples[0:999]:
		candidate = MNIST.flatten_image(sample['image'])
		dist = distances(image, candidate, method)

		if dist < np.max(nearest_dist):
			max_idx = np.argmax(nearest_dist)
			nearest_dist[max_idx] = dist
			nearest_lbls[max_idx] = sample['label']

	return np.argmax(np.bincount(nearest_lbls))
Beispiel #2
0
		self.terminal = sys.stdout
		self.log = open(time.strftime("./Logs/%Y%m%d-%H%M%S-logfile.log"), "a")		

	def write(self, message):
		self.terminal.write(message)
		self.log.write(message)  

	def flush(self):
		self.log.flush()
		self.terminal.flush()
		pass

sys.stdout = Logger()

train_count = 60000
train_labels = MNIST.label(*range(train_count), setname='train', decode=True)
train_samples = MNIST.image(*range(train_count), setname='train', flat=True, normalize=True)

test_count = 9000
test_labels = MNIST.label(*range(test_count), setname='test', decode=True)
test_samples = MNIST.image(*range(test_count), setname='test', flat=True, normalize=True)

mlp = MLP([784, 30, 10])

epoch_count = 10
train_errors = np.zeros(epoch_count)
test_errors = np.zeros(epoch_count)

begin_time = time.time()
print(time.strftime("Began training at %Y%m%d-%H%M%S"))
print()