Visualizing Bias in Data using Embedding Projector
Using Open-Sourced Embedding Projector tool for Interactive Visualization and Interpretation of Embeddings
Debug your data before debugging your model
Before a machine learning model is deployed, its performance is evaluated. However, apart from the performance aspect, it is also important to know what the model has learnt. This is necessary so as to ensure that the model hasn’t learnt something discriminatory or biased.
One way of approaching this problem is from a data visualisation perspective. By visualising how the model groups the data, we can get an idea of what the model thinks are similar and dissimilar data points. This is beneficial in order to understand why a model makes certain predictions and what kind of data is fed into the algorithm.
In this article, we will look at a tool called Embedding Projector, which enables us to visualise high dimensional data easily so that we can understand what does a model learn about data.
An embedding is essentially a low-dimensional space into which a high dimensional vector can be translated. During translation, an embedding preserves the semantic relationship of the inputs by placing similar inputs close together in the embedding space. Let’s try and wrap our head around this concept with examples. Here is a grab from the creators of the Embedding projector.
you can find the complete video here.
Words can also be represented as embeddings. Here is an example of a 300-dimensional embedding that maps words to vectors of real numbers. It is important to note that, the individual dimensional in these vectors do not provide much information. However, it’s the overall patters of distance and location between different vectors that are of use to machine learning.
Advantages of Embeddings
Multidimensional space helps to group semantically related items together while keeping the dissimilar items apart. This can prove to be highly useful in a machine learning task. Consider the following visualisations of real embeddings:
These embeddings capture the semantic relationships between words like verb tense, country capital relationship and gender analogies.
The Embedding Projector Application
Embedding Projector is a web application tool that interactively visualizes embeddings by reading them from our model and rendering them in two or three dimensions. Here is a visualisation of the ten thousand MNIST images which have been coloured by their label.
The Embedding Projector is open-sourced and integrated into the TensorFlow platform or can be used as a standalone tool at projector.tensorflow.org. The standalone tool works without the need to install and run TensorFlow.
The figure above shows the main view of the web app which consists of five distinct panels.
- Data panel, where we can choose which data set to examine. The Embedding Projector website includes a few datasets to play with or we can load our own datasets. It is also possible to publish and share our embeddings with others by clicking the ‘publish’ button.
- Projections panel where we can choose the type of projection.
- Inspector panel where we can search for particular points and see a list of nearest neighbours.
- Bookmarks panel lets us save the current state (including computed coordinates of any projection) as a small file which can then be shared.
- Visualization panel is the area where the data is displayed
The Embedding Projector offers four well-known methods for reducing the dimensionality of the data. Each method can be used to create either a two- or three-dimensional view for exploration.
- Principal Component Analysis(PCA)
PCA is a technique which extracts a new set of variables called Principal Components from the existing data. These Principal Components are a linear combination of original features and try to capture as much information from the original dataset. The Embedding Projector computes the top 10 principal components for the data, from which we can choose two or three to view. Let’s look at the PCA projection of 10000 MNIST Digits. Each MNIST digit has 784 pixels and the projection treats each pixel as a dimension.
t-SNE or T-distributed stochastic neighbour embedding visualizes high-dimensional data by giving each data point a location in a two or three-dimensional map. This technique finds clusters in data thereby making sure that an embedding preserves the meaning in the data. The following t-SNE projection for the famous MNIST dataset clearly shows that similar digits are clustered together.
A custom projection is a linear projection onto the horizontal and vertical axes which have been specified using the data labels. The custom projections mainly help to decipher the meaningful “directions” in data sets.
UMAP stands for Uniform Manifold Approximation and Projection for Dimension Reduction. t-SNE is an excellent technique to visualise high dimensional datasets but has certain drawbacks like high computation time and loss of large scale information. UMAP, on the other hand, tries to overcome these limitations as it can handle pretty large datasets easily while also preserving the local and global structure of data.
If you are looking for the mathematical description please see the UMAP paper.
We shall now examine how a Word2Vec model that has been trained to work with text, groups the meanings of words. It will be interesting to see these groupings since they will reveal a lot about the relationships between the words. The dataset has been included with the tool.
Wikipedia describes Word2vec as a group of related models that are used to produce word embeddings. Word2vec takes as its input a large corpus of text and produces a vector space, typically of several hundred dimensions, with each unique word in the corpus being assigned a corresponding vector in the space.
We shall work with Word2Vec 10K dataset. Here each dot represents one word and every word has 200 dimensions. Let’s quickly go through the steps to create the projections:
- Select the desired dataset from the data panel. Each of the data points here comes from a model trained on Wikipedia articles.
- Let’s now do a neighbourhood analysis by looking at the nearest neighbours for a set of predefined points. This will give us a fair idea of what relationships the model has learnt.
- In the inspector panel, type the word ‘Apple’.
- The tool suggests some matching words. Click on
appleand we get a list of nearest points in space, which include words that are similar to
- To get rid of the noise, isolate the relevant points by clicking on the grey button labelled,
isolate 101 points. The points include the selected point and its 100 nearest neighbours.
- We can also rotate the space, zoom and even pan a little bit.
fruit occur together while
microprocessor, are also grouped together at the top. It is important to note here that the model hasn’t been told the meaning of words, instead, it has been just shown millions of sentences as examples of how words are used.
Here are some more examples which will make things more clear.
On the left is the cluster of words related to
sound. The middle figure shows words similar to
silver while the rightmost picture shows words related to
Unearthing Bias in our language
Sometimes models learn things that are a cause for concern especially when machine learning models are used to make decisions for humans. This time we shall use the Word2Vec All corpus and search for the word Engineer to see its nearest neighbours. The embedding projector also allows us to reorient the visualization in order to perform more sophisticated tests for these cases so we shall re-orient our results using the Custom Projections tab.
I fixed an axis that goes from man to woman so words closed to man lie towards left while words similar to woman will be found on the right. Let’s look at the results for our anchor word which is Engineer, given the above axis.
It appears that the word engineer is already closer to man than woman. The words closer to man are in orange and include
astronomer, physicist, mathematician while words like
dancer, songwriter, teacher appear closer to woman.
How about changing the anchor word to math? Are the results affected? Let’s see for ourselves:
We have words like
computational, geometry, arithmetic next to man, while the nearest neighbours to woman are
music, teaching, philosophy etc.
Imagine if a machine learning algorithm trained on this dataset is used to predict how good someone is at their job related to art or math? Also, what will happen if a company relies on such an algorithm to hire potential engineers? The model might mistakenly believe that gender affected how good a candidate they were and the resulting decisions will be gender biased.
Bias in machine learning models is an area of concern. However, it is important to understand that most of the times the training data on which the model is trained is actually biased which is then ultimately reflected in the model. Therefore whereas it is important to emphasize on creating accurate models, it is also important to debug the training data for any bias or anomalies that is present in it.