---
title: "About Keras Layers"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{About Keras Layers}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
type: docs
repo: https://github.com/rstudio/keras
menu:
  main:
    name: "About Keras Layers"
    identifier: "keras-about-keras-layers"
    parent: "keras-advanced"
    weight: 20
---

```{r setup, include = FALSE}
library(keras)
knitr::opts_chunk$set(comment = NA, eval = FALSE)
```


## Overview

Keras layers are the fundamental building block of keras models. Layers are created using a wide variety of `layer_` functions and are typically composed together by stacking calls to them using the pipe `%>%` operator. For example:

```{r}
model <- keras_model_sequential() 
model %>% 
  layer_dense(units = 32, input_shape = c(784)) %>% 
  layer_activation('relu') %>% 
  layer_dense(units = 10) %>% 
  layer_activation('softmax')
```

A wide variety of layers are available, including:

- [Core Layers](https://keras.rstudio.com/reference/index.html#section-core-layers)

- [Convolutional Layers](https://keras.rstudio.com/reference/index.html#section-convolutional-layers)

- [Pooling Layers](https://keras.rstudio.com/reference/index.html#section-pooling-layers)

- [Activation Layers](https://keras.rstudio.com/reference/index.html#section-activation-layers)

- [Dropout Layers](https://keras.rstudio.com/reference/index.html#section-dropout-layers)

- [Locally-connected Layers](https://keras.rstudio.com/reference/index.html#section-locally-connected-layers)

- [Recurrent Layers](https://keras.rstudio.com/reference/index.html#section-recurrent-layers)

- [Embedding Layers](https://keras.rstudio.com/reference/index.html#section-embedding-layers)

- [Normalization Layers](https://keras.rstudio.com/reference/index.html#section-normalization-layers)

- [Noise Layers](https://keras.rstudio.com/reference/index.html#section-noise-layers)

- [Merge Layers](https://keras.rstudio.com/reference/index.html#section-merge-layers)

- [Layer Wrappers](https://keras.rstudio.com/reference/index.html#section-layer-wrappers)


## Properties

All layers share the following properties:

- `layer$name` ---  String, must be unique within a model.

- `layer$input_spec` --- List of input specifications. Each entry describes one required input: (ndim, dtype). A layer with `n` input tensors must have an `input_spec` of length `n`.

- `layer$trainable` --- Boolean, whether the layer weights will be updated during training.

- `layer$uses_learning_phase` -- Whether any operation of the layer uses `K.in_training_phase()` or `K.in_test_phase()`.

- `layer$input_shape` --- Input shape. Provided for convenience, but note that there may be cases in which this
    attribute is ill-defined (e.g. a shared layer with multiple input shapes), in which case
    requesting `input_shape` will result in an error. Prefer using `get_input_shape_at(layer, node_index)`.
    
- `layer$output_shape` --- Output shape. See above.

- `layer$inbound_nodes` --- List of nodes.

- `layer$outbound_nodes` --- List of nodes.

- `layer$input`, `layer$output` --- Input/output tensor(s). Note that if the layer is used more than
  once (shared layer), this is ill-defined and will result in an error. In such cases, use `get_input_at(layer, node_index)`.
  
- `layer$input_mask`, `layer$output_mask` --- Same as above, for masks.

- `layer$trainable_weights` --- List of variables.

- `layer$non_trainable_weights` --- List of variables.

- `layer$weights` ---  The concatenation of the lists trainable_weights and
    non_trainable_weights (in this order).
    
- `layer$constraints` ---  Mapping of weights to constraints.

## Functions

The following functions are available for interacting with layers:

<table class="ref-index">

<colgroup>
<col class="alias" />
<col class="title" />
</colgroup>

<tbody>

<tr>
<!--  -->
<td>
  `get_config()` `from_config()`
</td>
<td><p>Layer/Model configuration</p></td>
</tr><tr>
<!--  -->
<td>
  `get_weights()` `set_weights()`
</td>
<td><p>Layer/Model weights as R arrays</p></td>
</tr>
<tr>
<!--  -->
<td>
  `count_params()`
</td>
<td><p>Count the total number of scalars composing the weights.</p></td>
</tr>


<tr>
<!--  -->
<td>
  `get_input_at()` `get_output_at()` `get_input_shape_at()` `get_output_shape_at()` `get_input_mask_at()` `get_output_mask_at()`
</td>
<td><p>Retrieve tensors for layers with multiple nodes</p></td>
</tr>

<tr>
<!--  -->
<td>
  `reset_states()`
</td>
<td><p>Reset the states for a layer</p></td>
</tr>

</tbody>
</table>






