Ejemplo n.º 1
0
	def test_mnist_28by28(self):
		import time
		import os
		import numpy as np
		import matplotlib.pyplot as plt
		from sklearn.cross_validation import train_test_split
		from sklearn.datasets import load_digits
		from sklearn.metrics import confusion_matrix, classification_report
		from sklearn.preprocessing import LabelBinarizer
		from ann import ANN

		# load lecun mnist dataset
		X = []
		y = []
		with open('data/mnist_test_data.txt', 'r') as fd, open('data/mnist_test_label.txt', 'r') as fl:
			for line in fd:
				img = line.split()
				pixels = [int(pixel) for pixel in img]
				X.append(pixels)
			for line in fl:
				pixel = int(line)
				y.append(pixel)
		X = np.array(X, np.float)
		y = np.array(y, np.float)

		# normalize input into [0, 1]
		X -= X.min()
		X /= X.max()

		# quick test
		#X = X[:1000]
		#y = y[:1000]

		# for my network
		X_test = X
		y_test = y #LabelBinarizer().fit_transform(y)

		nn = ANN([1,1])
		nn = nn.deserialize('28_200000.pickle') # '28_100000.pickle'

		predictions = []
		for i in range(X_test.shape[0]):
			o = nn.predict(X_test[i])
			predictions.append(np.argmax(o))

		# compute a confusion matrix
		print("confusion matrix")
		print(confusion_matrix(y_test, predictions))

		# show a classification report
		print("classification report")
		print(classification_report(y_test, predictions))
Ejemplo n.º 2
0
	def test_xor_trainig(self):
		print("test_xor_trainig...")
		nn = ANN([2, 2, 1])
		inputs = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]
		targets = [[0.0], [1.0], [1.0], [0.0]]
		predicts = []

		# train
		nn.train(40000, inputs, targets)

		for i in range(len(targets)):
			predicts.append(nn.predict(inputs[i]))

		# the prediction for 0,0 and 1,1 should be less than prediction for 0,1 and 1,0
		self.assertTrue(predicts[0] < predicts[1], 'xor relation1 not learned')
		self.assertTrue(predicts[0] < predicts[2], 'xor relation2 not learned')
		self.assertTrue(predicts[3] < predicts[1], 'xor relation3 not learned')
		self.assertTrue(predicts[3] < predicts[2], 'xor relation4 not learned')
Ejemplo n.º 3
0
    def anncv(Xtrain, Ytrain, Xtest, Ytest):
        inputsize = Xtrain.shape[1]
        hiddensize = 12
        outputsize = len(classes)
        ann = ANN(inputsize,hiddensize,outputsize)

        mtrain = len(Ytrain)
        # for this example, im only training with the first 12 examples
        for n in xrange(400):
            for i in xrange(mtrain):
                x,y = Xtrain[i],Ytrain[i]
                target = np.zeros(len(classes))
                target[classes.index(y)] = 1
                ann.train(x,target,alpha=0.1,momentum=0.2)
        
        mtest = len(Ytest)
        Ypredict = np.zeros(mtest)
        for i in xrange(mtest):
            x,y = Xtest[i],Ytest[i]
            results = ann.predict(x)
            prediction = results.argmax()
            Ypredict[i] = prediction
        return Ypredict
Ejemplo n.º 4
0
ylb.fit(y)


# split the dataset
X_train, X_test, y_train, y_test = train_test_split(xsc.transform(X), y, random_state=0)


# load nn if exists else train
nn_fname = 'models/nn_iris_3000epochs.pickle'
if os.path.exists(nn_fname):
    # load
    print('loading the nn')
    nn = deserialize(nn_fname)
else:
    # train
    print('training the nn')
    nn = ANN([4, 10, 3])

    nn.train(X_train, ylb.transform(y_train), 3000)

    serialize(nn, nn_fname)

# predict
preds = np.array([nn.predict(example) for example in X_test])
y_pred = ylb.inverse_transform(preds)

# evaluate
print(confusion_matrix(y_test, y_pred))

print(classification_report(y_test, y_pred))
Ejemplo n.º 5
0
	# see how long training takes
	startTime = time.time()

	# train it
	nn.train2(30, X_train_l, labels_train_l, 100000, step_cb)

	elapsedTime = time.time() - startTime
	print("Training took {0} seconds".format(elapsedTime))

	# serialize and save the ANN
	nn.serialize(nn, serialized_name)

