GRU#
GRU - 14#
Version
name: GRU (GitHub)
domain: main
since_version: 14
function: False
support_level: SupportType.COMMON
shape inference: True
This version of the operator has been available since version 14.
Summary
Computes an one-layer GRU. This operator is usually supported via some custom implementation such as CuDNN.
Notations:
X - input tensor
z - update gate
r - reset gate
h - hidden gate
t - time step (t-1 means previous time step)
W[zrh] - W parameter weight matrix for update, reset, and hidden gates
R[zrh] - R recurrence weight matrix for update, reset, and hidden gates
Wb[zrh] - W bias vectors for update, reset, and hidden gates
Rb[zrh] - R bias vectors for update, reset, and hidden gates
WB[zrh] - W parameter weight matrix for backward update, reset, and hidden gates
RB[zrh] - R recurrence weight matrix for backward update, reset, and hidden gates
WBb[zrh] - W bias vectors for backward update, reset, and hidden gates
RBb[zrh] - R bias vectors for backward update, reset, and hidden gates
H - Hidden state
num_directions - 2 if direction == bidirectional else 1
Activation functions:
Relu(x) - max(0, x)
Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})
Sigmoid(x) - 1/(1 + e^{-x})
(NOTE: Below are optional)
Affine(x) - alpha*x + beta
LeakyRelu(x) - x if x >= 0 else alpha * x
ThresholdedRelu(x) - x if x >= alpha else 0
ScaledTanh(x) - alpha*Tanh(beta*x)
HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)
Elu(x) - x if x >= 0 else alpha*(e^x - 1)
Softsign(x) - x/(1 + |x|)
Softplus(x) - log(1 + e^x)
Equations (Default: f=Sigmoid, g=Tanh):
zt = f(Xt*(Wz^T) + Ht-1*(Rz^T) + Wbz + Rbz)
rt = f(Xt*(Wr^T) + Ht-1*(Rr^T) + Wbr + Rbr)
ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*(Rh^T) + Rbh + Wbh) # default, when linear_before_reset = 0
ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*(Rh^T) + Rbh)) + Wbh) # when linear_before_reset != 0
Ht = (1 - zt) (.) ht + zt (.) Ht-1
This operator has optional inputs/outputs. See ONNX for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument’s name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.
Attributes
activation_alpha: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01.
activation_beta: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.
activations: A list of 2 (or 4 if bidirectional) activation functions for update, reset, and hidden gates. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified.
clip: Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified.
direction: Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional. Default value is
'forward'
.hidden_size: Number of neurons in the hidden layer
layout: The shape format of inputs X, initial_h and outputs Y, Y_h. If 0, the following shapes are expected: X.shape = [seq_length, batch_size, input_size], Y.shape = [seq_length, num_directions, batch_size, hidden_size], initial_h.shape = Y_h.shape = [num_directions, batch_size, hidden_size]. If 1, the following shapes are expected: X.shape = [batch_size, seq_length, input_size], Y.shape = [batch_size, seq_length, num_directions, hidden_size], initial_h.shape = Y_h.shape = [batch_size, num_directions, hidden_size]. Default value is
0
.linear_before_reset: When computing the output of the hidden gate, apply the linear transformation before multiplying by the output of the reset gate. Default value is
0
.
Inputs
Between 3 and 6 inputs.
X (heterogeneous) - T: The input sequences packed (and potentially padded) into one 3-D tensor with the shape of [seq_length, batch_size, input_size].
W (heterogeneous) - T: The weight tensor for the gates. Concatenation of W[zrh] and WB[zrh] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 3*hidden_size, input_size].
R (heterogeneous) - T: The recurrence weight tensor. Concatenation of R[zrh] and RB[zrh] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 3*hidden_size, hidden_size].
B (optional, heterogeneous) - T: The bias tensor for the gates. Concatenation of [Wb[zrh], Rb[zrh]] and [WBb[zrh], RBb[zrh]] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 6*hidden_size]. Optional: If not specified - assumed to be 0
sequence_lens (optional, heterogeneous) - T1: Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length seq_length. It has shape [batch_size].
initial_h (optional, heterogeneous) - T: Optional initial value of the hidden. If not specified - assumed to be 0. It has shape [num_directions, batch_size, hidden_size].
Outputs
Between 0 and 2 outputs.
Y (optional, heterogeneous) - T: A tensor that concats all the intermediate output values of the hidden. It has shape [seq_length, num_directions, batch_size, hidden_size].
Y_h (optional, heterogeneous) - T: The last output value of the hidden. It has shape [num_directions, batch_size, hidden_size].
Type Constraints
T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.
T1 in ( tensor(int32) ): Constrain seq_lens to integer tensor.
Examples
defaults
input = np.array([[[1., 2.], [3., 4.], [5., 6.]]]).astype(np.float32)
input_size = 2
hidden_size = 5
weight_scale = 0.1
number_of_gates = 3
node = onnx.helper.make_node(
'GRU',
inputs=['X', 'W', 'R'],
outputs=['', 'Y_h'],
hidden_size=hidden_size
)
W = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)
R = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)
gru = GRU_Helper(X=input, W=W, R=R)
_, Y_h = gru.step()
expect(node, inputs=[input, W, R], outputs=[Y_h.astype(np.float32)], name='test_gru_defaults')
initial_bias
input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]]).astype(np.float32)
input_size = 3
hidden_size = 3
weight_scale = 0.1
custom_bias = 0.1
number_of_gates = 3
node = onnx.helper.make_node(
'GRU',
inputs=['X', 'W', 'R', 'B'],
outputs=['', 'Y_h'],
hidden_size=hidden_size
)
W = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)
R = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)
# Adding custom bias
W_B = custom_bias * np.ones((1, number_of_gates * hidden_size)).astype(np.float32)
R_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)
B = np.concatenate((W_B, R_B), axis=1)
gru = GRU_Helper(X=input, W=W, R=R, B=B)
_, Y_h = gru.step()
expect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_with_initial_bias')
seq_length
input = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]],
[[10., 11., 12.], [13., 14., 15.], [16., 17., 18.]]]).astype(np.float32)
input_size = 3
hidden_size = 5
number_of_gates = 3
node = onnx.helper.make_node(
'GRU',
inputs=['X', 'W', 'R', 'B'],
outputs=['', 'Y_h'],
hidden_size=hidden_size
)
W = np.random.randn(1, number_of_gates * hidden_size, input_size).astype(np.float32)
R = np.random.randn(1, number_of_gates * hidden_size, hidden_size).astype(np.float32)
# Adding custom bias
W_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)
R_B = np.random.randn(1, number_of_gates * hidden_size).astype(np.float32)
B = np.concatenate((W_B, R_B), axis=1)
gru = GRU_Helper(X=input, W=W, R=R, B=B)
_, Y_h = gru.step()
expect(node, inputs=[input, W, R, B], outputs=[Y_h.astype(np.float32)], name='test_gru_seq_length')
batchwise
input = np.array([[[1., 2.]], [[3., 4.]], [[5., 6.]]]).astype(np.float32)
input_size = 2
hidden_size = 6
number_of_gates = 3
weight_scale = 0.2
layout = 1
node = onnx.helper.make_node(
'GRU',
inputs=['X', 'W', 'R'],
outputs=['Y', 'Y_h'],
hidden_size=hidden_size,
layout=layout
)
W = weight_scale * np.ones((1, number_of_gates * hidden_size, input_size)).astype(np.float32)
R = weight_scale * np.ones((1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)
gru = GRU_Helper(X=input, W=W, R=R, layout=layout)
Y, Y_h = gru.step()
expect(node, inputs=[input, W, R], outputs=[Y.astype(np.float32), Y_h.astype(np.float32)], name='test_gru_batchwise')
Differences
0 | 0 | Computes an one-layer GRU. This operator is usually supported via some custom | Computes an one-layer GRU. This operator is usually supported via some custom |
1 | 1 | implementation such as CuDNN. | implementation such as CuDNN. |
2 | 2 |
|
|
3 | 3 | Notations: | Notations: |
4 | 4 |
|
|
5 | 5 | X - input tensor | X - input tensor |
6 | 6 |
|
|
7 | 7 | z - update gate | z - update gate |
8 | 8 |
|
|
9 | 9 | r - reset gate | r - reset gate |
10 | 10 |
|
|
11 | 11 | h - hidden gate | h - hidden gate |
12 | 12 |
|
|
13 | 13 | t - time step (t-1 means previous time step) | t - time step (t-1 means previous time step) |
14 | 14 |
|
|
15 | 15 | W[zrh] - W parameter weight matrix for update, reset, and hidden gates | W[zrh] - W parameter weight matrix for update, reset, and hidden gates |
16 | 16 |
|
|
17 | 17 | R[zrh] - R recurrence weight matrix for update, reset, and hidden gates | R[zrh] - R recurrence weight matrix for update, reset, and hidden gates |
18 | 18 |
|
|
19 | 19 | Wb[zrh] - W bias vectors for update, reset, and hidden gates | Wb[zrh] - W bias vectors for update, reset, and hidden gates |
20 | 20 |
|
|
21 | 21 | Rb[zrh] - R bias vectors for update, reset, and hidden gates | Rb[zrh] - R bias vectors for update, reset, and hidden gates |
22 | 22 |
|
|
23 | 23 | WB[zrh] - W parameter weight matrix for backward update, reset, and hidden gates | WB[zrh] - W parameter weight matrix for backward update, reset, and hidden gates |
24 | 24 |
|
|
25 | 25 | RB[zrh] - R recurrence weight matrix for backward update, reset, and hidden gates | RB[zrh] - R recurrence weight matrix for backward update, reset, and hidden gates |
26 | 26 |
|
|
27 | 27 | WBb[zrh] - W bias vectors for backward update, reset, and hidden gates | WBb[zrh] - W bias vectors for backward update, reset, and hidden gates |
28 | 28 |
|
|
29 | 29 | RBb[zrh] - R bias vectors for backward update, reset, and hidden gates | RBb[zrh] - R bias vectors for backward update, reset, and hidden gates |
30 | 30 |
|
|
31 | 31 | H - Hidden state | H - Hidden state |
32 | 32 |
|
|
33 | 33 | num_directions - 2 if direction == bidirectional else 1 | num_directions - 2 if direction == bidirectional else 1 |
34 | 34 |
|
|
35 | 35 | Activation functions: | Activation functions: |
36 | 36 |
|
|
37 | 37 | Relu(x) - max(0, x) | Relu(x) - max(0, x) |
38 | 38 |
|
|
39 | 39 | Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x}) | Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x}) |
40 | 40 |
|
|
41 | 41 | Sigmoid(x) - 1/(1 + e^{-x}) | Sigmoid(x) - 1/(1 + e^{-x}) |
42 | 42 |
|
|
43 | 43 | (NOTE: Below are optional) | (NOTE: Below are optional) |
44 | 44 |
|
|
45 | 45 | Affine(x) - alpha*x + beta | Affine(x) - alpha*x + beta |
46 | 46 |
|
|
47 | 47 | LeakyRelu(x) - x if x >= 0 else alpha * x | LeakyRelu(x) - x if x >= 0 else alpha * x |
48 | 48 |
|
|
49 | 49 | ThresholdedRelu(x) - x if x >= alpha else 0 | ThresholdedRelu(x) - x if x >= alpha else 0 |
50 | 50 |
|
|
51 | 51 | ScaledTanh(x) - alpha*Tanh(beta*x) | ScaledTanh(x) - alpha*Tanh(beta*x) |
52 | 52 |
|
|
53 | 53 | HardSigmoid(x) - min(max(alpha*x + beta, 0), 1) | HardSigmoid(x) - min(max(alpha*x + beta, 0), 1) |
54 | 54 |
|
|
55 | 55 | Elu(x) - x if x >= 0 else alpha*(e^x - 1) | Elu(x) - x if x >= 0 else alpha*(e^x - 1) |
56 | 56 |
|
|
57 | 57 | Softsign(x) - x/(1 + |x|) | Softsign(x) - x/(1 + |x|) |
58 | 58 |
|
|
59 | 59 | Softplus(x) - log(1 + e^x) | Softplus(x) - log(1 + e^x) |
60 | 60 |
|
|
61 | 61 | Equations (Default: f=Sigmoid, g=Tanh): | Equations (Default: f=Sigmoid, g=Tanh): |
62 | 62 |
|
|
63 | 63 | - zt = f(Xt*(Wz^T) + Ht-1*(Rz^T) + Wbz + Rbz) | - zt = f(Xt*(Wz^T) + Ht-1*(Rz^T) + Wbz + Rbz) |
64 | 64 |
|
|
65 | 65 | - rt = f(Xt*(Wr^T) + Ht-1*(Rr^T) + Wbr + Rbr) | - rt = f(Xt*(Wr^T) + Ht-1*(Rr^T) + Wbr + Rbr) |
66 | 66 |
|
|
67 | 67 | - ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*(Rh^T) + Rbh + Wbh) # default, when linear_before_reset = 0 | - ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*(Rh^T) + Rbh + Wbh) # default, when linear_before_reset = 0 |
68 | 68 |
|
|
69 | 69 | - ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*(Rh^T) + Rbh)) + Wbh) # when linear_before_reset != 0 | - ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*(Rh^T) + Rbh)) + Wbh) # when linear_before_reset != 0 |
70 | 70 |
|
|
71 | 71 | - Ht = (1 - zt) (.) ht + zt (.) Ht-1 | - Ht = (1 - zt) (.) ht + zt (.) Ht-1 |
72 | 72 | This operator has **optional** inputs/outputs. See ONNX | This operator has **optional** inputs/outputs. See ONNX |
73 | 73 |
|
|
74 | 74 | **Attributes** | **Attributes** |
75 | 75 |
|
|
76 | 76 | * **activation_alpha**: | * **activation_alpha**: |
77 | 77 | Optional scaling values used by some activation functions. The | Optional scaling values used by some activation functions. The |
78 | 78 | values are consumed in the order of activation functions, for | values are consumed in the order of activation functions, for |
79 | 79 | example (f, g, h) in LSTM. Default values are the same as of | example (f, g, h) in LSTM. Default values are the same as of |
80 | 80 | corresponding ONNX operators.For example with LeakyRelu, the default | corresponding ONNX operators.For example with LeakyRelu, the default |
81 | 81 | alpha is 0.01. | alpha is 0.01. |
82 | 82 | * **activation_beta**: | * **activation_beta**: |
83 | 83 | Optional scaling values used by some activation functions. The | Optional scaling values used by some activation functions. The |
84 | 84 | values are consumed in the order of activation functions, for | values are consumed in the order of activation functions, for |
85 | 85 | example (f, g, h) in LSTM. Default values are the same as of | example (f, g, h) in LSTM. Default values are the same as of |
86 | 86 | corresponding ONNX operators. | corresponding ONNX operators. |
87 | 87 | * **activations**: | * **activations**: |
88 | 88 | A list of 2 (or 4 if bidirectional) activation functions for update, | A list of 2 (or 4 if bidirectional) activation functions for update, |
89 | 89 | reset, and hidden gates. The activation functions must be one of the | reset, and hidden gates. The activation functions must be one of the |
90 | 90 | activation functions specified above. Optional: See the equations | activation functions specified above. Optional: See the equations |
91 | 91 | for default if not specified. | for default if not specified. |
92 | 92 | * **clip**: | * **clip**: |
93 | 93 | Cell clip threshold. Clipping bounds the elements of a tensor in the | Cell clip threshold. Clipping bounds the elements of a tensor in the |
94 | 94 | range of [-threshold, +threshold] and is applied to the input of | range of [-threshold, +threshold] and is applied to the input of |
95 | 95 | activations. No clip if not specified. | activations. No clip if not specified. |
96 | 96 | * **direction**: | * **direction**: |
97 | 97 | Specify if the RNN is forward, reverse, or bidirectional. Must be | Specify if the RNN is forward, reverse, or bidirectional. Must be |
98 | 98 | one of forward (default), reverse, or bidirectional. Default value is 'forward'. | one of forward (default), reverse, or bidirectional. Default value is 'forward'. |
99 | 99 | * **hidden_size**: | * **hidden_size**: |
100 | 100 | Number of neurons in the hidden layer | Number of neurons in the hidden layer |
101 | * **layout**: | ||
102 | The shape format of inputs X, initial_h and outputs Y, Y_h. If 0, | ||
103 | the following shapes are expected: X.shape = [seq_length, | ||
104 | batch_size, input_size], Y.shape = [seq_length, num_directions, | ||
105 | batch_size, hidden_size], initial_h.shape = Y_h.shape = | ||
106 | [num_directions, batch_size, hidden_size]. If 1, the following | ||
107 | shapes are expected: X.shape = [batch_size, seq_length, input_size], | ||
108 | Y.shape = [batch_size, seq_length, num_directions, hidden_size], | ||
109 | initial_h.shape = Y_h.shape = [batch_size, num_directions, | ||
110 | hidden_size]. Default value is 0. | ||
101 | 111 | * **linear_before_reset**: | * **linear_before_reset**: |
102 | 112 | When computing the output of the hidden gate, apply the linear | When computing the output of the hidden gate, apply the linear |
103 | 113 | transformation before multiplying by the output of the reset gate. Default value is 0. | transformation before multiplying by the output of the reset gate. Default value is 0. |
104 | 114 |
|
|
105 | 115 | **Inputs** | **Inputs** |
106 | 116 |
|
|
107 | 117 | Between 3 and 6 inputs. | Between 3 and 6 inputs. |
108 | 118 |
|
|
109 | 119 | * **X** (heterogeneous) - **T**: | * **X** (heterogeneous) - **T**: |
110 | 120 | The input sequences packed (and potentially padded) into one 3-D | The input sequences packed (and potentially padded) into one 3-D |
111 | 121 | tensor with the shape of [seq_length, batch_size, input_size]. | tensor with the shape of [seq_length, batch_size, input_size]. |
112 | 122 | * **W** (heterogeneous) - **T**: | * **W** (heterogeneous) - **T**: |
113 | 123 | The weight tensor for the gates. Concatenation of W[zrh] and | The weight tensor for the gates. Concatenation of W[zrh] and |
114 | 124 | WB[zrh] (if bidirectional) along dimension 0. This tensor has | WB[zrh] (if bidirectional) along dimension 0. This tensor has |
115 | 125 | shape [num_directions, 3*hidden_size, input_size]. | shape [num_directions, 3*hidden_size, input_size]. |
116 | 126 | * **R** (heterogeneous) - **T**: | * **R** (heterogeneous) - **T**: |
117 | 127 | The recurrence weight tensor. Concatenation of R[zrh] and | The recurrence weight tensor. Concatenation of R[zrh] and |
118 | 128 | RB[zrh] (if bidirectional) along dimension 0. This tensor has | RB[zrh] (if bidirectional) along dimension 0. This tensor has |
119 | 129 | shape [num_directions, 3*hidden_size, hidden_size]. | shape [num_directions, 3*hidden_size, hidden_size]. |
120 | 130 | * **B** (optional, heterogeneous) - **T**: | * **B** (optional, heterogeneous) - **T**: |
121 | 131 | The bias tensor for the gates. Concatenation of [Wb[zrh], Rb[zrh]] | The bias tensor for the gates. Concatenation of [Wb[zrh], Rb[zrh]] |
122 | 132 | and [WBb[zrh], RBb[zrh]] (if bidirectional) along dimension 0. | and [WBb[zrh], RBb[zrh]] (if bidirectional) along dimension 0. |
123 | 133 | This tensor has shape [num_directions, 6*hidden_size]. Optional: | This tensor has shape [num_directions, 6*hidden_size]. Optional: |
124 | 134 | If not specified - assumed to be 0 | If not specified - assumed to be 0 |
125 | 135 | * **sequence_lens** (optional, heterogeneous) - **T1**: | * **sequence_lens** (optional, heterogeneous) - **T1**: |
126 | 136 | Optional tensor specifying lengths of the sequences in a batch. If | Optional tensor specifying lengths of the sequences in a batch. If |
127 | 137 | not specified - assumed all sequences in the batch to have length | not specified - assumed all sequences in the batch to have length |
128 | 138 | seq_length. It has shape [batch_size]. | seq_length. It has shape [batch_size]. |
129 | 139 | * **initial_h** (optional, heterogeneous) - **T**: | * **initial_h** (optional, heterogeneous) - **T**: |
130 | 140 | Optional initial value of the hidden. If not specified - assumed to | Optional initial value of the hidden. If not specified - assumed to |
131 | 141 | be 0. It has shape [num_directions, batch_size, hidden_size]. | be 0. It has shape [num_directions, batch_size, hidden_size]. |
132 | 142 |
|
|
133 | 143 | **Outputs** | **Outputs** |
134 | 144 |
|
|
135 | 145 | Between 0 and 2 outputs. | Between 0 and 2 outputs. |
136 | 146 |
|
|
137 | 147 | * **Y** (optional, heterogeneous) - **T**: | * **Y** (optional, heterogeneous) - **T**: |
138 | 148 | A tensor that concats all the intermediate output values of the | A tensor that concats all the intermediate output values of the |
139 | 149 | hidden. It has shape [seq_length, num_directions, batch_size, | hidden. It has shape [seq_length, num_directions, batch_size, |
140 | 150 | hidden_size]. | hidden_size]. |
141 | 151 | * **Y_h** (optional, heterogeneous) - **T**: | * **Y_h** (optional, heterogeneous) - **T**: |
142 | 152 | The last output value of the hidden. It has shape [num_directions, | The last output value of the hidden. It has shape [num_directions, |
143 | 153 | batch_size, hidden_size]. | batch_size, hidden_size]. |
144 | 154 |
|
|
145 | 155 | **Type Constraints** | **Type Constraints** |
146 | 156 |
|
|
147 | 157 | * **T** in ( | * **T** in ( |
148 | 158 | tensor(double), | tensor(double), |
149 | 159 | tensor(float), | tensor(float), |
150 | 160 | tensor(float16) | tensor(float16) |
151 | 161 | ): | ): |
152 | 162 | Constrain input and output types to float tensors. | Constrain input and output types to float tensors. |
153 | 163 | * **T1** in ( | * **T1** in ( |
154 | 164 | tensor(int32) | tensor(int32) |
155 | 165 | ): | ): |
156 | 166 | Constrain seq_lens to integer tensor. | Constrain seq_lens to integer tensor. |
GRU - 7#
Version
name: GRU (GitHub)
domain: main
since_version: 7
function: False
support_level: SupportType.COMMON
shape inference: True
This version of the operator has been available since version 7.
Summary
Computes an one-layer GRU. This operator is usually supported via some custom implementation such as CuDNN.
Notations:
X - input tensor
z - update gate
r - reset gate
h - hidden gate
t - time step (t-1 means previous time step)
W[zrh] - W parameter weight matrix for update, reset, and hidden gates
R[zrh] - R recurrence weight matrix for update, reset, and hidden gates
Wb[zrh] - W bias vectors for update, reset, and hidden gates
Rb[zrh] - R bias vectors for update, reset, and hidden gates
WB[zrh] - W parameter weight matrix for backward update, reset, and hidden gates
RB[zrh] - R recurrence weight matrix for backward update, reset, and hidden gates
WBb[zrh] - W bias vectors for backward update, reset, and hidden gates
RBb[zrh] - R bias vectors for backward update, reset, and hidden gates
H - Hidden state
num_directions - 2 if direction == bidirectional else 1
Activation functions:
Relu(x) - max(0, x)
Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})
Sigmoid(x) - 1/(1 + e^{-x})
(NOTE: Below are optional)
Affine(x) - alpha*x + beta
LeakyRelu(x) - x if x >= 0 else alpha * x
ThresholdedRelu(x) - x if x >= alpha else 0
ScaledTanh(x) - alpha*Tanh(beta*x)
HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)
Elu(x) - x if x >= 0 else alpha*(e^x - 1)
Softsign(x) - x/(1 + |x|)
Softplus(x) - log(1 + e^x)
Equations (Default: f=Sigmoid, g=Tanh):
zt = f(Xt*(Wz^T) + Ht-1*(Rz^T) + Wbz + Rbz)
rt = f(Xt*(Wr^T) + Ht-1*(Rr^T) + Wbr + Rbr)
ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*(Rh^T) + Rbh + Wbh) # default, when linear_before_reset = 0
ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*(Rh^T) + Rbh)) + Wbh) # when linear_before_reset != 0
Ht = (1 - zt) (.) ht + zt (.) Ht-1
This operator has optional inputs/outputs. See ONNX for more details about the representation of optional arguments. An empty string may be used in the place of an actual argument’s name to indicate a missing argument. Trailing optional arguments (those not followed by an argument that is present) may also be simply omitted.
Attributes
activation_alpha: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01.
activation_beta: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.
activations: A list of 2 (or 4 if bidirectional) activation functions for update, reset, and hidden gates. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified.
clip: Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified.
direction: Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional. Default value is
'forward'
.hidden_size: Number of neurons in the hidden layer
linear_before_reset: When computing the output of the hidden gate, apply the linear transformation before multiplying by the output of the reset gate. Default value is
0
.
Inputs
Between 3 and 6 inputs.
X (heterogeneous) - T: The input sequences packed (and potentially padded) into one 3-D tensor with the shape of [seq_length, batch_size, input_size].
W (heterogeneous) - T: The weight tensor for the gates. Concatenation of W[zrh] and WB[zrh] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 3*hidden_size, input_size].
R (heterogeneous) - T: The recurrence weight tensor. Concatenation of R[zrh] and RB[zrh] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 3*hidden_size, hidden_size].
B (optional, heterogeneous) - T: The bias tensor for the gates. Concatenation of [Wb[zrh], Rb[zrh]] and [WBb[zrh], RBb[zrh]] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 6*hidden_size]. Optional: If not specified - assumed to be 0
sequence_lens (optional, heterogeneous) - T1: Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length seq_length. It has shape [batch_size].
initial_h (optional, heterogeneous) - T: Optional initial value of the hidden. If not specified - assumed to be 0. It has shape [num_directions, batch_size, hidden_size].
Outputs
Between 0 and 2 outputs.
Y (optional, heterogeneous) - T: A tensor that concats all the intermediate output values of the hidden. It has shape [seq_length, num_directions, batch_size, hidden_size].
Y_h (optional, heterogeneous) - T: The last output value of the hidden. It has shape [num_directions, batch_size, hidden_size].
Type Constraints
T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.
T1 in ( tensor(int32) ): Constrain seq_lens to integer tensor.
Differences
0 | 0 | Computes an one-layer GRU. This operator is usually supported via some custom | Computes an one-layer GRU. This operator is usually supported via some custom |
1 | 1 | implementation such as CuDNN. | implementation such as CuDNN. |
2 | 2 |
|
|
3 | 3 | Notations: | Notations: |
4 | 4 |
|
|
5 | 5 | X - input tensor | X - input tensor |
6 | 6 |
|
|
7 | 7 | z - update gate | z - update gate |
8 | 8 |
|
|
9 | 9 | r - reset gate | r - reset gate |
10 | 10 |
|
|
11 | 11 | h - hidden gate | h - hidden gate |
12 | 12 |
|
|
13 | 13 | t - time step (t-1 means previous time step) | t - time step (t-1 means previous time step) |
14 | 14 |
|
|
15 | 15 | W[zrh] - W parameter weight matrix for update, reset, and hidden gates | W[zrh] - W parameter weight matrix for update, reset, and hidden gates |
16 | 16 |
|
|
17 | 17 | R[zrh] - R recurrence weight matrix for update, reset, and hidden gates | R[zrh] - R recurrence weight matrix for update, reset, and hidden gates |
18 | 18 |
|
|
19 | 19 | Wb[zrh] - W bias vectors for update, reset, and hidden gates | Wb[zrh] - W bias vectors for update, reset, and hidden gates |
20 | 20 |
|
|
21 | 21 | Rb[zrh] - R bias vectors for update, reset, and hidden gates | Rb[zrh] - R bias vectors for update, reset, and hidden gates |
22 | 22 |
|
|
23 | 23 | WB[zrh] - W parameter weight matrix for backward update, reset, and hidden gates | WB[zrh] - W parameter weight matrix for backward update, reset, and hidden gates |
24 | 24 |
|
|
25 | 25 | RB[zrh] - R recurrence weight matrix for backward update, reset, and hidden gates | RB[zrh] - R recurrence weight matrix for backward update, reset, and hidden gates |
26 | 26 |
|
|
27 | 27 | WBb[zrh] - W bias vectors for backward update, reset, and hidden gates | WBb[zrh] - W bias vectors for backward update, reset, and hidden gates |
28 | 28 |
|
|
29 | 29 | RBb[zrh] - R bias vectors for backward update, reset, and hidden gates | RBb[zrh] - R bias vectors for backward update, reset, and hidden gates |
30 | 30 |
|
|
31 | 31 | H - Hidden state | H - Hidden state |
32 | 32 |
|
|
33 | 33 | num_directions - 2 if direction == bidirectional else 1 | num_directions - 2 if direction == bidirectional else 1 |
34 | 34 |
|
|
35 | 35 | Activation functions: | Activation functions: |
36 | 36 |
|
|
37 | 37 | Relu(x) - max(0, x) | Relu(x) - max(0, x) |
38 | 38 |
|
|
39 | 39 | Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x}) | Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x}) |
40 | 40 |
|
|
41 | 41 | Sigmoid(x) - 1/(1 + e^{-x}) | Sigmoid(x) - 1/(1 + e^{-x}) |
42 | 42 |
|
|
43 | 43 | (NOTE: Below are optional) | (NOTE: Below are optional) |
44 | 44 |
|
|
45 | 45 | Affine(x) - alpha*x + beta | Affine(x) - alpha*x + beta |
46 | 46 |
|
|
47 | 47 | LeakyRelu(x) - x if x >= 0 else alpha * x | LeakyRelu(x) - x if x >= 0 else alpha * x |
48 | 48 |
|
|
49 | 49 | ThresholdedRelu(x) - x if x >= alpha else 0 | ThresholdedRelu(x) - x if x >= alpha else 0 |
50 | 50 |
|
|
51 | 51 | ScaledTanh(x) - alpha*Tanh(beta*x) | ScaledTanh(x) - alpha*Tanh(beta*x) |
52 | 52 |
|
|
53 | 53 | HardSigmoid(x) - min(max(alpha*x + beta, 0), 1) | HardSigmoid(x) - min(max(alpha*x + beta, 0), 1) |
54 | 54 |
|
|
55 | 55 | Elu(x) - x if x >= 0 else alpha*(e^x - 1) | Elu(x) - x if x >= 0 else alpha*(e^x - 1) |
56 | 56 |
|
|
57 | 57 | Softsign(x) - x/(1 + |x|) | Softsign(x) - x/(1 + |x|) |
58 | 58 |
|
|
59 | 59 | Softplus(x) - log(1 + e^x) | Softplus(x) - log(1 + e^x) |
60 | 60 |
|
|
61 | 61 | Equations (Default: f=Sigmoid, g=Tanh): | Equations (Default: f=Sigmoid, g=Tanh): |
62 | 62 |
|
|
63 | 63 | - zt = f(Xt*(Wz^T) + Ht-1*Rz + Wbz + Rbz) |
|
64 | 64 |
|
|
65 | 65 | - rt = f(Xt*(Wr^T) + Ht-1*Rr + Wbr + Rbr) |
|
66 | 66 |
|
|
67 | 67 | - ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*Rh + Rbh + Wbh) # default, when linear_before_reset = 0 |
|
68 | 68 |
|
|
69 | 69 | - ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*Rh + Rbh) + Wbh) # when linear_before_reset != 0 |
|
70 | 70 |
|
|
71 | - Ht = (1 - zt) (.) ht + zt (.) Ht-1 | ||
71 | 72 | - Ht = (1 - zt) (.) ht + zt (.) Ht-1 |
|
72 | 73 |
|
|
73 | 74 | **Attributes** | **Attributes** |
74 | 75 |
|
|
75 | 76 | * **activation_alpha**: | * **activation_alpha**: |
76 | 77 | Optional scaling values used by some activation functions. The | Optional scaling values used by some activation functions. The |
77 | 78 | values are consumed in the order of activation functions, for | values are consumed in the order of activation functions, for |
78 | 79 | example (f, g, h) in LSTM. Default values are the same as of | example (f, g, h) in LSTM. Default values are the same as of |
79 | 80 | corresponding ONNX operators.For example with LeakyRelu, the default | corresponding ONNX operators.For example with LeakyRelu, the default |
80 | 81 | alpha is 0.01. | alpha is 0.01. |
81 | 82 | * **activation_beta**: | * **activation_beta**: |
82 | 83 | Optional scaling values used by some activation functions. The | Optional scaling values used by some activation functions. The |
83 | 84 | values are consumed in the order of activation functions, for | values are consumed in the order of activation functions, for |
84 | 85 | example (f, g, h) in LSTM. Default values are the same as of | example (f, g, h) in LSTM. Default values are the same as of |
85 | 86 | corresponding ONNX operators. | corresponding ONNX operators. |
86 | 87 | * **activations**: | * **activations**: |
87 | 88 | A list of 2 (or 4 if bidirectional) activation functions for update, | A list of 2 (or 4 if bidirectional) activation functions for update, |
88 | 89 | reset, and hidden gates. The activation functions must be one of the | reset, and hidden gates. The activation functions must be one of the |
89 | 90 | activation functions specified above. Optional: See the equations | activation functions specified above. Optional: See the equations |
90 | 91 | for default if not specified. | for default if not specified. |
91 | 92 | * **clip**: | * **clip**: |
92 | 93 | Cell clip threshold. Clipping bounds the elements of a tensor in the | Cell clip threshold. Clipping bounds the elements of a tensor in the |
93 | 94 | range of [-threshold, +threshold] and is applied to the input of | range of [-threshold, +threshold] and is applied to the input of |
94 | 95 | activations. No clip if not specified. | activations. No clip if not specified. |
95 | 96 | * **direction**: | * **direction**: |
96 | 97 | Specify if the RNN is forward, reverse, or bidirectional. Must be | Specify if the RNN is forward, reverse, or bidirectional. Must be |
97 | 98 | one of forward (default), reverse, or bidirectional. Default value is 'forward'. | one of forward (default), reverse, or bidirectional. Default value is 'forward'. |
98 | 99 | * **hidden_size**: | * **hidden_size**: |
99 | 100 | Number of neurons in the hidden layer | Number of neurons in the hidden layer |
100 | 101 | * **linear_before_reset**: | * **linear_before_reset**: |
101 | 102 | When computing the output of the hidden gate, apply the linear | When computing the output of the hidden gate, apply the linear |
102 | 103 | transformation before multiplying by the output of the reset gate. Default value is 0. | transformation before multiplying by the output of the reset gate. Default value is 0. |
103 | * **output_sequence**: | ||
104 | The sequence output for the hidden is optional if 0. Default 0. Default value is 0. | ||
105 | 104 |
|
|
106 | 105 | **Inputs** | **Inputs** |
107 | 106 |
|
|
108 | 107 | Between 3 and 6 inputs. | Between 3 and 6 inputs. |
109 | 108 |
|
|
110 | 109 | * **X** (heterogeneous) - **T**: | * **X** (heterogeneous) - **T**: |
111 | 110 | The input sequences packed (and potentially padded) into one 3-D | The input sequences packed (and potentially padded) into one 3-D |
112 | 111 | tensor with the shape of [seq_length, batch_size, input_size]. | tensor with the shape of [seq_length, batch_size, input_size]. |
113 | 112 | * **W** (heterogeneous) - **T**: | * **W** (heterogeneous) - **T**: |
114 | 113 | The weight tensor for the gates. Concatenation of W[zrh] and | The weight tensor for the gates. Concatenation of W[zrh] and |
115 | 114 | WB[zrh] (if bidirectional) along dimension 0. This tensor has | WB[zrh] (if bidirectional) along dimension 0. This tensor has |
116 | 115 | shape [num_directions, 3*hidden_size, input_size]. | shape [num_directions, 3*hidden_size, input_size]. |
117 | 116 | * **R** (heterogeneous) - **T**: | * **R** (heterogeneous) - **T**: |
118 | 117 | The recurrence weight tensor. Concatenation of R[zrh] and | The recurrence weight tensor. Concatenation of R[zrh] and |
119 | 118 | RB[zrh] (if bidirectional) along dimension 0. This tensor has | RB[zrh] (if bidirectional) along dimension 0. This tensor has |
120 | 119 | shape [num_directions, 3*hidden_size, hidden_size]. | shape [num_directions, 3*hidden_size, hidden_size]. |
121 | 120 | * **B** (optional, heterogeneous) - **T**: | * **B** (optional, heterogeneous) - **T**: |
122 | 121 | The bias tensor for the gates. Concatenation of [Wb[zrh], Rb[zrh]] | The bias tensor for the gates. Concatenation of [Wb[zrh], Rb[zrh]] |
123 | 122 | and [WBb[zrh], RBb[zrh]] (if bidirectional) along dimension 0. | and [WBb[zrh], RBb[zrh]] (if bidirectional) along dimension 0. |
124 | 123 | This tensor has shape [num_directions, 6*hidden_size]. Optional: | This tensor has shape [num_directions, 6*hidden_size]. Optional: |
125 | 124 | If not specified - assumed to be 0 | If not specified - assumed to be 0 |
126 | 125 | * **sequence_lens** (optional, heterogeneous) - **T1**: | * **sequence_lens** (optional, heterogeneous) - **T1**: |
127 | 126 | Optional tensor specifying lengths of the sequences in a batch. If | Optional tensor specifying lengths of the sequences in a batch. If |
128 | 127 | not specified - assumed all sequences in the batch to have length | not specified - assumed all sequences in the batch to have length |
129 | 128 | seq_length. It has shape [batch_size]. | seq_length. It has shape [batch_size]. |
130 | 129 | * **initial_h** (optional, heterogeneous) - **T**: | * **initial_h** (optional, heterogeneous) - **T**: |
131 | 130 | Optional initial value of the hidden. If not specified - assumed to | Optional initial value of the hidden. If not specified - assumed to |
132 | 131 | be 0. It has shape [num_directions, batch_size, hidden_size]. | be 0. It has shape [num_directions, batch_size, hidden_size]. |
133 | 132 |
|
|
134 | 133 | **Outputs** | **Outputs** |
135 | 134 |
|
|
136 | 135 | Between 0 and 2 outputs. | Between 0 and 2 outputs. |
137 | 136 |
|
|
138 | 137 | * **Y** (optional, heterogeneous) - **T**: | * **Y** (optional, heterogeneous) - **T**: |
139 | 138 | A tensor that concats all the intermediate output values of the | A tensor that concats all the intermediate output values of the |
140 | 139 | hidden. It has shape [seq_length, num_directions, batch_size, | hidden. It has shape [seq_length, num_directions, batch_size, |
141 | 140 | hidden_size]. It is optional if output_sequence is 0. |
|
142 | 141 | * **Y_h** (optional, heterogeneous) - **T**: | * **Y_h** (optional, heterogeneous) - **T**: |
143 | 142 | The last output value of the hidden. It has shape [num_directions, | The last output value of the hidden. It has shape [num_directions, |
144 | 143 | batch_size, hidden_size]. | batch_size, hidden_size]. |
145 | 144 |
|
|
146 | 145 | **Type Constraints** | **Type Constraints** |
147 | 146 |
|
|
148 | 147 | * **T** in ( | * **T** in ( |
149 | 148 | tensor(double), | tensor(double), |
150 | 149 | tensor(float), | tensor(float), |
151 | 150 | tensor(float16) | tensor(float16) |
152 | 151 | ): | ): |
153 | 152 | Constrain input and output types to float tensors. | Constrain input and output types to float tensors. |
154 | 153 | * **T1** in ( | * **T1** in ( |
155 | 154 | tensor(int32) | tensor(int32) |
156 | 155 | ): | ): |
157 | 156 | Constrain seq_lens to integer tensor. | Constrain seq_lens to integer tensor. |
GRU - 3#
Version
name: GRU (GitHub)
domain: main
since_version: 3
function: False
support_level: SupportType.COMMON
shape inference: True
This version of the operator has been available since version 3.
Summary
Computes an one-layer GRU. This operator is usually supported via some custom implementation such as CuDNN.
Notations:
X - input tensor
z - update gate
r - reset gate
h - hidden gate
t - time step (t-1 means previous time step)
W[zrh] - W parameter weight matrix for update, reset, and hidden gates
R[zrh] - R recurrence weight matrix for update, reset, and hidden gates
Wb[zrh] - W bias vectors for update, reset, and hidden gates
Rb[zrh] - R bias vectors for update, reset, and hidden gates
WB[zrh] - W parameter weight matrix for backward update, reset, and hidden gates
RB[zrh] - R recurrence weight matrix for backward update, reset, and hidden gates
WBb[zrh] - W bias vectors for backward update, reset, and hidden gates
RBb[zrh] - R bias vectors for backward update, reset, and hidden gates
H - Hidden state
num_directions - 2 if direction == bidirectional else 1
Activation functions:
Relu(x) - max(0, x)
Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})
Sigmoid(x) - 1/(1 + e^{-x})
(NOTE: Below are optional)
Affine(x) - alpha*x + beta
LeakyRelu(x) - x if x >= 0 else alpha * x
ThresholdedRelu(x) - x if x >= alpha else 0
ScaledTanh(x) - alpha*Tanh(beta*x)
HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)
Elu(x) - x if x >= 0 else alpha*(e^x - 1)
Softsign(x) - x/(1 + |x|)
Softplus(x) - log(1 + e^x)
Equations (Default: f=Sigmoid, g=Tanh):
zt = f(Xt*(Wz^T) + Ht-1*Rz + Wbz + Rbz)
rt = f(Xt*(Wr^T) + Ht-1*Rr + Wbr + Rbr)
ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*Rh + Rbh + Wbh) # default, when linear_before_reset = 0
ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*Rh + Rbh) + Wbh) # when linear_before_reset != 0
Ht = (1 - zt) (.) ht + zt (.) Ht-1
Attributes
activation_alpha: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.For example with LeakyRelu, the default alpha is 0.01.
activation_beta: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM. Default values are the same as of corresponding ONNX operators.
activations: A list of 2 (or 4 if bidirectional) activation functions for update, reset, and hidden gates. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified.
clip: Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified.
direction: Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional. Default value is
'forward'
.hidden_size: Number of neurons in the hidden layer
linear_before_reset: When computing the output of the hidden gate, apply the linear transformation before multiplying by the output of the reset gate. Default value is
0
.output_sequence: The sequence output for the hidden is optional if 0. Default 0. Default value is
0
.
Inputs
Between 3 and 6 inputs.
X (heterogeneous) - T: The input sequences packed (and potentially padded) into one 3-D tensor with the shape of [seq_length, batch_size, input_size].
W (heterogeneous) - T: The weight tensor for the gates. Concatenation of W[zrh] and WB[zrh] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 3*hidden_size, input_size].
R (heterogeneous) - T: The recurrence weight tensor. Concatenation of R[zrh] and RB[zrh] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 3*hidden_size, hidden_size].
B (optional, heterogeneous) - T: The bias tensor for the gates. Concatenation of [Wb[zrh], Rb[zrh]] and [WBb[zrh], RBb[zrh]] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 6*hidden_size]. Optional: If not specified - assumed to be 0
sequence_lens (optional, heterogeneous) - T1: Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length seq_length. It has shape [batch_size].
initial_h (optional, heterogeneous) - T: Optional initial value of the hidden. If not specified - assumed to be 0. It has shape [num_directions, batch_size, hidden_size].
Outputs
Between 0 and 2 outputs.
Y (optional, heterogeneous) - T: A tensor that concats all the intermediate output values of the hidden. It has shape [seq_length, num_directions, batch_size, hidden_size]. It is optional if output_sequence is 0.
Y_h (optional, heterogeneous) - T: The last output value of the hidden. It has shape [num_directions, batch_size, hidden_size].
Type Constraints
T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.
T1 in ( tensor(int32) ): Constrain seq_lens to integer tensor.
Differences
0 | 0 | Computes an one-layer GRU. This operator is usually supported via some custom | Computes an one-layer GRU. This operator is usually supported via some custom |
1 | 1 | implementation such as CuDNN. | implementation such as CuDNN. |
2 | 2 |
|
|
3 | 3 | Notations: | Notations: |
4 | 4 |
|
|
5 | 5 | X - input tensor | X - input tensor |
6 | 6 |
|
|
7 | 7 | z - update gate | z - update gate |
8 | 8 |
|
|
9 | 9 | r - reset gate | r - reset gate |
10 | 10 |
|
|
11 | 11 | h - hidden gate | h - hidden gate |
12 | 12 |
|
|
13 | 13 | t - time step (t-1 means previous time step) | t - time step (t-1 means previous time step) |
14 | 14 |
|
|
15 | 15 | W[zrh] - W parameter weight matrix for update, reset, and hidden gates | W[zrh] - W parameter weight matrix for update, reset, and hidden gates |
16 | 16 |
|
|
17 | 17 | R[zrh] - R recurrence weight matrix for update, reset, and hidden gates | R[zrh] - R recurrence weight matrix for update, reset, and hidden gates |
18 | 18 |
|
|
19 | 19 | Wb[zrh] - W bias vectors for update, reset, and hidden gates | Wb[zrh] - W bias vectors for update, reset, and hidden gates |
20 | 20 |
|
|
21 | 21 | Rb[zrh] - R bias vectors for update, reset, and hidden gates | Rb[zrh] - R bias vectors for update, reset, and hidden gates |
22 | 22 |
|
|
23 | 23 | WB[zrh] - W parameter weight matrix for backward update, reset, and hidden gates | WB[zrh] - W parameter weight matrix for backward update, reset, and hidden gates |
24 | 24 |
|
|
25 | 25 | RB[zrh] - R recurrence weight matrix for backward update, reset, and hidden gates | RB[zrh] - R recurrence weight matrix for backward update, reset, and hidden gates |
26 | 26 |
|
|
27 | 27 | WBb[zrh] - W bias vectors for backward update, reset, and hidden gates | WBb[zrh] - W bias vectors for backward update, reset, and hidden gates |
28 | 28 |
|
|
29 | 29 | RBb[zrh] - R bias vectors for backward update, reset, and hidden gates | RBb[zrh] - R bias vectors for backward update, reset, and hidden gates |
30 | 30 |
|
|
31 | 31 | H - Hidden state | H - Hidden state |
32 | 32 |
|
|
33 | 33 | num_directions - 2 if direction == bidirectional else 1 | num_directions - 2 if direction == bidirectional else 1 |
34 | 34 |
|
|
35 | 35 | Activation functions: | Activation functions: |
36 | 36 |
|
|
37 | 37 | Relu(x) - max(0, x) | Relu(x) - max(0, x) |
38 | 38 |
|
|
39 | 39 | Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x}) | Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x}) |
40 | 40 |
|
|
41 | 41 | Sigmoid(x) - 1/(1 + e^{-x}) | Sigmoid(x) - 1/(1 + e^{-x}) |
42 | 42 |
|
|
43 | 43 | (NOTE: Below are optional) | (NOTE: Below are optional) |
44 | 44 |
|
|
45 | 45 | Affine(x) - alpha*x + beta | Affine(x) - alpha*x + beta |
46 | 46 |
|
|
47 | 47 | LeakyRelu(x) - x if x >= 0 else alpha * x | LeakyRelu(x) - x if x >= 0 else alpha * x |
48 | 48 |
|
|
49 | 49 | ThresholdedRelu(x) - x if x >= alpha else 0 | ThresholdedRelu(x) - x if x >= alpha else 0 |
50 | 50 |
|
|
51 | 51 | ScaledTanh(x) - alpha*Tanh(beta*x) | ScaledTanh(x) - alpha*Tanh(beta*x) |
52 | 52 |
|
|
53 | 53 | HardSigmoid(x) - min(max(alpha*x + beta, 0), 1) | HardSigmoid(x) - min(max(alpha*x + beta, 0), 1) |
54 | 54 |
|
|
55 | 55 | Elu(x) - x if x >= 0 else alpha*(e^x - 1) | Elu(x) - x if x >= 0 else alpha*(e^x - 1) |
56 | 56 |
|
|
57 | 57 | Softsign(x) - x/(1 + |x|) | Softsign(x) - x/(1 + |x|) |
58 | 58 |
|
|
59 | 59 | Softplus(x) - log(1 + e^x) | Softplus(x) - log(1 + e^x) |
60 | 60 |
|
|
61 | 61 | Equations (Default: f=Sigmoid, g=Tanh): | Equations (Default: f=Sigmoid, g=Tanh): |
62 | 62 |
|
|
63 | 63 | - zt = f(Xt*(Wz^T) + Ht-1*Rz + Wbz + Rbz) | - zt = f(Xt*(Wz^T) + Ht-1*Rz + Wbz + Rbz) |
64 | 64 |
|
|
65 | 65 | - rt = f(Xt*(Wr^T) + Ht-1*Rr + Wbr + Rbr) | - rt = f(Xt*(Wr^T) + Ht-1*Rr + Wbr + Rbr) |
66 | 66 |
|
|
67 | 67 | - ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*Rh + Rbh + Wbh) # default, when linear_before_reset = 0 | - ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*Rh + Rbh + Wbh) # default, when linear_before_reset = 0 |
68 | 68 |
|
|
69 | 69 | - ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*Rh + Rbh) + Wbh) # when linear_before_reset != 0 | - ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*Rh + Rbh) + Wbh) # when linear_before_reset != 0 |
70 | 70 |
|
|
71 | 71 | - Ht = (1 - zt) (.) ht + zt (.) Ht-1 | - Ht = (1 - zt) (.) ht + zt (.) Ht-1 |
72 | 72 |
|
|
73 | 73 | **Attributes** | **Attributes** |
74 | 74 |
|
|
75 | 75 | * **activation_alpha**: | * **activation_alpha**: |
76 | 76 | Optional scaling values used by some activation functions. The | Optional scaling values used by some activation functions. The |
77 | 77 | values are consumed in the order of activation functions, for | values are consumed in the order of activation functions, for |
78 | 78 | example (f, g, h) in LSTM. |
|
79 | corresponding ONNX operators.For example with LeakyRelu, the default | ||
80 | alpha is 0.01. | ||
79 | 81 | * **activation_beta**: | * **activation_beta**: |
80 | 82 | Optional scaling values used by some activation functions. The | Optional scaling values used by some activation functions. The |
81 | 83 | values are consumed in the order of activation functions, for | values are consumed in the order of activation functions, for |
82 | 84 | example (f, g, h) in LSTM. |
|
85 | corresponding ONNX operators. | ||
83 | 86 | * **activations**: | * **activations**: |
84 | 87 | A list of 2 (or 4 if bidirectional) activation functions for update, | A list of 2 (or 4 if bidirectional) activation functions for update, |
85 | 88 | reset, and hidden gates. The activation functions must be one of the | reset, and hidden gates. The activation functions must be one of the |
86 | 89 | activation functions specified above. Optional: See the equations | activation functions specified above. Optional: See the equations |
87 | 90 | for default if not specified. | for default if not specified. |
88 | 91 | * **clip**: | * **clip**: |
89 | 92 | Cell clip threshold. Clipping bounds the elements of a tensor in the | Cell clip threshold. Clipping bounds the elements of a tensor in the |
90 | 93 | range of [-threshold, +threshold] and is applied to the input of | range of [-threshold, +threshold] and is applied to the input of |
91 | 94 | activations. No clip if not specified. | activations. No clip if not specified. |
92 | 95 | * **direction**: | * **direction**: |
93 | 96 | Specify if the RNN is forward, reverse, or bidirectional. Must be | Specify if the RNN is forward, reverse, or bidirectional. Must be |
94 | 97 | one of forward (default), reverse, or bidirectional. Default value is 'foward'. |
|
95 | 98 | * **hidden_size**: | * **hidden_size**: |
96 | 99 | Number of neurons in the hidden layer | Number of neurons in the hidden layer |
100 | * **linear_before_reset**: | ||
101 | When computing the output of the hidden gate, apply the linear | ||
102 | transformation before multiplying by the output of the reset gate. Default value is 0. | ||
97 | 103 | * **output_sequence**: | * **output_sequence**: |
98 | 104 | The sequence output for the hidden is optional if 0. Default 0. Default value is 0. | The sequence output for the hidden is optional if 0. Default 0. Default value is 0. |
99 | 105 |
|
|
100 | 106 | **Inputs** | **Inputs** |
101 | 107 |
|
|
102 | 108 | Between 3 and 6 inputs. | Between 3 and 6 inputs. |
103 | 109 |
|
|
104 | 110 | * **X** (heterogeneous) - **T**: | * **X** (heterogeneous) - **T**: |
105 | 111 | The input sequences packed (and potentially padded) into one 3-D | The input sequences packed (and potentially padded) into one 3-D |
106 | 112 | tensor with the shape of [seq_length, batch_size, input_size]. | tensor with the shape of [seq_length, batch_size, input_size]. |
107 | 113 | * **W** (heterogeneous) - **T**: | * **W** (heterogeneous) - **T**: |
108 | 114 | The weight tensor for the gates. Concatenation of W[zrh] and | The weight tensor for the gates. Concatenation of W[zrh] and |
109 | 115 | WB[zrh] (if bidirectional) along dimension 0. This tensor has | WB[zrh] (if bidirectional) along dimension 0. This tensor has |
110 | 116 | shape [num_directions, 3*hidden_size, input_size]. | shape [num_directions, 3*hidden_size, input_size]. |
111 | 117 | * **R** (heterogeneous) - **T**: | * **R** (heterogeneous) - **T**: |
112 | 118 | The recurrence weight tensor. Concatenation of R[zrh] and | The recurrence weight tensor. Concatenation of R[zrh] and |
113 | 119 | RB[zrh] (if bidirectional) along dimension 0. This tensor has | RB[zrh] (if bidirectional) along dimension 0. This tensor has |
114 | 120 | shape [num_directions, 3*hidden_size, hidden_size]. | shape [num_directions, 3*hidden_size, hidden_size]. |
115 | 121 | * **B** (optional, heterogeneous) - **T**: | * **B** (optional, heterogeneous) - **T**: |
116 | 122 | The bias tensor for the gates. Concatenation of [Wb[zrh], Rb[zrh]] | The bias tensor for the gates. Concatenation of [Wb[zrh], Rb[zrh]] |
117 | 123 | and [WBb[zrh], RBb[zrh]] (if bidirectional) along dimension 0. | and [WBb[zrh], RBb[zrh]] (if bidirectional) along dimension 0. |
118 | 124 | This tensor has shape [num_directions, 6*hidden_size]. Optional: | This tensor has shape [num_directions, 6*hidden_size]. Optional: |
119 | 125 | If not specified - assumed to be 0 | If not specified - assumed to be 0 |
120 | 126 | * **sequence_lens** (optional, heterogeneous) - **T1**: | * **sequence_lens** (optional, heterogeneous) - **T1**: |
121 | 127 | Optional tensor specifying lengths of the sequences in a batch. If | Optional tensor specifying lengths of the sequences in a batch. If |
122 | 128 | not specified - assumed all sequences in the batch to have length | not specified - assumed all sequences in the batch to have length |
123 | 129 | seq_length. It has shape [batch_size]. | seq_length. It has shape [batch_size]. |
124 | 130 | * **initial_h** (optional, heterogeneous) - **T**: | * **initial_h** (optional, heterogeneous) - **T**: |
125 | 131 | Optional initial value of the hidden. If not specified - assumed to | Optional initial value of the hidden. If not specified - assumed to |
126 | 132 | be 0. It has shape [num_directions, batch_size, hidden_size]. | be 0. It has shape [num_directions, batch_size, hidden_size]. |
127 | 133 |
|
|
128 | 134 | **Outputs** | **Outputs** |
129 | 135 |
|
|
136 | Between 0 and 2 outputs. | ||
137 |
| ||
130 | 138 | * **Y** (optional, heterogeneous) - **T**: | * **Y** (optional, heterogeneous) - **T**: |
131 | 139 | A tensor that concats all the intermediate output values of the | A tensor that concats all the intermediate output values of the |
132 | 140 | hidden. It has shape [seq_length, num_directions, batch_size, | hidden. It has shape [seq_length, num_directions, batch_size, |
133 | 141 | hidden_size]. It is optional if output_sequence is 0. | hidden_size]. It is optional if output_sequence is 0. |
134 | 142 | * **Y_h** (heterogeneous) - **T**: |
|
135 | 143 | The last output value of the hidden. It has shape [num_directions, | The last output value of the hidden. It has shape [num_directions, |
136 | 144 | batch_size, hidden_size]. | batch_size, hidden_size]. |
137 | 145 |
|
|
138 | 146 | **Type Constraints** | **Type Constraints** |
139 | 147 |
|
|
140 | 148 | * **T** in ( | * **T** in ( |
141 | 149 | tensor(double), | tensor(double), |
142 | 150 | tensor(float), | tensor(float), |
143 | 151 | tensor(float16) | tensor(float16) |
144 | 152 | ): | ): |
145 | 153 | Constrain input and output types to float tensors. | Constrain input and output types to float tensors. |
146 | 154 | * **T1** in ( | * **T1** in ( |
147 | 155 | tensor(int32) | tensor(int32) |
148 | 156 | ): | ): |
149 | 157 | Constrain seq_lens to integer tensor. | Constrain seq_lens to integer tensor. |
GRU - 1#
Version
name: GRU (GitHub)
domain: main
since_version: 1
function: False
support_level: SupportType.COMMON
shape inference: False
This version of the operator has been available since version 1.
Summary
Computes an one-layer GRU. This operator is usually supported via some custom implementation such as CuDNN.
Notations:
X - input tensor
z - update gate
r - reset gate
h - hidden gate
t - time step (t-1 means previous time step)
W[zrh] - W parameter weight matrix for update, reset, and hidden gates
R[zrh] - R recurrence weight matrix for update, reset, and hidden gates
Wb[zrh] - W bias vectors for update, reset, and hidden gates
Rb[zrh] - R bias vectors for update, reset, and hidden gates
WB[zrh] - W parameter weight matrix for backward update, reset, and hidden gates
RB[zrh] - R recurrence weight matrix for backward update, reset, and hidden gates
WBb[zrh] - W bias vectors for backward update, reset, and hidden gates
RBb[zrh] - R bias vectors for backward update, reset, and hidden gates
H - Hidden state
num_directions - 2 if direction == bidirectional else 1
Activation functions:
Relu(x) - max(0, x)
Tanh(x) - (1 - e^{-2x})/(1 + e^{-2x})
Sigmoid(x) - 1/(1 + e^{-x})
(NOTE: Below are optional)
Affine(x) - alpha*x + beta
LeakyRelu(x) - x if x >= 0 else alpha * x
ThresholdedRelu(x) - x if x >= alpha else 0
ScaledTanh(x) - alpha*Tanh(beta*x)
HardSigmoid(x) - min(max(alpha*x + beta, 0), 1)
Elu(x) - x if x >= 0 else alpha*(e^x - 1)
Softsign(x) - x/(1 + |x|)
Softplus(x) - log(1 + e^x)
Equations (Default: f=Sigmoid, g=Tanh):
zt = f(Xt*(Wz^T) + Ht-1*Rz + Wbz + Rbz)
rt = f(Xt*(Wr^T) + Ht-1*Rr + Wbr + Rbr)
ht = g(Xt*(Wh^T) + (rt (.) Ht-1)*Rh + Rbh + Wbh) # default, when linear_before_reset = 0
ht = g(Xt*(Wh^T) + (rt (.) (Ht-1*Rh + Rbh) + Wbh) # when linear_before_reset != 0
Ht = (1 - zt) (.) ht + zt (.) Ht-1
Attributes
activation_alpha: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM.
activation_beta: Optional scaling values used by some activation functions. The values are consumed in the order of activation functions, for example (f, g, h) in LSTM.
activations: A list of 2 (or 4 if bidirectional) activation functions for update, reset, and hidden gates. The activation functions must be one of the activation functions specified above. Optional: See the equations for default if not specified.
clip: Cell clip threshold. Clipping bounds the elements of a tensor in the range of [-threshold, +threshold] and is applied to the input of activations. No clip if not specified.
direction: Specify if the RNN is forward, reverse, or bidirectional. Must be one of forward (default), reverse, or bidirectional. Default value is
'foward'
.hidden_size: Number of neurons in the hidden layer
output_sequence: The sequence output for the hidden is optional if 0. Default 0. Default value is
0
.
Inputs
Between 3 and 6 inputs.
X (heterogeneous) - T: The input sequences packed (and potentially padded) into one 3-D tensor with the shape of [seq_length, batch_size, input_size].
W (heterogeneous) - T: The weight tensor for the gates. Concatenation of W[zrh] and WB[zrh] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 3*hidden_size, input_size].
R (heterogeneous) - T: The recurrence weight tensor. Concatenation of R[zrh] and RB[zrh] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 3*hidden_size, hidden_size].
B (optional, heterogeneous) - T: The bias tensor for the gates. Concatenation of [Wb[zrh], Rb[zrh]] and [WBb[zrh], RBb[zrh]] (if bidirectional) along dimension 0. This tensor has shape [num_directions, 6*hidden_size]. Optional: If not specified - assumed to be 0
sequence_lens (optional, heterogeneous) - T1: Optional tensor specifying lengths of the sequences in a batch. If not specified - assumed all sequences in the batch to have length seq_length. It has shape [batch_size].
initial_h (optional, heterogeneous) - T: Optional initial value of the hidden. If not specified - assumed to be 0. It has shape [num_directions, batch_size, hidden_size].
Outputs
Y (optional, heterogeneous) - T: A tensor that concats all the intermediate output values of the hidden. It has shape [seq_length, num_directions, batch_size, hidden_size]. It is optional if output_sequence is 0.
Y_h (heterogeneous) - T: The last output value of the hidden. It has shape [num_directions, batch_size, hidden_size].
Type Constraints
T in ( tensor(double), tensor(float), tensor(float16) ): Constrain input and output types to float tensors.
T1 in ( tensor(int32) ): Constrain seq_lens to integer tensor.