Compute **true** classification error in batch mode, from a ``wrong`` tensor. The ``wrong`` tensor is supposed to be an binary vector containing whether each sample in the batch is *incorrectly* classified. You can use ``tf.nn.in_top_k`` to produce this vector. This Inferencer
| 133 | |
| 134 | |
| 135 | class ClassificationError(Inferencer): |
| 136 | """ |
| 137 | Compute **true** classification error in batch mode, from a ``wrong`` tensor. |
| 138 | |
| 139 | The ``wrong`` tensor is supposed to be an binary vector containing |
| 140 | whether each sample in the batch is *incorrectly* classified. |
| 141 | You can use ``tf.nn.in_top_k`` to produce this vector. |
| 142 | |
| 143 | This Inferencer produces the "true" error, which could be different from |
| 144 | ``ScalarStats('error_rate')``. |
| 145 | It takes account of the fact that batches might not have the same size in |
| 146 | testing (because the size of test set might not be a multiple of batch size). |
| 147 | Therefore the result can be different from averaging the error rate of each batch. |
| 148 | |
| 149 | You can also use the "correct prediction" tensor, then this inferencer will |
| 150 | give you "classification accuracy" instead of error. |
| 151 | """ |
| 152 | |
| 153 | def __init__(self, wrong_tensor_name='incorrect_vector', summary_name='validation_error'): |
| 154 | """ |
| 155 | Args: |
| 156 | wrong_tensor_name(str): name of the ``wrong`` binary vector tensor. |
| 157 | summary_name(str): the name to log the error with. |
| 158 | """ |
| 159 | self.wrong_tensor_name = wrong_tensor_name |
| 160 | self.summary_name = summary_name |
| 161 | |
| 162 | def _before_inference(self): |
| 163 | self.err_stat = RatioCounter() |
| 164 | |
| 165 | def _get_fetches(self): |
| 166 | return [self.wrong_tensor_name] |
| 167 | |
| 168 | def _on_fetches(self, outputs): |
| 169 | vec = outputs[0] |
| 170 | # TODO put shape assertion into inference-runner |
| 171 | assert vec.ndim == 1, "{} is not a vector!".format(self.wrong_tensor_name) |
| 172 | batch_size = len(vec) |
| 173 | wrong = np.sum(vec) |
| 174 | self.err_stat.feed(wrong, batch_size) |
| 175 | |
| 176 | def _after_inference(self): |
| 177 | return {self.summary_name: self.err_stat.ratio} |
| 178 | |
| 179 | |
| 180 | class BinaryClassificationStats(Inferencer): |
no outgoing calls
no test coverage detected