# compute the predictions

predictions = []
for i in range(X_test.shape[0]):
	o = nn.predict(X_test[i])
	# the inverse of the binarization would be taking the maximum argument index
	# ex: [.1 .1 .1 .1 .9 .1 .1 .1 .1 .1] -> 4
	# ex: [.1 .1 .1 .1 .1 .1 .1 .9 .1 .1] -> 7
	predictions.append(np.argmax(o))

# compute a confusion matrix
print("confusion matrix")
print(confusion_matrix(y_test, predictions))

# show a classification report
print("classification report")
print(classification_report(y_test, predictions))
Ejemplo n.º 6
0
	def test_mnist_8by8_training(self):
		print("test_mnist_8by8_training")
		import time
		import numpy as np
		import matplotlib.pyplot as plt
		from sklearn.cross_validation import train_test_split
		from sklearn.datasets import load_digits
		from sklearn.metrics import confusion_matrix, classification_report
		from sklearn.preprocessing import LabelBinarizer
		from sklearn.metrics import precision_score, recall_score

		# import the simplified mnist dataset from scikit learn
		digits = load_digits()

		# get the input vectors (X is a vector of vectors of type int)
		X = digits.data

		# get the output vector ( y is a vector of type int)
		y = digits.target

		# normalize input into [0, 1]
		X -= X.min()
		X /= X.max()

		# split data into training and testing 75% of examples are used for training and 25% are used for testing
		X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=123)

		# binarize the labels from a number into a vector with a 1 at that index
		labels_train = LabelBinarizer().fit_transform(y_train)
		labels_test = LabelBinarizer().fit_transform(y_test)

		# convert from numpy to normal python list for our simple implementation
		X_train_l = X_train.tolist()
		labels_train_l = labels_train.tolist()

		# create the artificial neuron network with:
		# 1 input layer of size 64 (the images are 8x8 gray pixels)
		# 1 hidden layer of size 100
		# 1 output layer of size 10 (the labels of digits are 0 to 9)
		nn = ANN([64, 100, 10])

		# see how long training takes
		startTime = time.time()

		# train it
		nn.train(10, X_train_l, labels_train_l)

		elapsedTime = time.time() - startTime
		print("time took " + str(elapsedTime))
		self.assertTrue(elapsedTime < 300, 'Training took more than 300 seconds')

		# compute the predictions
		predictions = []
		for i in range(X_test.shape[0]):
			o = nn.predict(X_test[i])
			predictions.append(np.argmax(o))

		# compute a confusion matrix
		# print(confusion_matrix(y_test, predictions))
		# print(classification_report(y_test, predictions))

		precision = precision_score(y_test, predictions, average='macro')
		print("precision", precision)
		recall = recall_score(y_test, predictions, average='macro')
		print("recall", recall)

		self.assertTrue(precision > 0.93, 'Precision must be bigger than 93%')
		self.assertTrue(recall > 0.93, 'Recall must be bigger than 93%')
Ejemplo n.º 7
0
	# serialize and save the ANN
	nn.serialize(nn, serialized_name)

	# plot error over time
	#plt.plot(step, train_error, 'b--', step, validation_err, 'gs', t, t**3, 'g^')
	#plt.plot(step, train_error, 'b--', step, validation_err, 'g')
	plt.plot(steps, train_error, 'b--', label="Training Error")
	plt.plot(steps, validation_err, 'g', label="Validation Error")
	plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.)
	plt.title('Training Error vs Validation Error')
	plt.show()

# compute the predictions
predictions = []
for i in range(X_test.shape[0]):
	o = nn.predict(X_test[i])
	# the inverse of the binarization would be taking the maximum argument index
	# ex: [.1 .1 .1 .1 .9 .1 .1 .1 .1 .1] -> 4
	# ex: [.1 .1 .1 .1 .1 .1 .1 .9 .1 .1] -> 7
	predictions.append(np.argmax(o))

# compute a confusion matrix
print("confusion matrix")
print(confusion_matrix(y_test, predictions))

# show a classification report
print("classification report")
print(classification_report(y_test, predictions))

# visualize
show_training_digits(digits)