What's new

GloVe - Subscript out of bounds error in R


Active member
Staff member
I am attempting to write a feature that checks for each query what the maximum score is. The query is the search term, and the glove word vectors are trained using product descriptions. The code I use for training the word vectors and the feature itself (all.gloveterms) are listed below. I am receiving the error: Error in word_vectors[query, , drop = FALSE] : subscript out of bounds, after attempting to execute the command: all.gloveterms(query_product$search_term[2]), where the search term should return "angle bracket". I am having a hard time finding out how to fix this error, and was wondering if anyone can help.

# train the word vectors
glove = GlobalVectors$new(rank = 50, x_max = 10)
wv_main = glove$fit_transform(tcm, n_iter = 50, convergence_tol = 0.01)
wv_context = glove$components
word_vectors = wv_main + t(wv_context)

all.gloveterms <- function (queries)
n <- length(queries)
scores <- c(0)
for(i in 1:n){
query <- queries
wordvector = word_vectors[query, , drop = FALSE]
cosine = sim2(x = word_vectors, y = wordvector, method = "cosine", norm = "l2")
scores <- append(scores, max(cosine))
y <- as.integer(0)
score <- sum(unlist(scores))/n
feature <- if (length(score)) score else y

Continue reading...