Commit f56f33b4 authored by podrazka's avatar podrazka
Browse files

First attempt at visualizing BERT

parent 741bc935
Loading
Loading
Loading
Loading
+26 −0
Changes for SBERT_Model.py: 26 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,11 @@ import STSB_Dataset

from scipy.stats import spearmanr, pearsonr
from sklearn import metrics
import numpy as np
import scipy.spatial as sp
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
from random import randrange

class SBERT_Model:
    def __init__(self, name, filepath, dataset, probing=False):
@@ -20,6 +25,27 @@ class SBERT_Model:
        self.cosine_scores = self.get_cosine_scores()
        self.preds = self.get_preds()
    
    def visualize_embeddings(self):
        sentences = self.sentences1 + self.sentences2
        embs = self.get_embeddings(sentences)
        pca = PCA(n_components=2)
        X = pca.fit_transform(embs_together)
        plt.figure(figsize=(20,10))
        plt.scatter(X[:, 0], X[:, 1])
        for x, y in X:
            label = "{:.2f}".format(y)
            name = i
            label = f"{name}\n({x:.2f},{y:.2f})"

            plt.annotate(label, # this is the text
                        (x,y), # this is the point to label
                        textcoords="offset points", # how to position the text
                        xytext=(0,10), # distance from text to points (x,y)
                        ha='center') # horizontal alignment can be left, right or center
            i = i + 1
        plt.savefig(f'{randrange(0,10000)}plot.png')


    def get_embeddings(self, sentences):
        return self.model.encode(sentences, convert_to_tensor=True)