Skip to contents

Multiple embeddings in one layer

Usage

layer_multi_embedding(object, input_dims, output_dims, new_dim = FALSE, ...)

Arguments

object

What to compose the new Layer instance with. Typically a Sequential model or a Tensor (e.g., as returned by layer_input()). The return value depends on object. If object is:

  • missing or NULL, the Layer instance is returned.

  • a Sequential model, the model with an additional layer is returned.

  • a Tensor, the output tensor from layer_instance(object) is returned.

new_dim

If TRUE, new dimension is created instead of stacking outputs in the same dimension

Examples

# ==========================================================================
#                          SIMPLE CONCATENATION
# ==========================================================================

inp <- layer_input(c(28, 3))
emb <- layer_multi_embedding(input_dims = c(4, 6, 8), output_dims = c(3, 4, 5))(inp)

emb_model <- keras_model(inp, emb)

dummy_input <- array(1, dim = c(1, 28, 3))
dummy_input[,,1] <- sample(4,size = 28, replace = TRUE)
dummy_input[,,2] <- sample(6,size = 28, replace = TRUE)
dummy_input[,,3] <- sample(8,size = 28, replace = TRUE)

out <- emb_model(dummy_input)
dim(out)
#> [1]  1 28 12
# ==========================================================================
#                              ONE VARIABLE
# ==========================================================================

inp <- layer_input(c(32, 1))
emb <- layer_multi_embedding(input_dims = 10, output_dims = 2)(inp)

# ==========================================================================
#                              NEW DIMESNION
# ==========================================================================

inp <- layer_input(c(28, 3))
emb <- layer_multi_embedding(input_dims = c(4, 6, 8), output_dims = 5, new_dim = TRUE)(inp)

emb_model <- keras_model(inp, emb)

dummy_input <- array(1, dim = c(1, 28, 3))
dummy_input[,,1] <- sample(4,size = 28, replace = TRUE)
dummy_input[,,2] <- sample(6,size = 28, replace = TRUE)
dummy_input[,,3] <- sample(8,size = 28, replace = TRUE)

out <- emb_model(dummy_input)
dim(out)
#> [1]  1 28  3  5