Run this component, given a model and input(s).
(
self,
inputs: list[JsonDict],
model: lit_model.Model,
dataset: lit_dataset.Dataset,
model_outputs: Optional[list[JsonDict]] = None,
config: Optional[JsonDict] = None,
)
| 166 | self._seed: str = str(seed) if seed is not None else '' |
| 167 | |
| 168 | def run( |
| 169 | self, |
| 170 | inputs: list[JsonDict], |
| 171 | model: lit_model.Model, |
| 172 | dataset: lit_dataset.Dataset, |
| 173 | model_outputs: Optional[list[JsonDict]] = None, |
| 174 | config: Optional[JsonDict] = None, |
| 175 | ) -> Optional[list[JsonDict]]: |
| 176 | """Run this component, given a model and input(s).""" |
| 177 | config_defaults = {k: v.default for k, v in self.config_spec().items()} |
| 178 | config = dict(config_defaults, **(config or {})) # update and return |
| 179 | |
| 180 | kernel_width = int(config[KERNEL_WIDTH_KEY]) |
| 181 | num_samples = int(config[NUM_SAMPLES_KEY]) |
| 182 | mask_string = (config[MASK_KEY]) |
| 183 | # pylint: disable=g-explicit-bool-comparison |
| 184 | seed = int(config[SEED_KEY]) if config[SEED_KEY] != '' else None |
| 185 | # pylint: enable=g-explicit-bool-comparison |
| 186 | |
| 187 | # Find keys of input (text) segments to explain. |
| 188 | # Search in the input spec, since it's only useful to look at ones that are |
| 189 | # used by the model. |
| 190 | text_keys = utils.find_spec_keys(model.input_spec(), types.TextSegment) |
| 191 | if not text_keys: |
| 192 | logging.warning('LIME requires text inputs.') |
| 193 | return None |
| 194 | logging.info('Found text fields for LIME attribution: %s', str(text_keys)) |
| 195 | |
| 196 | available_pred_keys = utils.find_spec_keys( |
| 197 | model.output_spec(), _SUPPORTED_PRED_TYPES |
| 198 | ) |
| 199 | if not available_pred_keys: |
| 200 | logging.warning('LIME did not find any supported output fields.') |
| 201 | return None |
| 202 | |
| 203 | if (field := config[TARGET_HEAD_KEY]) and ( |
| 204 | cls_idx := int(config[CLASS_KEY]) |
| 205 | ) != -1: |
| 206 | # TODO(b/205996131): remove this case |
| 207 | pred_key = field |
| 208 | provided_class_to_explain = cls_idx |
| 209 | elif target_config := config.get(TARGET_INFO_KEY): |
| 210 | pred_key = target_config['field'] |
| 211 | if pred_key not in available_pred_keys: |
| 212 | logging.warning("LIME is not compatible with field '%s'", pred_key) |
| 213 | return None |
| 214 | # May be None, if there's no label vocab. |
| 215 | provided_class_to_explain = target_config.get('index') |
| 216 | else: |
| 217 | pred_key = available_pred_keys[0] |
| 218 | provided_class_to_explain = None # use model prediction |
| 219 | |
| 220 | pred_type_info = model.output_spec()[pred_key] |
| 221 | all_results = [] |
| 222 | |
| 223 | # Explain each input. |
| 224 | for example in inputs: |
| 225 | # dict[field name -> interpretations] |
nothing calls this directly
no test coverage detected