Statistics of some scalar tensor. The value will be averaged over all given datapoints. Note that the average of accuracy over all batches is not necessarily the accuracy of the whole dataset. See :class:`ClassificationError` for details.
| 89 | |
| 90 | |
| 91 | class ScalarStats(Inferencer): |
| 92 | """ |
| 93 | Statistics of some scalar tensor. |
| 94 | The value will be averaged over all given datapoints. |
| 95 | |
| 96 | Note that the average of accuracy over all batches is not necessarily the |
| 97 | accuracy of the whole dataset. See :class:`ClassificationError` for details. |
| 98 | """ |
| 99 | |
| 100 | def __init__(self, names, prefix='validation'): |
| 101 | """ |
| 102 | Args: |
| 103 | names(list or str): list of names or just one name. The |
| 104 | corresponding tensors have to be scalar. |
| 105 | prefix(str): a prefix for logging |
| 106 | """ |
| 107 | if not isinstance(names, list): |
| 108 | self.names = [names] |
| 109 | else: |
| 110 | self.names = names |
| 111 | self.prefix = prefix |
| 112 | |
| 113 | def _before_inference(self): |
| 114 | self.stats = [] |
| 115 | |
| 116 | def _get_fetches(self): |
| 117 | return self.names |
| 118 | |
| 119 | def _on_fetches(self, output): |
| 120 | self.stats.append(output) |
| 121 | |
| 122 | def _after_inference(self): |
| 123 | if len(self.stats): |
| 124 | self.stats = np.mean(self.stats, axis=0) |
| 125 | assert len(self.stats) == len(self.names) |
| 126 | |
| 127 | ret = {} |
| 128 | for stat, name in zip(self.stats, self.names): |
| 129 | opname, _ = get_op_tensor_name(name) |
| 130 | name = '{}_{}'.format(self.prefix, opname) if self.prefix else opname |
| 131 | ret[name] = stat |
| 132 | return ret |
| 133 | |
| 134 | |
| 135 | class ClassificationError(Inferencer): |
no outgoing calls
no test coverage detected