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 | |
from numpy.polynomial import Polynomial | |
def polynomial_modulo(polynomial, mod): | |
""" | |
Perform polynomial modulo operation using divmod. | |
""" | |
q, r = divmod(polynomial, mod) | |
return r |
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 | |
from numpy.polynomial import Polynomial | |
def polynomial_modulo(polynomial, mod): | |
""" | |
Perform polynomial modulo operation using divmod. | |
""" | |
q, r = divmod(polynomial, mod) | |
return r |
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
library(polynom) | |
M <- 8 | |
N <- M %/% 2 | |
scale <- 64 | |
xi <- complex(real = cos(2 * pi / M), imaginary = sin(2 * pi / M)) | |
vandermonde <- function(xi, M) { | |
N <- M %/% 2 | |
# Initialize an empty matrix with complex data type |
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 | |
from numpy.polynomial import Polynomial | |
# Set the parameters | |
M = 8 | |
N = M // 2 | |
scale = 64 | |
xi = np.exp(2 * np.pi * 1j / M) | |
def vandermonde(xi: np.complex128, M: int) -> np.array: |
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
library(matrixStats) | |
# Softmax function | |
softmax <- function(x) { | |
exp_x <- exp(x - max(x)) | |
exp_x / sum(exp_x) | |
} | |
# Scaled dot product attention | |
scaled_dot_product_attention <- function(Q, K, V, mask = NULL) { |
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
# HE illustrated primer | |
# define some parameters (small as an example) | |
# N.B. these parameters are not secure, they are completely insecure | |
d = 4 | |
n = 2^d | |
t = (n/2)-1 | |
q = 874 | |
# load library to create polynomials |
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
# HE illustrated primer | |
# define some parameters (small as an example) | |
# N.B. these parameters are not secure, they are completely insecure | |
n = 4 | |
d = 2^n | |
t = 7 | |
q = 874 | |
# load library to create polynomials |
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
# attention.R | |
# Bastiaan Quast | |
# bquast@gmail.com | |
# based on: | |
# https://machinelearningmastery.com/the-attention-mechanism-from-scratch/ | |
# encoder representations of four different words | |
word_1 = matrix(c(1,0,0), nrow=1) | |
word_2 = matrix(c(0,1,0), nrow=1) |
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
# logsumexp | |
logsumexp <- function (x) { | |
y = max(x) | |
y + log(sum(exp(x - y))) | |
} | |
# softmax | |
softmax <- function (x) { | |
exp(x - logsumexp(x)) | |
} |
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
# contructor function | |
tensor <- function(x) { | |
# check that it's numeric | |
if (!is.numeric(x)) stop("X must be numeric") | |
# create the array and change the class | |
y <- structure(array(x), class = "tensor") | |
# add attributes | |
attributes(y)$creators <- list() |
NewerOlder