def main():
	shake = "docs/shakespeare-"
	txt = ".txt"
	twain = "docs/twain-"
	nn = nn_matrix.neural_network(6,2,[8],1, 0.5)
	prsr = ip.parser()
	shake_desired = np.array([[1], [0]])
	twain_desired = np.array([[0], [1]])
	input_shake = []
	input_twain = []
	for j in range(1, 6):
		input_shake.append(np.array([prsr.parse(shake+str(j)+txt)]).T)
		input_twain.append(np.array([prsr.parse(twain+str(j)+txt)]).T)

	print (input_shake)
	print(input_twain)
	for x in range(0, 1000):
		for i in range(0, 5):
			# input_shake = np.array([prsr.parse(shake+str(i)+txt)]).T
			# print("input_shake: ", input_shake)
			# input_twain = np.array([prsr.parse(twain+str(i)+txt)]).T
			# print("input_twain: ", input_twain)
			nn.output(input_shake[i])
			nn.back_prop(shake_desired)
			nn.output(input_twain[i])
			nn.back_prop(twain_desired)
	test_shake = prsr.parse(shake+"test1"+txt)
	test_twain = prsr.parse(twain+"test1"+txt)
	shake_out = nn.output(test_shake)
	twain_out = nn.output(test_twain)
	print("shake_out: ", shake_out)
	print("twain_out: ", twain_out)
def main():
	shake = "docs2/shakespeare/"
	twain = "docs2/twain/"
	dickens = "docs2/dickens/"
	txt = ".txt"
	nn = nn_matrix.neural_network(6,3,[8],1, .3)
	prsr = ip.parser()
	shake_desired = np.array([[1], [0], [0]])
	twain_desired = np.array([[0], [1], [0]])
	dickens_desired = np.array([[0], [0], [1]])
	input_shake = []
	input_twain = []
	input_dickens = []
	for j in range(1, 15):
		input_shake.append(np.array([prsr.parse(shake+str(j)+txt)]).T)
		input_twain.append(np.array([prsr.parse(twain+str(j)+txt)]).T)
		input_dickens.append(np.array([prsr.parse(dickens+str(j)+txt)]).T)

	# print (input_shake)
	# print(input_twain)
	# print(input_dickens)
	for x in range(0, 100):
		for i in range(0, 14):
			# input_shake = np.array([prsr.parse(shake+str(i)+txt)]).T
			# print("input_shake: ", input_shake)
			# input_twain = np.array([prsr.parse(twain+str(i)+txt)]).T
			# print("input_twain: ", input_twain)
			nn.output(input_shake[i])
			nn.back_prop(shake_desired)
			nn.output(input_twain[i])
			nn.back_prop(twain_desired)
			nn.output(input_dickens[i])
			nn.back_prop(dickens_desired)
	for a in range(14,19):
		stra = str(a)
		test_shake = prsr.parse(shake+stra+txt)
		test_twain = prsr.parse(twain+stra+txt)
		test_dickens = prsr.parse(dickens+stra+txt)
		shake_out = nn.output(test_shake)
		twain_out = nn.output(test_twain)
		dickens_out = nn.output(test_dickens)
		print("shake_out"+stra + ": ", test(shake_out))
		print("twain_out"+stra+": ", test(twain_out))
		print("dickens_out"+stra+": ", test(dickens_out))
		print("\n")
		print(shake_out)
		print(twain_out)
		print(dickens_out)
		print("\n")
Exemplo n.º 3
0
	def __init__(self, load):
		self.lastPercent = 0.0
		self.bestPercent = 0.0
		if (load != '0'):
			print("\ncreating train data...")
			sys.stdout.flush()
			self.td = train.train_data(files_location)
			pickle.dump(self.td, open("_train_data.p", "wb"))
			print("created sucessfully\n")
			sys.stdout.flush()
		else:
			print("\nloading train data...")
			sys.stdout.flush()
			self.td = pickle.load(open("_train_data.p", "rb"))
			print("loaded sucessfully\n")
			sys.stdout.flush()

		self.nn = nn_matrix.neural_network(len(self.td.inputs[0][0]),len(self.td.authors),nlayers,dummy_var, learn_rate)
