Skip to contents

Multiple dense layers in one layer

Usage

layer_multi_dense(object, units, 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.

units

Positive integer, dimensionality of the output space.

Input and Output Shapes

Input shape: nD tensor with shape: (batch_size, ..., input_dim). The most common situation would be a 2D input with shape (batch_size, input_dim).

Output shape:

  • If length of units equals 1 nD tensor with shape: (batch_size, ..., units). For instance, for a 2D input with shape (batch_size, input_dim), the output would have shape (batch_size, unit).

  • If length of units is greater than 1 nD tensor with shape: (batch_size, ..., units). For instance, for a 2D input with shape (batch_size, input_dim), the output would have shape (batch_size, unit).

Examples

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

inp <- layer_input(c(28, 3))
md <- layer_multi_dense(units = c(4, 6, 8))(inp)

md_model <- keras_model(inp, md)

dummy_input <- array(1, dim = c(1, 28, 3))

out <- md_model(dummy_input)
#> Error in py_call_impl(callable, dots$args, dots$keywords): RuntimeError: Exception encountered when calling layer "multi_dense" "                 f"(type MultiDense).
#> 
#> Evaluation error: tensorflow.python.framework.errors_impl.InternalError: Exception encountered when calling layer "dense" "                 f"(type Dense).
#> 
#> {{function_node __wrapped__MatMul_device_/job:localhost/replica:0/task:0/device:GPU:0}} Attempting to perform BLAS operation using StreamExecutor without BLAS support [Op:MatMul]
#> 
#> Call arguments received by layer "dense" "                 f"(type Dense):
#>   • inputs=tf.Tensor(shape=(1, 28, 1), dtype=float32)
#> .
#> 
#> Call arguments received by layer "multi_dense" "                 f"(type MultiDense):
#>   • inputs=tf.Tensor(shape=(1, 28, 3), dtype=float32)
dim(out)
#> Error in eval(expr, envir, enclos): object 'out' not found

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

inp <- layer_input(c(28, 3))
md <- layer_multi_dense(units = 5, new_dim = TRUE)(inp)

md_model <- keras_model(inp, md)

dummy_input <- array(1, dim = c(1, 28, 3))

out <- md_model(dummy_input)
#> Error in py_call_impl(callable, dots$args, dots$keywords): RuntimeError: Exception encountered when calling layer "multi_dense_1" "                 f"(type MultiDense).
#> 
#> Evaluation error: tensorflow.python.framework.errors_impl.InternalError: Exception encountered when calling layer "dense" "                 f"(type Dense).
#> 
#> {{function_node __wrapped__MatMul_device_/job:localhost/replica:0/task:0/device:GPU:0}} Attempting to perform BLAS operation using StreamExecutor without BLAS support [Op:MatMul]
#> 
#> Call arguments received by layer "dense" "                 f"(type Dense):
#>   • inputs=tf.Tensor(shape=(1, 28, 1), dtype=float32)
#> .
#> 
#> Call arguments received by layer "multi_dense_1" "                 f"(type MultiDense):
#>   • inputs=tf.Tensor(shape=(1, 28, 3), dtype=float32)
dim(out)
#> Error in eval(expr, envir, enclos): object 'out' not found