-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
""" | ||
Probe experiments are used to generate data for probing tasks. | ||
""" | ||
|
||
|
||
class ProbeExperiment(ABC): | ||
def __init__(self, task_name: str): | ||
self.task_name = task_name | ||
self.data = None | ||
|
||
def __repr__(self) -> str: | ||
return self.task_name | ||
|
||
@abstractmethod | ||
def get_data(self) -> list[tuple[str, int]]: | ||
pass |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import Callable | ||
|
||
from probe_lens.experiments.experiments import ProbeExperiment | ||
|
||
LETTERS = "abcdefghijklmnopqrstuvwxyz" | ||
WORDS_DATASET = "https://www.mit.edu/~ecprice/wordlist.10000" | ||
|
||
|
||
def default_spelling_prompt_generator(word: str): | ||
return f"The word '{word}' is spelled:" | ||
|
||
|
||
def first_letter_index(word: str): | ||
return LETTERS.index(word.strip().lower()[0]) | ||
|
||
|
||
class FirstLetterSpelling(ProbeExperiment): | ||
def __init__( | ||
self, | ||
words: list[str], | ||
prompt_fn: Callable[[str], str] = default_spelling_prompt_generator, | ||
class_fn: Callable[[str], int] = first_letter_index, | ||
): | ||
super().__init__("First Letter Spelling Experiment") | ||
self.words = words | ||
self.prompt_fn = prompt_fn | ||
self.class_fn = class_fn | ||
self.generate_data() | ||
|
||
def generate_data(self): | ||
self.classes = [self.class_fn(word) for word in self.words] | ||
self.prompts = [self.prompt_fn(word) for word in self.words] | ||
self.data = list(zip(self.prompts, self.classes)) | ||
|
||
def get_data(self) -> list[tuple[str, int]]: | ||
return self.data |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from probe_lens.experiments.spelling import LETTERS, FirstLetterSpelling | ||
|
||
|
||
def test_first_letter_spelling(): | ||
words = ["example", "words", "to", "spell"] | ||
spelling_task = FirstLetterSpelling(words) | ||
data = spelling_task.data | ||
classes = [c for _, c in data] | ||
assert classes == [LETTERS.index(word.lower()[0]) for word in words] |
This file contains 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