Example #1
0
                temperature(a) + [1, 0, 0],
                temperature(0) + [0, 1, 0],
                temperature(0) + [0, 0, 1]
            ])
            y.append([[b / 10.0], [a / 10.0], [((a + b) % 10) / 10.0],
                      [((a + b) // 10) / 10.0]])

x = np.array(x)
y = np.array(y)
test_x = np.array(test_x)
test_y = np.array(test_y)

model = LSTMModel(4, [512, 512], 12, 1)
model.train(x, y, test_x, test_y, 10, 5000)

results = model.predict(x)

count = 0

for index, result in enumerate(results):
    left = round(result[2][0], 1)
    right = round(result[3][0], 1)
    leftTarget = round(y[index][2][0], 1)
    rightTarget = round(y[index][3][0], 1)

    if left == leftTarget and right == rightTarget:
        count += 1

test_count = 0

test_results = model.predict(test_x)
Example #2
0
print("Test dataset has {} samples.".format(*x_test.shape))
# print(x_test[:3])
# print(y_test[:3])

# Build & train model
model = LSTMModel(timesteps, hidden_neurons).build()
print("Fitting the model...")
history = model.fit(x_train, y_train,
                    batch_size=batchsize, epochs=epochs, validation_data=(x_val, y_val))

# Output training history to csv
history_df = pd.DataFrame(history.history)
history_df.to_csv("training_history.csv")

print("Predicting...")
result = model.predict(x_test)
predicted = pd.DataFrame(result)
predicted.columns = ['predicted_nikkei']
predicted['actual_nikkei'] = y_test

print("Completed Prediction.")
print(predicted.shape)
print(predicted[:10])
# Output to csv
predicted.to_csv("predicted.csv")

# Evaluate
evaluation_score = model.evaluate(x_test, y_test, batch_size=batchsize, verbose=1)
print("Evaluation score is {}".format(evaluation_score))

# Evaluate benchmark
Example #3
0
[
    noisyTrain,
    noisySymbolsTrain,
    targetsTrain,
    noisyValidation,
    noisySymbolsValidation,
    targetsValidation,
    noisyTest,
    noisySymbolsTest,
    targetsTest
] = dataLoader.getSequencialDataWithDontCare(0, 2, 2)

model = LSTMModel(2, [512, 512, 512], 1568, 20)
model.train(noisyTrain, targetsTrain, noisyValidation, targetsValidation, 100, 100)
result = model.evaluate(noisyTest, targetsTest)
predictions = model.predict(noisyTest)
count = 0
for index in range(len(predictions)):
    a = utils.argmax(predictions[index][:10])
    b = utils.argmax(predictions[index][10:])
    predicted = a * 10 + b

    a = utils.argmax(targetsTest[index][:10])
    b = utils.argmax(targetsTest[index][10:])
    actual = a * 10 + b

    if actual == predicted:
        count += 1


print("ACCURACY: " + str(result[1]))
            ])
            y.append([
                one_hot(c),
                one_hot(b),
                one_hot(a),
                one_hot((a + b + c) % 10),
                one_hot((a + b + c) // 10)
            ])

x = np.array(x)
y = np.array(y)

model = LSTMModel(5, [10, 10], 12, 12)
model.train(x, y, x, y, 10, 5000)

results = model.predict(x)
activations_0 = model.getActivations(0, x)
activations_1 = model.getActivations(1, x)

count = 0

for index, result in enumerate(results):
    left = argmax(result[3])
    right = argmax(result[4])
    leftTarget = argmax(y[index][3])
    rightTarget = argmax(y[index][4])

    if left == leftTarget and right == rightTarget:
        count += 1

for i in range(10):
Example #5
0
def main():
	"""Program execution starts here."""
	intersections = read_intersections("intersections.data")
	intersection_list = []
	
	for i in range(len(intersections)):
		intersection_list.append(intersections[i]['name'].strip(' '))
		
	#sel_intersection = intersections[0]['name']
	sel_intersection = "4589"

	df = get_dataset(sel_intersection, intersections)
	x_train, x_test, y_train, y_test = preprocessing(df)

	model_file = search(intersections, 'name', sel_intersection)

	main_menu = [
		"CAPSTONE TRAFFIC PREDICTION",

		"Select Intersection",
		"List of Intersections",
		"Train Intersection",
		"Test Intersection (Accuracy)",
		"Route Check"
	]
	train_menu = [
		"Train Mode:",

		"Train from scratch w/ events",
		"Train from file w/ events",
		"Train from scratch",
		"Train from file",
	]
	route_check_menu = [
		"Train Mode:",

		"Train from scratch w/ events",
		"Train from file w/ events",
		"Train from scratch",
		"Train from file",
	]

	while True:
		print("Currently Selected Intersection:", sel_intersection)
		choice = menu.do_menu(main_menu)
		model = LSTMModel(x_train.shape[1], y_train.shape[1])
		if choice is None:
			return  # Exit main() (and program).
		if choice == 1:
			# Select Intersection
			temp_menu = ["Please Select a New Intersection"]

			for line in intersections:
				option = line["name"] + ": " + line["street"]
				temp_menu.append(option)

			choice = menu.do_menu(temp_menu)
			if choice is not None:
				sel_intersection = intersections[choice - 1]['name']
				print(sel_intersection, "set as current intersection.")

				df = get_dataset(sel_intersection, intersections)
				x_train, x_test, y_train, y_test = preprocessing(df)

				model_file = search(intersections, 'name', sel_intersection)
				if model_file is not None:
					model_file = intersections[model_file]['model']
					#try:
					model.load_network('./model/'+str(sel_intersection)+'.hdf')
					#except:
					#	print("File for loading model is not found! Creating a new model from scratch")
					#	model.init_network(hidden_size=50)

		elif choice == 2:
			# List Intersections
			print_intersections(intersections)
		elif choice == 3:
			model = LSTMModel(x_train.shape[1], y_train.shape[1])
			# Train Intersections
			# TODO test each option
			choice = menu.do_menu(train_menu)
			if choice == 1:
				x_train, x_test, y_train, y_test = preprocessing(df, events=True)
				model = LSTMModel(x_train.shape[1], y_train.shape[1])

				intersection_idx = search(intersections, 'name', sel_intersection)
				model_file = "model/" + sel_intersection + ".hdf"
				#if os.path.exists(model_file):
					#os.remove(model_file)
				model.init_network(hidden_size=50)
			elif choice == 2:
				x_train, x_test, y_train, y_test = preprocessing(df, events=True)
				model = LSTMModel(x_train.shape[1], y_train.shape[1])

				intersection_idx = search(intersections, 'name', sel_intersection)
				model_file = "model/" + sel_intersection + ".hdf"
				if os.path.exists(model_file):
					model.load_network(model_file)
				else:
					print("Model does not exist, starting from scratch")
					model.init_network(hidden_size=50)
			elif choice == 3:
				x_train, x_test, y_train, y_test = preprocessing(df, events=False)
				model = LSTMModel(x_train.shape[1], y_train.shape[1])

				intersection_idx = search(intersections, 'name', sel_intersection)
				model_file = "model/" + sel_intersection + ".hdf"
				if os.path.exists(model_file):
					os.remove(model_file)
				model.init_network(hidden_size=50)
			elif choice == 4:
				x_train, x_test, y_train, y_test = preprocessing(df, events=False)
				model = LSTMModel(x_train.shape[1], y_train.shape[1])

				intersection_idx = search(intersections, 'name', sel_intersection)
				model_file = "model/" + sel_intersection + ".hdf"
				if os.path.exists(model_file):
					model.load_network(model_file)
				else:
					print("Model does not exist, starting from scratch")
					model.init_network(hidden_size=50)
			try:
				e = int(input("Enter Epochs to train for (Default=50): "))
				model.epochs = e
			except ValueError:
				print("Invalid number entered, using default value")
			model.train(x_train, y_train, './'+ model_file)

			intersections[intersection_idx]['model'] = model_file
			save_intersections(intersections, "intersections.data")

		elif choice == 4:
			# Test Intersections
			model_file = "model/" + sel_intersection + ".hdf"
			model = LSTMModel(x_train.shape[1], y_train.shape[1])
			if os.path.exists(model_file):
				model.load_network(model_file)
				test_output = model.get_accuracy(x_test, y_test)
				N, S, E, W = confusion_matrix(test_output, y_test, True)
			else:
				print("Please train intersection first")

		elif choice == 5:
			model = LSTMModel(x_train.shape[1], y_train.shape[1], intersection_list)
			# Route Check
			x_data = []
			day_week = [1,2,3,4,5,6,7]
			season=[1,2,3,4]
			time_str = ''
			flag = 0
			x_data = []
			peak = 0

			while flag == 0:
				num_inter = input("Please enter intersection number: ")
				if num_inter in intersection_list:
					flag = 1
					num_inter = int(num_inter)
					#x_data.append(int(num_inter))
				else:
					print("Intersection not found!")
					
			flag = 0
			while flag == 0:
				week = [0,0,0,0,0,0,0]
				num_day = input("Please enter the day of the week:\nOptions:\n1:Sunday\n2:Monday\n3:Tuesday\n4:Wednesday\n5:Thursday\n6:Friay\n7:Saturday\n")
				num_day = int(num_day)
				if num_day in day_week:
					flag = 1
					week[num_day-1] = 1
					x_data = x_data + week
				else:
					print("Day of the week not found!")
			
			flag = 0
			while flag == 0:
				time_day = input("Please enter the time of the day (ex. 17:30): ")
				temp = time_day.split(':')
				if len(temp) == 2:
					hour = int(temp[0]) * 60
					if hour > 9 and hour < 17:
						peak = 1
					time = hour + int(temp[1])
					time_d = float(time) / float(1440)
					if time_d > 1.0:
						print("Please enter time in the proper format!")
					else:
						x_data.append(time_d)
						flag = 1
				else:
					print("Please enter time in the proper format!")
			
			flag = 0
			while flag == 0:
				seasons = [0,0,0,0]
				season_input = input("Please enter the season:\n1:Summer\n2:Fall\n3:Winter\n4:Spring\n")
				season_input = int(season_input)
				
				if season_input in season:
					flag = 1
					seasons[season_input-1] = 1
					x_data = x_data + seasons
				else:
					print("Season not found!")
			
			# add [0,0] for events
			x_data.append(peak)
			x_data = x_data + [0,0]
			x_test = np.array([x_data])
			x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
			res = model.predict(x_test, num_inter)
			print("Prediction: " + str(res))