Esempio n. 1
0
def question_1h_sanity_check():
    """ Sanity check for highway.py
        designed by Kortez
    """
    test_model0 = highway.Highway(e_word=256)
    x_test0 = torch.zeros(BATCH_SIZE, 256)
    assert(test_model0.forward(x_test0).shape == x_test0.shape), \
    "Shape of forward() should be {} but is: {}".format(x_test0.shape, test_model.forward(x_test0).shape)
    print("-" * 80)
    print("Shape of forward() passed Sanity Check!")
    print("-" * 80)
    x_test1 = torch.tensor([[[-1, 3, 2, 0], [100, 10, 1, 0.1]],
                            [[1, 1, 1, 1], [0, 0, 0, 0]]])
    test_model1 = highway.Highway(e_word=4)
    test_model1.load_state_dict({'conv_out_to_proj_hidden.weight':torch.tensor([[1,-1,0,1],[1,-1,0,1],[1,-1,1,0],[1,-1,1,0]]), \
    'conv_out_to_proj_hidden.bias':torch.tensor([1,1,1,1]), \
    'conv_out_to_gate_hidden.weight':torch.tensor([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]), \
    'conv_out_to_gate_hidden.bias':torch.tensor([0,0,0,0])})
    gold_p1 = [[[-0.5, 1.5, 1, 0], [95.55, 50.55, 46.5, 46.05]],
               [[1.5, 1.5, 1.5, 1.5], [0.5, 0.5, 0.5, 0.5]]]
    out_p1 = [[[round(x.item(), 2) for x in y] for y in z]
              for z in test_model1.forward(x_test1)]
    assert (
        out_p1 == gold_p1), "Output of part1 should be {} but is: {}".format(
            gold_p1, out_p1)
    print("Part 1 passed Sanity Check!")
    print("-" * 80)
    x_test2 = torch.rand((3, 5, 4)) - torch.full((3, 5, 4), 0.5)
    test_model2 = highway.Highway(e_word=4)
    w_p, b_p = test_model2.state_dict(
    )['conv_out_to_proj_hidden.weight'], test_model2.state_dict(
    )['conv_out_to_proj_hidden.bias']
    w_g, b_g = test_model2.state_dict(
    )['conv_out_to_gate_hidden.weight'], test_model2.state_dict(
    )['conv_out_to_gate_hidden.bias']
    b_w_p = torch.unsqueeze(torch.t(w_p), 0)
    b_b_p = torch.unsqueeze(torch.unsqueeze(b_p, 0), 0)
    b_w_g = torch.unsqueeze(torch.t(w_g), 0)
    b_b_g = torch.unsqueeze(torch.unsqueeze(b_g, 0), 0)
    b_w_p = torch.cat((b_w_p, b_w_p, b_w_p), 0)
    b_b_p = torch.cat((b_b_p, b_b_p, b_b_p), 0)
    b_w_g = torch.cat((b_w_g, b_w_g, b_w_g), 0)
    b_b_g = torch.cat((b_b_g, b_b_g, b_b_g), 0)
    x_p = torch.nn.functional.relu(torch.bmm(x_test2, b_w_p) + b_b_p)
    x_g = torch.sigmoid(torch.bmm(x_test2, b_w_g) + b_b_g)
    print("+" * 10 + "Should be:" + "+" * 10)
    print(torch.mul(x_g, x_p) + torch.mul(1 - x_g, x_test2))
    print("+" * 10 + "Yours:" + "+" * 10)
    print(test_model2.forward(x_test2))
    print("End of Part 2!")
    print("Sanity Check Passed for Question 1h Highway!")
    print("-" * 80)
Esempio n. 2
0
def question_1f_sanity_check():
    """ Sanity check for highway.py
        basic shape check
    """
    print("-" * 80)
    print("Running Sanity Check for Question 1f: HIGHWAY CLASS ")
    print("-" * 80)

    inpt = torch.zeros(BATCH_SIZE, EMBED_SIZE)
    HIGH_source = highway.Highway(EMBED_SIZE)
    output = HIGH_source.forward(inpt)
    output_expected_size = [BATCH_SIZE, EMBED_SIZE]
    assert (
        list(output.size()) == output_expected_size
    ), "output shape is incorrect: it should be:\n {} but is:\n{}".format(
        output_expected_size, list(output.size()))
    print("Sanity Check Passed for Question 1f: HIGHWAY CLASS!")
    print("-" * 80)
