| Title: | Functions to support aimat.dk |
|---|---|
| Description: | Functions to support the Danish language site aimat.dk containing teaching material related to the math behind AI. |
| Authors: | Ege Rubak [aut, cre] |
| Maintainer: | Ege Rubak <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.1 |
| Built: | 2026-06-04 22:11:17 UTC |
| Source: | https://github.com/aalborg-intelligence/aimat |
This function launches a Shiny app for training neural networks using the 'aimat' package.
aimatApp(appname)aimatApp(appname)
appname |
A character string specifying the name of the app to run. The app should be located in the ‘aimat' package’s system file directory. |
if (interactive() & requireNamespace("shiny", quietly = TRUE)) { aimatApp("app_full") }if (interactive() & requireNamespace("shiny", quietly = TRUE)) { aimatApp("app_full") }
Words in an artificial childrens book (in Danish)
bogbog
## 'bog' A vector of words
Fibonacci Sphere
fibonacci_sphere(samples = 1000)fibonacci_sphere(samples = 1000)
samples |
Number of points to generate on the sphere. |
A matrix of points on the sphere, where each row is a point in 3D space.
points <- fibonacci_sphere() if(requireNamespace("plotly", quietly = TRUE)){ plotly::plot_ly(x = points[, 1], y = points[, 2], z = points[, 3], type = "scatter3d", mode = "markers", marker = list(size = 5)) }points <- fibonacci_sphere() if(requireNamespace("plotly", quietly = TRUE)){ plotly::plot_ly(x = points[, 1], y = points[, 2], z = points[, 3], type = "scatter3d", mode = "markers", marker = list(size = 5)) }
General neural network with at most two hidden layers
nn_fun( formula, data, weights = NA, n_hidden = c(1, 1), activation = "Sigmoid", eta = 0.01, iter = 1000, scale = TRUE, lossfun = "squared", type = "klassifikation", trace = FALSE )nn_fun( formula, data, weights = NA, n_hidden = c(1, 1), activation = "Sigmoid", eta = 0.01, iter = 1000, scale = TRUE, lossfun = "squared", type = "klassifikation", trace = FALSE )
formula |
response ~ predictors |
data |
data frame |
weights |
Starting weights |
|
integer vector of length 2 with number of neurons in the hidden layers (can be zero) |
|
activation |
one of "Sigmoid", "Tangenshyperbolsk", "ReLu", "Softsign", "Identitet" |
eta |
learning rate |
iter |
number of iterations |
scale |
logical to scale the input data |
lossfun |
one of "squared", "cross-entropy" |
type |
one of "regression", "klassifikation" |
trace |
logical to save each iteration |
Fitted neural network model as an object of class "nn"
ir <- iris ir[,1:4] <- scale(ir[,1:4]) fit_ir <- nn_fun(Species ~ ., ir, n_hidden = c(3,5), eta = 0.01, iter = 1000, lossfun = "cross-entropy", activation = "Sigmoid", type = "klassifikation")ir <- iris ir[,1:4] <- scale(ir[,1:4]) fit_ir <- nn_fun(Species ~ ., ir, n_hidden = c(3,5), eta = 0.01, iter = 1000, lossfun = "cross-entropy", activation = "Sigmoid", type = "klassifikation")
Cross-validation for neural network
nn_fun_cv(formula, data, ..., k = 5)nn_fun_cv(formula, data, ..., k = 5)
formula |
response ~ predictors |
data |
data frame |
... |
Additional arguments passed to 'nn_fun' |
k |
Number of folds for cross-validation |
Mean accuracy across folds
cv <- nn_fun_cv(Species ~ ., iris, n_hidden = c(3,5), eta = 0.01, iter = 1000, lossfun = "cross-entropy")cv <- nn_fun_cv(Species ~ ., iris, n_hidden = c(3,5), eta = 0.01, iter = 1000, lossfun = "cross-entropy")
Visualize a neural network
nn_viz(nn)nn_viz(nn)
nn |
FIXME |
ir <- iris ir[,1:4] <- scale(ir[,1:4]) fit_ir <- nn_fun(Species ~ ., ir, n_hidden = c(3,5), eta = 0.01, iter = 1000, lossfun = "cross-entropy", activation = "Sigmoid", type = "klassifikation") if(requireNamespace("visNetwork", quietly = TRUE)){ nn_viz(fit_ir) }ir <- iris ir[,1:4] <- scale(ir[,1:4]) fit_ir <- nn_fun(Species ~ ., ir, n_hidden = c(3,5), eta = 0.01, iter = 1000, lossfun = "cross-entropy", activation = "Sigmoid", type = "klassifikation") if(requireNamespace("visNetwork", quietly = TRUE)){ nn_viz(fit_ir) }
Make next word prediction data
nwp_make_word_data(corpus, context_size, w2v, return = "both")nwp_make_word_data(corpus, context_size, w2v, return = "both")
corpus |
Vector of words. |
context_size |
Integer, size of the context window (using this many words to predict the next). |
w2v |
Word2Vec model object. This is a list with three elements: 'W1', 'W2' and 'vocab'. 'W1' and 'W2' are matrices of word vectors, and 'vocab' is a vector of words. |
return |
Character, one of "both", "words" or "numeric". If "both", returns a list with two elements: 'words' and 'numeric'. If "words", returns only the 'words' element. If "numeric", returns only the 'numeric' element. Default is "both". |
A list containing data to train next word prediction. The list contains one or both of the following elements: - words: a data.frame with 'context_size'+1 columns of words where the last column is the target word. - numeric: a numeric matrix
bog_w2v <- w2v(bog, win = 1, hidden_dim = 3, epochs = 5, learning_rate = 0.01, verbose = 0) bog_data <- nwp_make_word_data(corpus = bog, context_size = 2, w2v = bog_w2v)bog_w2v <- w2v(bog, win = 1, hidden_dim = 3, epochs = 5, learning_rate = 0.01, verbose = 0) bog_data <- nwp_make_word_data(corpus = bog, context_size = 2, w2v = bog_w2v)
Plot word2vec model
## S3 method for class 'w2v' plot(x, ..., which = 1)## S3 method for class 'w2v' plot(x, ..., which = 1)
x |
Word2Vec model object (list with W1, W2 and vocab) |
... |
Additional arguments (not used) |
which |
Integer, 1 or 2. If 1, plot W1 (input layer), if 2, plot W2 (output layer) |
w <- w2v(bog, hidden_dim = 2, epochs = 10, learning_rate = 0.01, verbose = 1) plot(w, which = 1)w <- w2v(bog, hidden_dim = 2, epochs = 10, learning_rate = 0.01, verbose = 1) plot(w, which = 1)
Predict method for neural network
## S3 method for class 'nn' predict(object, newdata, type = "response", ...)## S3 method for class 'nn' predict(object, newdata, type = "response", ...)
object |
Fitted neural network model |
newdata |
Data frame with new data |
type |
One of "response", "class" |
... |
Additional arguments, currently not used |
Predicted values
nn_iris <- nn_fun(Species ~ ., iris, n_hidden = c(3,5), eta = 0.01, iter = 1000, lossfun = "cross-entropy", activation = "Sigmoid", type = "klassifikation", scale = TRUE) predict(nn_iris, iris, type = "class") nn_trees <- nn_fun(Volume ~ ., trees, n_hidden = c(3,5), eta = 0.01, iter = 1000, lossfun = "squared", activation = "Sigmoid", type = "regression", scale = TRUE) predict(nn_trees, trees, type = "response")nn_iris <- nn_fun(Species ~ ., iris, n_hidden = c(3,5), eta = 0.01, iter = 1000, lossfun = "cross-entropy", activation = "Sigmoid", type = "klassifikation", scale = TRUE) predict(nn_iris, iris, type = "class") nn_trees <- nn_fun(Volume ~ ., trees, n_hidden = c(3,5), eta = 0.01, iter = 1000, lossfun = "squared", activation = "Sigmoid", type = "regression", scale = TRUE) predict(nn_trees, trees, type = "response")
Word2vec Neural Network
w2v( corpus, win = 1, hidden_dim = 3, epochs = 100, learning_rate = 0.01, verbose = 10, weights = NULL )w2v( corpus, win = 1, hidden_dim = 3, epochs = 100, learning_rate = 0.01, verbose = 10, weights = NULL )
corpus |
Vector of words |
win |
Integer, size of the context window |
|
Dimension of the hidden layer |
|
epochs |
Number of training epochs |
learning_rate |
Learning rate |
verbose |
Integer, verbosity level (0 = no output, >0 = output every 'verbose' epochs) |
weights |
FIXME |
w <- w2v(bog, hidden_dim = 2, epochs = 10, learning_rate = 0.01, verbose = 1)w <- w2v(bog, hidden_dim = 2, epochs = 10, learning_rate = 0.01, verbose = 1)
Build word2vec data from a corpus represented as a vector of words
w2v_build_data(corpus, win = 1)w2v_build_data(corpus, win = 1)
corpus |
vector of words |
win |
integer, size of the context window |
A list containing the following elements: - 'X': Sparse matrix of one-hot encoded input vectors - 'Y': Sparse matrix of one-hot encoded output vectors - 'vocab': Vector of unique words in the corpus - 'pairs': Data frame of word pairs in context
w2v_data <- w2v_build_data(bog, win = 1)w2v_data <- w2v_build_data(bog, win = 1)
Word2vec Neural Network
w2v_nn(X, Y, hidden_dim, epochs, learning_rate, weights = NULL, verbose = 10)w2v_nn(X, Y, hidden_dim, epochs, learning_rate, weights = NULL, verbose = 10)
X |
Matrix of one-hot encoded input vectors |
Y |
Matrix of one-hot encoded output vectors |
|
Dimension of the hidden layer |
|
epochs |
Number of training epochs |
learning_rate |
Learning rate |
weights |
Initial weights for the network (random if NULL) |
verbose |
FIXME |
Named list contaning trained weights for the network
# Example usage library(Matrix) n <- 10 # Dimension of input and output m <- 3 # Dimension of the first hidden layer # Create input matrix with several one-hot vectors X <- Matrix(0, nrow = 5, ncol = n, sparse = TRUE) for (i in 1:5) { X[i, sample(1:n, 1)] <- 1 } # Create output matrix with corresponding one-hot vectors Y <- Matrix(0, nrow = 5, ncol = n, sparse = TRUE) for (i in 1:5) { Y[i, sample(1:n, 1)] <- 1 } # Train the network trained_weights <- w2v_nn(X, Y, m, epochs = 100, learning_rate = 0.1, verbose = 10)# Example usage library(Matrix) n <- 10 # Dimension of input and output m <- 3 # Dimension of the first hidden layer # Create input matrix with several one-hot vectors X <- Matrix(0, nrow = 5, ncol = n, sparse = TRUE) for (i in 1:5) { X[i, sample(1:n, 1)] <- 1 } # Create output matrix with corresponding one-hot vectors Y <- Matrix(0, nrow = 5, ncol = n, sparse = TRUE) for (i in 1:5) { Y[i, sample(1:n, 1)] <- 1 } # Train the network trained_weights <- w2v_nn(X, Y, m, epochs = 100, learning_rate = 0.1, verbose = 10)