/
layer_simple_rnn.qmd
132 lines (85 loc) · 6.9 KB
/
layer_simple_rnn.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
---
execute:
freeze: true
---
[R/layers-recurrent.R](https://github.com/rstudio/keras//blob/main/R/layers-recurrent.R#L54)
# layer_simple_rnn
## Fully-connected RNN where the output is to be fed back to input.
## Description
Fully-connected RNN where the output is to be fed back to input.
## Usage
```r
layer_simple_rnn(
object,
units,
activation = "tanh",
use_bias = TRUE,
return_sequences = FALSE,
return_state = FALSE,
go_backwards = FALSE,
stateful = FALSE,
unroll = FALSE,
kernel_initializer = "glorot_uniform",
recurrent_initializer = "orthogonal",
bias_initializer = "zeros",
kernel_regularizer = NULL,
recurrent_regularizer = NULL,
bias_regularizer = NULL,
activity_regularizer = NULL,
kernel_constraint = NULL,
recurrent_constraint = NULL,
bias_constraint = NULL,
dropout = 0,
recurrent_dropout = 0,
...
)
```
## Arguments
|Arguments|Description|
|---|---|
| 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: <br>- missing or `NULL`, the `Layer` instance is returned. <br>- a `Sequential` model, the model with an additional layer is returned. <br>- a Tensor, the output tensor from `layer_instance(object)` is returned. |
| units | Positive integer, dimensionality of the output space. |
| activation | Activation function to use. Default: hyperbolic tangent (`tanh`). If you pass `NULL`, no activation is applied (ie. "linear" activation: `a(x) = x`). |
| use_bias | Boolean, whether the layer uses a bias vector. |
| return_sequences | Boolean. Whether to return the last output in the output sequence, or the full sequence. |
| return_state | Boolean (default FALSE). Whether to return the last state in addition to the output. |
| go_backwards | Boolean (default FALSE). If TRUE, process the input sequence backwards and return the reversed sequence. |
| stateful | Boolean (default FALSE). If TRUE, the last state for each sample at index i in a batch will be used as initial state for the sample of index i in the following batch. |
| unroll | Boolean (default FALSE). If TRUE, the network will be unrolled, else a symbolic loop will be used. Unrolling can speed-up a RNN, although it tends to be more memory-intensive. Unrolling is only suitable for short sequences. |
| kernel_initializer | Initializer for the `kernel` weights matrix, used for the linear transformation of the inputs. |
| recurrent_initializer | Initializer for the `recurrent_kernel` weights matrix, used for the linear transformation of the recurrent state. |
| bias_initializer | Initializer for the bias vector. |
| kernel_regularizer | Regularizer function applied to the `kernel` weights matrix. |
| recurrent_regularizer | Regularizer function applied to the `recurrent_kernel` weights matrix. |
| bias_regularizer | Regularizer function applied to the bias vector. |
| activity_regularizer | Regularizer function applied to the output of the layer (its "activation").. |
| kernel_constraint | Constraint function applied to the `kernel` weights matrix. |
| recurrent_constraint | Constraint function applied to the `recurrent_kernel` weights matrix. |
| bias_constraint | Constraint function applied to the bias vector. |
| dropout | Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. |
| recurrent_dropout | Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. |
| ... | Standard Layer args. |
## Section
## Input shapes
N-D tensor with shape `(batch_size, timesteps, ...)`, or `(timesteps, batch_size, ...)` when `time_major = TRUE`.
## Output shape
- if `return_state`: a list of tensors. The first tensor is the output. The remaining tensors are the last states, each with shape `(batch_size, state_size)`, where `state_size` could be a high dimension tensor shape.
- if `return_sequences`: N-D tensor with shape `[batch_size, timesteps, output_size]`, where `output_size` could be a high dimension tensor shape, or `[timesteps, batch_size, output_size]` when `time_major` is `TRUE`
- else, N-D tensor with shape `[batch_size, output_size]`, where `output_size` could be a high dimension tensor shape.
## Masking
This layer supports masking for input data with a variable number of timesteps. To introduce masks to your data, use `layer_embedding()` with the `mask_zero` parameter set to `TRUE`.
## Statefulness in RNNs
You can set RNN layers to be 'stateful', which means that the states computed for the samples in one batch will be reused as initial states for the samples in the next batch. This assumes a one-to-one mapping between samples in different successive batches. For intuition behind statefulness, there is a helpful blog post here: [https://philipperemy.github.io/keras-stateful-lstm/](https://philipperemy.github.io/keras-stateful-lstm/) To enable statefulness:
- Specify `stateful = TRUE` in the layer constructor.
- Specify a fixed batch size for your model. For sequential models, pass `batch_input_shape = list(...)` to the first layer in your model. For functional models with 1 or more Input layers, pass `batch_shape = list(...)` to all the first layers in your model. This is the expected shape of your inputs **including the batch size**. It should be a list of integers, e.g. `list(32, 10, 100)`. For dimensions which can vary (are not known ahead of time), use `NULL` in place of an integer, e.g. `list(32, NULL, NULL)`.
- Specify `shuffle = FALSE` when calling `fit()`.
To reset the states of your model, call `layer$reset_states()` on either a specific layer, or on your entire model.
## Initial State of RNNs
You can specify the initial state of RNN layers symbolically by calling them with the keyword argument `initial_state.` The value of initial_state should be a tensor or list of tensors representing the initial state of the RNN layer. You can specify the initial state of RNN layers numerically by calling `reset_states` with the named argument `states.` The value of `states` should be an array or list of arrays representing the initial state of the RNN layer.
## Passing external constants to RNNs
You can pass "external" constants to the cell using the `constants` named argument of `RNN$__call__` (as well as `RNN$call`) method. This requires that the `cell$call` method accepts the same keyword argument `constants`. Such constants can be used to condition the cell transformation on additional static inputs (not changing over time), a.k.a. an attention mechanism.
## References
- [A Theoretically Grounded Application of Dropout in Recurrent Neural Networks](https://arxiv.org/abs/1512.05287)
## See Also
- [https://www.tensorflow.org/guide/keras/rnn](https://www.tensorflow.org/guide/keras/rnn)
Other recurrent layers: `layer_cudnn_gru()`, `layer_cudnn_lstm()`, `layer_gru()`, `layer_lstm()`, `layer_rnn()`