def main():
	train_rounds = 500
	learn_rate = .5
	shake = "docs2/shakespeare/"
	twain = "docs2/twain/"
	dickens = "docs2/dickens/"
	doyle = "docs2/doyle/"
	milton = "docs2/milton/"
	txt = ".txt"
	nn = nn_matrix.neural_network(10,5,[20],1, learn_rate)
	prsr = ip.parser()
	shake_desired = 	np.array([[1], [0], [0], [0], [0]])
	twain_desired =		np.array([[0], [1], [0], [0], [0]])
	dickens_desired = 	np.array([[0], [0], [1], [0], [0]])
	doyle_desired = 	np.array([[0], [0], [0], [1], [0]])
	milton_desired = 	np.array([[0], [0], [0], [0], [1]])
	input_shake = []
	input_twain = []
	input_dickens = []
	input_doyle = []
	input_milton = []
	for j in range(1, 16):
		input_shake.append(np.array([prsr.parse(shake+str(j)+txt)]).T)
		input_twain.append(np.array([prsr.parse(twain+str(j)+txt)]).T)
		input_dickens.append(np.array([prsr.parse(dickens+str(j)+txt)]).T)
		input_doyle.append(np.array([prsr.parse(doyle+str(j)+txt)]).T)
		input_milton.append(np.array([prsr.parse(milton+str(j)+txt)]).T)


	for x in range(0, train_rounds):
		for i in range(0, 15):
			nn.output(input_shake[i])
			nn.back_prop(shake_desired)
			nn.output(input_twain[i])
			nn.back_prop(twain_desired)
			nn.output(input_dickens[i])
			nn.back_prop(dickens_desired)
			nn.output(input_doyle[i])
			nn.back_prop(doyle_desired)
			nn.output(input_milton[i])
			nn.back_prop(milton_desired)
	total_wrong = 0
	total_tests = 0
	for a in range(16,19):
		total_tests += 5
		stra = str(a)
		test_shake = prsr.parse(shake+stra+txt)
		test_twain = prsr.parse(twain+stra+txt)
		test_dickens = prsr.parse(dickens+stra+txt)
		test_doyle = prsr.parse(doyle+stra+txt)
		test_milton = prsr.parse(milton+stra+txt)
		shake_out = nn.output(test_shake)
		twain_out = nn.output(test_twain)
		dickens_out = nn.output(test_dickens)
		doyle_out = nn.output(test_doyle)
		milton_out = nn.output(test_milton)
		# print("test"+stra+":")
		if (test(shake_out) != "shakespeare"): 
			total_wrong += 1
			print("shake_out"+stra+": ", test(shake_out))
		if (test(twain_out) != "twain"): 
			total_wrong += 1
			print("twain_out"+stra+": ", test(twain_out))
		if (test(dickens_out) != "dickens"): 
			total_wrong += 1
			print("dikns_out"+stra+": ", test(dickens_out))
		if (test(doyle_out) != "doyle"): 
			total_wrong += 1
			print("doyle_out"+stra+": ", test(doyle_out))
		if (test(milton_out) != "milton"): 
			total_wrong +=1
			print("miltn_out"+stra+": ", test(milton_out))
		# print(shake_out)
		# print(twain_out)
		# print(dickens_out)
		# print(doyle_out)
		# print("\n")
	for a in range(19, 30):
		total_tests += 4
		stra = str(a)
		test_shake = prsr.parse(shake+stra+txt)
		test_twain = prsr.parse(twain+stra+txt)
		test_doyle = prsr.parse(doyle+stra+txt)
		test_milton = prsr.parse(milton+stra+txt)
		shake_out = nn.output(test_shake)
		twain_out = nn.output(test_twain)
		doyle_out = nn.output(test_doyle)
		milton_out = nn.output(test_milton)
		# print("test"+stra+":")
		if (test(shake_out) != "shakespeare"): 
			total_wrong += 1
			print("shake_out"+stra + ": ", test(shake_out))
		if (test(twain_out) != "twain"): 
			total_wrong += 1
			print("twain_out"+stra+": ", test(twain_out))
		if (test(doyle_out) != "doyle"): 
			total_wrong += 1
			print("doyle_out"+stra+": ", test(doyle_out))
		if (test(milton_out) != "milton"): 
			total_wrong +=1
			print("miltn_out"+stra+": ", test(milton_out))

	print("correct percentage:", 1 - total_wrong/total_tests)
	print("number wrong: ", total_wrong)
	print("number of tests:", total_tests)
	print("traing rounds:", train_rounds)
	print("learning rate:", learn_rate)