This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from typing import Optional, Tuple, List, Any | |
class TreeNode: | |
"""Represents a node in the parse tree.""" | |
def __init__(self, rule_name: str, content: Any = None, children: List['TreeNode'] = None): | |
self.rule_name = rule_name | |
self.content = content # For terminal nodes | |
self.children = children or [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from copy import copy | |
from collections import defaultdict, Counter | |
from functools import cache | |
from glob import glob | |
import pandas as pd | |
import json | |
from tqdm import tqdm | |
@cache |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
from collections import defaultdict | |
# --- Hook Implementation --- | |
# Dictionary to store attention weights during the forward pass | |
# Structure: {layer_index: attention_weights_tensor} | |
# The tensor shape will be (batch_size, num_heads, seq_len, seq_len) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.request | |
from collections import defaultdict | |
raw_bytes = urllib.request.urlopen( | |
'http://www.sls.hawaii.edu/bley-vroman/brown.txt') | |
brown_corpus = raw_bytes.read().decode('utf8').replace('\r\n', '\n') | |
B = brown_corpus[:250] | |
def find_pairs(text): | |
pairs = defaultdict(int) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
import numpy as np | |
import pandas as pd | |
import torch | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
from tqdm.auto import tqdm | |
with open('sts_attributions/shelf_approx_attr_l-9_N-100.pkl', 'rb') as inp: | |
shelf_approx = pickle.load(inp) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import pickle | |
import requests | |
import torch | |
from sentence_transformers import SentenceTransformer | |
from sentence_transformers.models import Pooling | |
from sentence_transformers import util | |
from xsbert import models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from http.server import BaseHTTPRequestHandler, HTTPServer | |
import pandas as pd | |
hostName = "localhost" | |
serverPort = 20000 | |
# A global variable to store the queue elements | |
queue = [] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
from itertools import combinations | |
import pandas as pd | |
import numpy as np | |
from sentence_transformers import SentenceTransformer, util | |
def compute_kernel_bias(vecs, k=None): | |
""" | |
Code taken from: https://github.com/bojone/BERT-whitening |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import shutil | |
def copy_tree(src, dst): | |
''' | |
Copy a directory tree from src to dst ignoring dangling | |
symlinks, retrieving files symlinks point to, and | |
breaking the cycles, i.e. never copying the same |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from math import ceil | |
from random import shuffle | |
import torch | |
import torch.nn as nn | |
from transformers import AutoTokenizer, AutoModel | |
from transformers import AdamW, get_scheduler |
NewerOlder