def forward(self, V): self.in_act = V ## We shall use the technique of im2Col as detailed in the notes for the course ## CS231n. However, there are still speed-ups to be had if we could link ## numpy with the BLAS Library A = Vol(self.out_sx, self.out_sy, self.out_depth, 0.0) input_vol = V.w input_vol = input_vol.reshape(V.depth, V.sx, V.sy) inputim2col = im2col(input_vol, [self.filter_sx, self.filter_sy], self.stride) ##Create the filter matrix filtercol = np.zeros((self.out_depth,self.filter_sx*self.filter_sy*self.in_depth)) for i in xrange(self.out_depth): filtercol[i] = self.filters[i].w.flatten() ##Perform the convolution step convolve = np.dot(filtercol, inputim2col) ##Adding biases ##Could be done in a neater fashion for i in xrange(self.out_depth): convolve[i] += self.biases.w[i] A.w = convolve.flatten() self.out_act = A return self.out_act
def setUp(self): # Vols self.paris_newyork = Vol("Paris CDG", "New York", 6000, 500) self.paris_hongkong = Vol("Paris CDG", "Hong Kong", 9620, 200) # Avions disponible self.airbus_a350 = Avion() self.hawker_siddley_748 = Avion(reservoir=4000, nb_passenger=48, poids=21) self.boeing_737_max = Avion(reservoir=150000, nb_passenger=120, poids=118) self.boeing_737_max_2 = Avion(reservoir=150000, nb_passenger=140, poids=118)
def forward(self, V): """ Performs the forward pass. Straightforward implementation since every neuron connected to every neuron in the previous layer """ self.in_act = V input_weights = V.w output = Vol(1,1,self.out_depth, 0.0) for i in xrange(self.out_depth): a = 0.0 filter_weights = self.filters[i].w # a = np.dot(input_weights, filter_weights) + self.biases.w[i] for d in xrange(self.num_inputs): a += input_weights[d] * filter_weights[d] a += self.biases.w[i] output.w[i] = a self.out_act = output return self.out_act
def forward(self, V): self.in_act = V V2 = Vol(1,1,self.out_depth, 0.0) ##To prevent the exponentials from exploding, ##we follow the advice from the CS231n ##by subtracting the maximum weight ##thereby making the calculation stable V2_weights = V.w max_weight = np.amax(V2_weights) exponentials = np.exp(V2_weights - max_weight) denom = np.sum(exponentials) exponentials /= denom V2.w = exponentials ##We will use the exponentials for backprop self.exponentials = exponentials self.out_act = V2 return self.out_act
def step_given_vol(context, ville_depart, ville_arrivee, distance, nb_max_passagers): context.vol = Vol(ville_depart, ville_arrivee, distance, nb_max_passagers)
class MyTestCase(unittest.TestCase): def setUp(self): # Vols self.paris_newyork = Vol("Paris CDG", "New York", 6000, 500) self.paris_hongkong = Vol("Paris CDG", "Hong Kong", 9620, 200) # Avions disponible self.airbus_a350 = Avion() self.hawker_siddley_748 = Avion(reservoir=4000, nb_passenger=48, poids=21) self.boeing_737_max = Avion(reservoir=150000, nb_passenger=120, poids=118) self.boeing_737_max_2 = Avion(reservoir=150000, nb_passenger=140, poids=118) def test_add_passenger(self): # Arrange self.airbus_a350.set_passenger(10) # Act self.airbus_a350.add_passenger(10) # Assert self.assertEqual(20, self.airbus_a350.get_passenger()) # Act self.airbus_a350.add_passenger(15) # Assert self.assertEqual(35, self.airbus_a350.get_passenger()) def test_calcul_consommer(self): # Arrange self.airbus_a350.set_passenger(150) self.airbus_a350.set_poids(118) self.airbus_a350.set_reservoir(10000) # Act self.airbus_a350.add_vol(self.paris_newyork) # Assert self.assertAlmostEqual(1330, self.airbus_a350.calcul_consommation(), places=0) def test_calcul_distance_avec_plusieurs_vols(self): # Arrange self.airbus_a350.set_passenger(150) self.airbus_a350.set_poids(118) self.airbus_a350.set_reservoir(150000) # Act self.airbus_a350.add_vol(self.paris_newyork) self.airbus_a350.add_vol(self.paris_hongkong) # Assert self.assertAlmostEqual(11278.195, self.airbus_a350.calcul_distance(), places=3) def test_crash_distance(self): # Arrange self.hawker_siddley_748.add_vol(self.paris_newyork) # Assert self.assertEqual(0, self.hawker_siddley_748.calcul_distance()) def test_nb_passenger_autorise(self): # Arrange self.paris_newyork.add_avion(self.boeing_737_max) self.paris_newyork.add_avion(self.boeing_737_max_2) # Assert self.assertEqual(True, self.paris_newyork.is_max_passenger()) def test_nb_passenger_autorise_depasse(self): # Arrange self.paris_hongkong.add_avion(self.boeing_737_max) self.paris_hongkong.add_avion(self.boeing_737_max_2) # Assert self.assertEqual(False, self.paris_hongkong.is_max_passenger())