bias_plot
#
Users can detect bias in a model by seeing which concepts certain vectors are closer to. This is a particularly useful tool when users are looking at semantic vectors and would like to check if certain words are leaning particularly towards any specific category.
An example of analysing gender bias inside Google’s Universal Sentence Encoder can be found below.
# Set up the encoder
!pip install -q sentence-transformers==2.2.0
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-mpnet-base-v2")
from relevanceai.utils.bias_detection import bias_indicator
bias_indicator(
anchors=["boy", "girl"],
values=["basketball", "draft", "skirt", "dress", "grave digger"],
encoder=model.encode
)
Module Contents#
- class bias_plot.BiasIndicator#
Class for all document utilities. Primarily should be used as a mixin for future functions but can be a standalone. # TODO: Extend to Chunk Doc Reading and Chunk Doc Writing
- displacement_from_unit_line(self, v)#
Distance from unit line
- remove_box_line(self, axes, edge: str)#
- bias_indicator_to_html(self, anchor_documents: List[Dict], documents: List[Dict], metadata_field: str, vector_field: str, marker_colors=['purple', '#2E8B86'], white_sep_space: float = 0.4, xlabel='L2 Distance From Neutrality Line', title: str = None)#
- bias_indicator(self, anchor_documents: List[Dict], documents: List[Dict], metadata_field: str, vector_field: str)#
Bias Indicator returns a 0 if it is not the most similar group and a displacement value from neutrality towards the most similar group.
- get_cosine_similarity_scores(self, documents: List[Dict[str, Any]], anchor_document: Dict[str, Any], vector_field: str) List[float] #
Compare scores based on cosine similarity
- Parameters
other_documents – List of documents (Python Dictionaries)
anchor_document – Document to compare all the other documents with.
vector_field – The field in the documents to compare
Example
>>> documents = [{...}] >>> client.get_cosine_similarity_scores(documents[1:10], documents[0])
- static calculate_cosine_similarity(a, b)#
- bias_plot.bias_indicator(anchors: List, values: List, encoder: Callable)#
Simple bias indicator based on vectors.
from relevanceai.utils.bias_detection import bias_indicator from sentence_transformers import SentenceTransformer model = SentenceTransformer("all-mpnet-base-v2") bias_indicator( anchors=["boy", "girl"], values=["basketball", "draft", "skirt", "dress", "grave digger"], encoder=model.encode )