Skip to content

Instantly share code, notes, and snippets.

@mike-lawrence
Created December 22, 2019 16:13
Show Gist options
  • Save mike-lawrence/2f37383067d4cf4050c492c56f21f2ca to your computer and use it in GitHub Desktop.
Save mike-lawrence/2f37383067d4cf4050c492c56f21f2ca to your computer and use it in GitHub Desktop.
Failed attempt at using holographic database for determining entry presence/absense
#define a function to generate a random vector
makeNewVector = function(vectorLength){
#random normal
x = rnorm(vectorLength)
#normalize to length 1
x/(sqrt(sum(x^2)))
}
#define a function to compute cosine similarity between two vectors
cosine <- function( x, y) {
crossprod(x,y) / sqrt( crossprod(x)*crossprod(y) )
}
#set random seed
set.seed(1)
#set simulation parameters
vectorLength = 1e3
numVectors = 1e4
#generate entry vectors
vecList = list()
for(i in 1:numVectors){
#each vector is a random normal with length 1
vecList[[i]] = makeNewVector(vectorLength)
}
#generate database
db = rep(0,vectorLength)
for(i in 1:numVectors){
#database is simply the superposition of its vectors
db = db + vecList[[i]]
}
#compute resonance of each entry
sims = rep(NA,numVectors)
for(i in 1:numVectors){
sims[i] = cosine(vecList[[i]],db)
}
#check against vectors not in database
newSims = rep(NA,numVectors)
for(i in 1:numVectors){
#each vector is a random normal with length 1
newVec = makeNewVector(vectorLength)
newSims[i] = cosine(newVec,db)
}
#gather & vizualize the similarities
library(tidyverse)
tibble(
newSims
, sims
) %>%
tidyr::gather(
key = class
, value = value
) %>%
ggplot()+
facet_grid(
class ~ .
)+
geom_histogram(
mapping = aes(
x = value
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment