Creates a bidirectional recurrent neural network. Similar to the unidirectional case (rnn) but takes input and builds independent forward and backward RNNs with the final forward and backward outputs depth-concatenated, such that the output will have the format [time][batch][cell_fw.output_
(cell_fw,
cell_bw,
inputs,
initial_state_fw=None,
initial_state_bw=None,
dtype=None,
sequence_length=None,
scope=None)
| 215 | |
| 216 | @deprecated(None, 'Please consider `tf.nn.bidirectional_dynamic_rnn`.') |
| 217 | def bidirectional_rnn(cell_fw, |
| 218 | cell_bw, |
| 219 | inputs, |
| 220 | initial_state_fw=None, |
| 221 | initial_state_bw=None, |
| 222 | dtype=None, |
| 223 | sequence_length=None, |
| 224 | scope=None): |
| 225 | """Creates a bidirectional recurrent neural network. |
| 226 | |
| 227 | Similar to the unidirectional case (rnn) but takes input and builds |
| 228 | independent forward and backward RNNs with the final forward and backward |
| 229 | outputs depth-concatenated, such that the output will have the format |
| 230 | [time][batch][cell_fw.output_size + cell_bw.output_size]. The input_size of |
| 231 | forward and backward cell must match. The initial state for both directions |
| 232 | is zero by default (but can be set optionally) and no intermediate states |
| 233 | are ever returned -- the network is fully unrolled for the given (passed in) |
| 234 | length(s) of the sequence(s) or completely unrolled if length(s) is not |
| 235 | given. |
| 236 | Args: |
| 237 | cell_fw: An instance of RNNCell, to be used for forward direction. |
| 238 | cell_bw: An instance of RNNCell, to be used for backward direction. |
| 239 | inputs: A length T list of inputs, each a tensor of shape |
| 240 | [batch_size, cell.input_size]. |
| 241 | initial_state_fw: (optional) An initial state for the forward RNN. |
| 242 | This must be a tensor of appropriate type and shape |
| 243 | [batch_size x cell.state_size]. |
| 244 | initial_state_bw: (optional) Same as for initial_state_fw. |
| 245 | dtype: (optional) The data type for the initial state. Required if |
| 246 | either of the initial states are not provided. |
| 247 | sequence_length: (optional) An int64 vector (tensor) of size |
| 248 | [batch_size], |
| 249 | containing the actual lengths for each of the sequences. |
| 250 | scope: VariableScope for the created subgraph; defaults to "BiRNN" |
| 251 | |
| 252 | Returns: |
| 253 | A pair (outputs, state) where: |
| 254 | outputs is a length T list of outputs (one for each input), which |
| 255 | are depth-concatenated forward and backward outputs |
| 256 | state is the concatenated final state of the forward and backward RNN |
| 257 | |
| 258 | Raises: |
| 259 | TypeError: If "cell_fw" or "cell_bw" is not an instance of RNNCell. |
| 260 | ValueError: If inputs is None or an empty list. |
| 261 | """ |
| 262 | |
| 263 | if not isinstance(cell_fw, contrib_rnn.RNNCell): |
| 264 | raise TypeError('cell_fw must be an instance of RNNCell') |
| 265 | if not isinstance(cell_bw, contrib_rnn.RNNCell): |
| 266 | raise TypeError('cell_bw must be an instance of RNNCell') |
| 267 | if not isinstance(inputs, list): |
| 268 | raise TypeError('inputs must be a list') |
| 269 | if not inputs: |
| 270 | raise ValueError('inputs must not be empty') |
| 271 | |
| 272 | name = scope or 'BiRNN' |
| 273 | # Forward direction |
| 274 | with vs.variable_scope(name + '_FW'): |