This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def plot_eli5_top_explanations( | |
| model: Model, | |
| image: np.array, | |
| class_names_mapping: Dict[int, str], | |
| top_preds_count: int = 3, | |
| fig_name: Optional[str] = None | |
| ) -> None: | |
| image_columns = 3 | |
| image_rows = math.ceil(top_preds_count / image_columns) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def plot_lime_top_explanations( | |
| model: Model, | |
| image: np.array, | |
| class_names_mapping: Dict[int, str], | |
| top_preds_count: int = 3, | |
| fig_name: Optional[str] = None | |
| ) -> None: | |
| image_columns = 3 | |
| image_rows = math.ceil(top_preds_count / image_columns) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def plot_shap_top_explanations( | |
| model: Model, | |
| image: np.array, | |
| class_names_mapping: Dict[int, str], | |
| top_preds_count: int = 3, | |
| fig_name: Optional[str] = None | |
| ) -> None: | |
| image_columns = 3 | |
| image_rows = math.ceil(top_preds_count / image_columns) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class FlattenLayer(Layer): | |
| def __init__(self): | |
| self._shape = () | |
| def forward_pass(self, a_prev: np.array, training: bool) -> np.array: | |
| self._shape = a_prev.shape | |
| return np.ravel(a_prev).reshape(a_prev.shape[0], -1) | |
| def backward_pass(self, da_curr: np.array) -> np.array: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def forward_pass(self, a_prev: np.array) -> np.array: | |
| self._a_prev = np.array(a_prev, copy=True) | |
| return np.dot(a_prev, self._w.T) + self._b | |
| def backward_pass(self, da_curr: np.array) -> np.array: | |
| n = self._a_prev.shape[0] | |
| self._dw = np.dot(da_curr.T, self._a_prev) / n | |
| self._db = np.sum(da_curr, axis=0, keepdims=True) / n | |
| return np.dot(da_curr, self._w) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MaxPoolLayer(Layer): | |
| def __init__(self, pool_size: Tuple[int, int], stride: int = 2): | |
| self._pool_size = pool_size | |
| self._stride = stride | |
| self._a = None | |
| self._cache = {} | |
| def forward_pass(self, a_prev: np.array, training: bool) -> np.array: | |
| self._a = np.array(a_prev, copy=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MaxPoolLayer(Layer): | |
| def __init__(self, pool_size: Tuple[int, int], stride: int = 2): | |
| self._pool_size = pool_size | |
| self._stride = stride | |
| self._a = None | |
| self._cache = {} | |
| def forward_pass(self, a_prev: np.array, training: bool) -> np.array: | |
| self._a = np.array(a_prev, copy=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def train( | |
| self, x_train: np.array, y_train: np.array, | |
| epochs: int, batch_size: int = 64, | |
| ) -> None: | |
| for epoch in range(epochs): | |
| for x_batch, y_batch in generate_batches(x_train, y_train, batch_size): | |
| y_hat_batch = self._forward(x_batch) | |
| activation = y_hat_batch - y_batch | |
| self._backward(activation) | |
| self._update() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def forward_pass(self, a_prev: np.array) -> np.array: | |
| n, h_in, w_in, _ = a_prev.shape | |
| _, h_out, w_out, _ = output_shape | |
| h_f, w_f, _, n_f = self._w.shape | |
| output_shape = self.calculate_output_dims(input_dims=a_prev.shape) | |
| output = np.zeros(output_shape) | |
| for i in range(h_out): | |
| for j in range(w_out): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def forward_pass(self, a_prev: np.array) -> np.array: | |
| self._shape = a_prev.shape | |
| n, h_in, w_in, c = a_prev.shape | |
| h_pool, w_pool = self._pool_size | |
| h_out = 1 + (h_in - h_pool) // self._stride | |
| w_out = 1 + (w_in - w_pool) // self._stride | |
| output = np.zeros((n, h_out, w_out, c)) | |
| for i in range(h_out): | |
| for j in range(w_out): |