Esempio n. 3
0
    def run(self, iteration_num, interval, duration, jam_start, jam_end):
        iteration = iteration_num
        acc_start = jam_start
        acc_stop = jam_end
        hwy = highway.Highway ()
        basemap = settings.UI_BASEMAP
        accident = "ON"                 # toggle between ON and OFF
        traffic_light = "ON"            # toggle between ON and OFF

        if interval == -1:
            traffic_light = "OFF"
        if jam_start == -1:
            accident = "OFF"

        traff_intv = interval
        traff_dura = duration
        for itr in range (iteration):

            if accident == "ON":
                # Interface for calling traffic accidents: hwy.update_states(itr, flag)
                # flag = 1: traffic accident; flag = 0: no accident
                if itr > acc_start or itr < acc_stop:
                    hwy.update_states(itr, 0.3, 1)
                else:
                    hwy.update_states(itr, 0.3, 0)
            
            if traffic_light == "ON":
                if itr % (traff_intv + traff_dura) == 0:
                    hwy.mergelane.e_prob1 = 0
                elif itr % (traff_intv + traff_dura) == traff_intv:
                    hwy.mergelane.e_prob1 = 0.7
                if itr == iteration - 1:
                    hwy.mergelane.e_prob1 = 0.5
            
            if accident == "OFF":
                hwy.update_states (itr, 0.3, 0)
            
            res1 = hwy.multiway.lanes
            res2 = hwy.mergelane.lanes
            res3 = hwy.exitway.lanes
            
            res = [[] for _ in range (8)]
            res[:5] = res1
            res[5:7] = res2
            res[7] = res3
            
            x = [[] for _ in range (8)]
            y = [[] for _ in range (8)]
            for i, lanex in enumerate (res):
                for j, c in enumerate (lanex.cells):
                    if c != None:
                        id = c.id
                        xi, yi = basemap[i][j]
                        x[id].append (xi)
                        y[id].append (yi)
            
            
            if itr % 30 == 0 or itr % 30 == 1 or itr % 30 == 2:
                print "\n----------------- The %dth Run: -----------------" % (itr)
                for i, lane in enumerate (res):
                    count = 0
                    v_sum = 0
                    for j, c in enumerate (lane.cells):
                        if c != None:
                            count += 1
                            v_sum += c.speed
                    print "Num of vehicles on Lane %d is %d" % (i, count)
                    print "Average speed of vehicles on Lane %d is %f" % (i, float(10*v_sum)/count)
                    
                    count_enter = 0
                    v_sum_enter = 0
                    count_exit = 0
                    v_sum_exit= 0
                    
                    if i in range(5):
                        a, b, c = 100, 151, 50
                    else:
                        a, b, c = 30, 51, 0
                    d = b - a - 1
                    
                    for j in range(a, b):
                        if lane.cells[j] != None:
                            count_enter += 1
                            v_sum_enter += lane.cells[j].speed
                        if lane.cells[c-j] != None:
                            count_exit += 1
                            v_sum_exit += lane.cells[c-j].speed
                    
                    print "Sparsity(at entrance): %f" % (float(count_enter)/d)
                    print "Sparsity(at exit): %f" % (float(count_exit)/d)
                    
                    if count_enter == 0:
                        print "No Entrance"
                    else:
                        print "Enter Speed (at entrance): %f" % (float(v_sum_enter)/count_enter)
            
                    if count_exit == 0:
                        print "No Exit"
                    else:
                        print "Exit Speed (at exit): %f" % (float (v_sum_exit)/count_exit)
                    
                    print
                    
            # send data to UI
            self.sendMessage (x, y, itr)
            if not self.masterMessageQueue.empty():
                return