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
import pandas as pd | |
import setriq | |
df = pd.read_csv("vdjdb.csv", names=["cdr3", "epitope", "gene", "species"]) | |
lev = setriq.Levenshtein(return_squareform=True) | |
# limit to 1000 to avoid explosion in time-taken | |
D = lev(df["cdr3"].iloc[:1000]) |
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
from typing import List | |
import numpy as np | |
import pandas as pd | |
import setriq | |
def search_most_similar_video(search: str, video_titles: List[str]) -> str: | |
lev = setriq.Levenshtein(return_squareform=True) | |
distances = lev([search] + video_titles) |
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
def softmax(y): | |
return np.exp(y) / np.sum(np.exp(y)) | |
class ModernHopfield: | |
def __init__(self, memories): | |
self.memories = memories | |
def update(self, query): | |
normalised_memories = self.memories / np.vstack(np.linalg.norm(self.memories, |
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
import numpy as np | |
def is_binary(x): | |
if np.all(np.abs(x) == 1): | |
return x | |
raise AttributeError('Inputs need to be binary: 1 or -1') | |
class ClassicHopfield: |
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
# testing | |
err = 0 | |
for n in range(n_data_points-test, n_data_points): | |
z = np.array(inputs[n]) | |
t = int(np.sum(z) >= 0) | |
x = np.append(z, -1) | |
err += loss(t, f(x.dot(w))) |
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
n_data_points = len(inputs) | |
train = int(n_data_points * 0.8) | |
validate = (n_data_points - train) // 2 | |
test = n_data_points - train - validate | |
# train the model over the specified number of epochs | |
for i in range(epochs): | |
err = 0 | |
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
epochs = 500 # number of epochs | |
epsilon = 0.01 # learning rate || step size | |
inputs = list(product([-1,1], repeat=10)) # produces all 1024 permutations | |
w = uniform.rvs(size=11) # weight initialisation | |
ep_err = [] # initialise list for per-epoch error |
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
def gradient_descent(t, w, x): | |
z = x.dot(w) | |
delta_w = (t-f(z)) * fprime(z) * x | |
return delta_w # notice that the signs cancel |
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
def loss(t,y): | |
err = 0.5 * (t-y)**2 | |
return err |
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
def fprime(z): | |
der = f(z) * (1-f(z)) | |
return der |
NewerOlder