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
| # 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 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
| 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 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
| # rnn.R | |
| # Bastiaan Quast | |
| # [email protected] | |
| # Set the seed to obtain identical random values | |
| set.seed(0) | |
| # compute sigmoid nonlinearity | |
| sigmoid = function(x) | |
| 1 / (1+exp(-x)) |
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
| # cnn.R | |
| # Bastiaan Quast | |
| # [email protected] | |
| # Convolution function | |
| conv2d <- function(input, filter, stride = 1) { | |
| input_height <- dim(input)[1] | |
| input_width <- dim(input)[2] | |
| filter_height <- dim(filter)[1] | |
| filter_width <- dim(filter)[2] |
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
| # NN-1-layer.R | |
| # Bastiaan Quast | |
| # [email protected] | |
| # create data | |
| inputs = matrix(c(0,1,1, | |
| 1,0,1, | |
| 1,0,1, | |
| 1,1,0 ), nrow=4, byrow=TRUE) |
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
| # DNN2.R | |
| # Bastiaan Quast | |
| # [email protected] | |
| # derivative | |
| der <- function(x){ | |
| x * (1-x) | |
| } | |
| # input data |
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
| # transformer simple.R | |
| # Bastiaan Quast | |
| softmax <- function(x) { | |
| e_x <- exp(x - max(x)) # subtract max to avoid numerical instability | |
| return(e_x / sum(e_x)) | |
| } | |
| # Initialize input queries, keys, and values | |
| query <- rnorm(5) |
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 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 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
| 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 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 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 |