def test_calls_sigmoid_activation_one_time(self): with unittest.mock.patch( 'deepen.propagation.sigmoid', wraps = prop.sigmoid ) as sigmoid_spy: prop.model_forward(self.X, self.params) sigmoid_spy.assert_called_once()
def test_calls_relu_activation_L_minus_1_times(self): with unittest.mock.patch( 'deepen.propagation.relu', wraps = prop.relu ) as relu_spy: prop.model_forward(self.X, self.params) self.assertTrue(relu_spy.call_count == len(self.params) // 2 - 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 predict(self, X): """Calculate predictions using the trained model. Parameters ---------- X : ndarray Input data of shape (input size, number of examples). Returns ------- predictions : ndarray of {0 or 1} Model predictions for the given input. """ predictions, _ = prop.model_forward(X, self.params) return predictions.round()
def test_Y_hat_has_the_correct_shape(self): Y_hat, _ = prop.model_forward(self.X, self.params) self.assertTrue(Y_hat.shape == (1, self.X.shape[1]))
def test_cache_contains_L_caches(self): _, caches = prop.model_forward(self.X, self.params) self.assertTrue(len(caches) == len(self.params) // 2)