def test_correct_shape_for_biases(self): params = prop.initialize_params(self.layer_dims) for l in range(1, self.num_dims): with self.subTest(l = l): self.assertEqual( params['b' + str(l)].shape, (self.layer_dims[l], 1) )
def learn_generator(self, X, Y, iterations=3000, progress=1): """Train the model. This generator function yields the model and cost after each count of iterations given by `progress`. Parameters ---------- X : ndarray Input data of shape (input size, number of examples). Y : ndarray Vector of true values of shape (1, number of examples). iterations : int Maximum number of complete cycles of forward and back propagation to train the model. progress : int in [0, iterations] If non-zero, provide progress after each successive increment of the given number of iterations. Returns ------- tuple of (int, ndarray, list) i : int Number of completed iterations. params : dict of {str: ndarray} Current parameters for the trained model. `Wl` : ndarray Current weights matrix. `bl` : ndarray Current biases vector. cost : list The cost computed for the current iteration of training. """ # TODO: Throw error if progress < 0 or progress > iterations. params = prop.initialize_params(self.layer_dims) for i in range(1, iterations + 1): Y_hat, caches = prop.model_forward(X, params) cost = prop.compute_cost(Y_hat, Y) grads = prop.model_backward(Y_hat, Y, caches) params = prop.update_params(params, grads, self.learning_rate) self._params = params if progress and (i == 1 or i % progress == 0): yield (i, params, cost)
def test_biases_are_zero(self): params = prop.initialize_params(self.layer_dims) for l in range(1, self.num_dims): with self.subTest(l = l): self.assertFalse(params['b' + str(l)].any())
def test_weights_are_not_zero(self): params = prop.initialize_params(self.layer_dims) for l in range(1, self.num_dims): with self.subTest(l = l): self.assertTrue(params['W' + str(l)].all())
def test_returns_correct_number_of_params(self): number_of_params = 2 * (self.num_dims - 1) params = prop.initialize_params(self.layer_dims) self.assertEqual(len(params), number_of_params)