|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Helpers for getting default values for LitApp configurations.""" |
| 16 | +from typing import Union |
| 17 | +from lit_nlp.api import components as lit_components |
| 18 | +from lit_nlp.api import model as lit_model |
| 19 | +from lit_nlp.components import ablation_flip |
| 20 | +from lit_nlp.components import classification_results |
| 21 | +from lit_nlp.components import curves |
| 22 | +from lit_nlp.components import gradient_maps |
| 23 | +from lit_nlp.components import hotflip |
| 24 | +from lit_nlp.components import lemon_explainer |
| 25 | +from lit_nlp.components import lime_explainer |
| 26 | +from lit_nlp.components import metrics |
| 27 | +from lit_nlp.components import model_salience |
| 28 | +from lit_nlp.components import nearest_neighbors |
| 29 | +from lit_nlp.components import pca |
| 30 | +from lit_nlp.components import pdp |
| 31 | +from lit_nlp.components import projection |
| 32 | +from lit_nlp.components import regression_results |
| 33 | +from lit_nlp.components import salience_clustering |
| 34 | +from lit_nlp.components import scrambler |
| 35 | +from lit_nlp.components import shap_explainer |
| 36 | +from lit_nlp.components import tcav |
| 37 | +from lit_nlp.components import thresholder |
| 38 | +from lit_nlp.components import umap |
| 39 | +from lit_nlp.components import word_replacer |
| 40 | + |
| 41 | +ComponentGroup = lit_components.ComponentGroup |
| 42 | +Generator = lit_components.Generator |
| 43 | +Interpreter = lit_components.Interpreter |
| 44 | +Model = lit_model.Model |
| 45 | + |
| 46 | + |
| 47 | +def default_generators() -> dict[str, Generator]: |
| 48 | + """Returns a dict of the default generators used in a LitApp.""" |
| 49 | + return { |
| 50 | + 'Ablation Flip': ablation_flip.AblationFlip(), |
| 51 | + 'Hotflip': hotflip.HotFlip(), |
| 52 | + 'Scrambler': scrambler.Scrambler(), |
| 53 | + 'Word Replacer': word_replacer.WordReplacer(), |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | +def default_interpreters(models: dict[str, Model]) -> dict[str, Interpreter]: |
| 58 | + """Returns a dict of the default interpreters (and metrics) used in a LitApp. |
| 59 | +
|
| 60 | + Args: |
| 61 | + models: A dictionary of models that included in the LitApp that may provide |
| 62 | + thier own salience information. |
| 63 | + """ |
| 64 | + # Ensure the embedding-based interpreters are included. |
| 65 | + embedding_based_interpreters: dict[str, Interpreter] = { |
| 66 | + 'nearest neighbors': nearest_neighbors.NearestNeighbors(), |
| 67 | + # Embedding projectors expose a standard interface, but get special |
| 68 | + # handling so we can precompute the projections if requested. |
| 69 | + 'pca': projection.ProjectionManager(pca.PCAModel), |
| 70 | + 'umap': projection.ProjectionManager(umap.UmapModel), |
| 71 | + } |
| 72 | + gradient_map_interpreters: dict[str, Interpreter] = { |
| 73 | + 'Grad L2 Norm': gradient_maps.GradientNorm(), |
| 74 | + 'Grad ⋅ Input': gradient_maps.GradientDotInput(), |
| 75 | + 'Integrated Gradients': gradient_maps.IntegratedGradients(), |
| 76 | + 'LIME': lime_explainer.LIME(), |
| 77 | + } |
| 78 | + metrics_group: ComponentGroup = ComponentGroup({ |
| 79 | + 'regression': metrics.RegressionMetrics(), |
| 80 | + 'multiclass': metrics.MulticlassMetrics(), |
| 81 | + 'paired': metrics.MulticlassPairedMetrics(), |
| 82 | + 'bleu': metrics.CorpusBLEU(), |
| 83 | + 'rouge': metrics.RougeL(), |
| 84 | + }) |
| 85 | + # Ensure the prediction analysis interpreters are included. |
| 86 | + prediction_analysis_interpreters: dict[str, Interpreter] = { |
| 87 | + 'classification': classification_results.ClassificationInterpreter(), |
| 88 | + 'regression': regression_results.RegressionInterpreter(), |
| 89 | + } |
| 90 | + # pyformat: disable |
| 91 | + interpreters: dict[str, Union[ComponentGroup, Interpreter]] = { |
| 92 | + 'Model-provided salience': model_salience.ModelSalience(models), |
| 93 | + 'counterfactual explainer': lemon_explainer.LEMON(), |
| 94 | + 'tcav': tcav.TCAV(), |
| 95 | + 'curves': curves.CurvesInterpreter(), |
| 96 | + 'thresholder': thresholder.Thresholder(), |
| 97 | + 'metrics': metrics_group, |
| 98 | + 'pdp': pdp.PdpInterpreter(), |
| 99 | + 'Salience Clustering': salience_clustering.SalienceClustering( |
| 100 | + gradient_map_interpreters), |
| 101 | + 'Tabular SHAP': shap_explainer.TabularShapExplainer(), |
| 102 | + } |
| 103 | + # pyformat: enable |
| 104 | + interpreters.update(**gradient_map_interpreters, |
| 105 | + **prediction_analysis_interpreters, |
| 106 | + **embedding_based_interpreters) |
| 107 | + return interpreters |
0 commit comments