Exemplo n.º 1
0
                    val_x.append([right, left, operators[key], equals])
                    val_y.append([
                        temperature(b),
                        temperature(a), rightTarget, leftTarget
                    ])

train_x = np.array(train_x)
train_y = np.array(train_y)
val_x = np.array(val_x)
val_y = np.array(val_y)
test_x = np.array(test_x)
test_y = np.array(test_y)

model = LSTMModel(4, [10, 100], 784, 9)
model.train(train_x, train_y, val_x, val_y, 100, 5000)

results = model.predict(val_x)

count = 0

for index, result in enumerate(results):
    left = result[2]
    right = result[3]
    leftTarget = val_y[index][2]
    rightTarget = val_y[index][3]

    if is_temperature(left, leftTarget) and is_temperature(right, rightTarget):
        count += 1

test_count = 0
Exemplo n.º 2
0
            x.append([
                temperature(b) + [1, 0, 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
Exemplo n.º 3
0
            ])
            y.append([
                temperature(b),
                temperature(a),
                temperature((a + b) % 10),
                temperature((a + b) // 10)
            ])

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

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

model = LSTMModel(4, [20, 20], 12, 9)
model.train(x, y, x, y, 10, 5000)

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

count = 0

for index, result in enumerate(results):
    right = temperatureToInt(result[2])
    left = temperatureToInt(result[3])
    rightTarget = temperatureToInt(test_y[index][2])
    leftTarget = temperatureToInt(test_y[index][3])

    if left == leftTarget and right == rightTarget:
        count += 1
Exemplo n.º 4
0
tctoScores = []
tcfoScores = []
fctoScores = []
fcfoScores = []
epochs = []

k_fold_index = 0
bestModel = None
maxAcccuracy = 0
while k_fold_index < k:

    [train_x, train_y, val_x, val_y, test_x,
     test_y] = dataLoader.getData(k_fold_index, 4, 4, num_with_symbols)

    model = LSTMModel(4, [512, 512], 784, 50)
    epoch = model.train(train_x, train_y, val_x, val_y, 100, 200)
    epochs.append(epoch)

    results = model.predict(test_x)

    tcto = 0
    tcfo = 0
    fcto = 0
    fcfo = 0
    total = 0

    for index, result in enumerate(results):

        left = argmax(result[3][-20:-10])
        right = argmax(result[3][-10:])
        leftTarget = argmax(test_y[index][3][-20:-10])
Exemplo n.º 5
0
sys.dont_write_bytecode = True

from data import DataLoader
from lstm_model import LSTMModel
from feed_forward_model import FeedForwardModel

k = 5
results = []
total = 0

for i in range(k):

    dataLoader = DataLoader(src="MNIST_data/dataset")

    [
        noisyTrain, noisySymbolsTrain, targetsTrain, noisyValidation,
        noisySymbolsValidation, targetsValidation, noisyTest, noisySymbolsTest,
        targetsTest
    ] = dataLoader.getSequencialData(i, 2, 2)

    model = LSTMModel(2, [512, 512], 1568, 20)
    model.train(noisySymbolsTrain, targetsTrain, noisySymbolsValidation,
                targetsValidation, 100, 200)
    result = model.evaluate(noisySymbolsTest, targetsTest)

    results.append(result[1])
    total += result[1]

print(results)
print("SCORE: " + str(total / k))
Exemplo n.º 6
0
dataLoader = DataLoader(src="MNIST_data/dataset")

[
    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
Exemplo n.º 7
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))
Exemplo n.º 8
0
test_y = []
for x, y, r in sequances[75:]:
    test_x.append(x)
    test_y.append(r)

train_x = np.array(train_x)
train_y = np.array(train_y)
val_x = np.array(val_x)
val_y = np.array(val_y)
test_x = np.array(test_x)

print(len(train_x))
print(len(test_x))

model = LSTMModel(4, [512, 512, 512, 512], 784, 20)
model.train(train_x, train_y, val_x, val_y, 10, 200)

results = model.predict(test_x)
count = 0
total = 0

image = Image.new('RGB', (28 * 10, 28 * len(results)))
draw = ImageDraw.Draw(image)

for index, result in enumerate(results):
    left = argmax(result[3][:10])
    right = argmax(result[3][10:])

    if left * 10 + right == test_y[index]:
        count += 1
    total += 1
Exemplo n.º 9
0
test_y = []
test_result = []
for x, y, r in testSequances:
    test_x.append(x)
    test_y.append(y)
    test_result.append(r)

train_x = np.array(train_x)
train_y = np.array(train_y)
test_x = np.array(test_x)
test_y = np.array(test_y)

print(len(train_x))
print(len(test_x))

model = LSTMModel(3, [512, 512], 10, 20)
model.train(train_x, train_y, test_x, test_y, 10, 200)

results = model.predict(test_x)
count = 0
total = 0

for index, result in enumerate(results):
    left = argmax(result[2][:10])
    right = argmax(result[2][10:])

    if left * 10 + right == test_result[index]:
        count += 1
    total += 1

print("SCORE: " + str(count / float(total)))