RNN that accepts a state saver for time-truncated RNN calculation. Args: cell: An instance of `RNNCell`. inputs: A length T list of inputs, each a `Tensor` of shape `[batch_size, input_size]`. state_saver: A state saver object with methods `state` and `save_state`. state_nam
(cell,
inputs,
state_saver,
state_name,
sequence_length=None,
scope=None)
| 1269 | |
| 1270 | |
| 1271 | def static_state_saving_rnn(cell, |
| 1272 | inputs, |
| 1273 | state_saver, |
| 1274 | state_name, |
| 1275 | sequence_length=None, |
| 1276 | scope=None): |
| 1277 | """RNN that accepts a state saver for time-truncated RNN calculation. |
| 1278 | |
| 1279 | Args: |
| 1280 | cell: An instance of `RNNCell`. |
| 1281 | inputs: A length T list of inputs, each a `Tensor` of shape |
| 1282 | `[batch_size, input_size]`. |
| 1283 | state_saver: A state saver object with methods `state` and `save_state`. |
| 1284 | state_name: Python string or tuple of strings. The name to use with the |
| 1285 | state_saver. If the cell returns tuples of states (i.e., |
| 1286 | `cell.state_size` is a tuple) then `state_name` should be a tuple of |
| 1287 | strings having the same length as `cell.state_size`. Otherwise it should |
| 1288 | be a single string. |
| 1289 | sequence_length: (optional) An int32/int64 vector size [batch_size]. |
| 1290 | See the documentation for rnn() for more details about sequence_length. |
| 1291 | scope: VariableScope for the created subgraph; defaults to "rnn". |
| 1292 | |
| 1293 | Returns: |
| 1294 | A pair (outputs, state) where: |
| 1295 | outputs is a length T list of outputs (one for each input) |
| 1296 | states is the final state |
| 1297 | |
| 1298 | Raises: |
| 1299 | TypeError: If `cell` is not an instance of RNNCell. |
| 1300 | ValueError: If `inputs` is `None` or an empty list, or if the arity and |
| 1301 | type of `state_name` does not match that of `cell.state_size`. |
| 1302 | """ |
| 1303 | state_size = cell.state_size |
| 1304 | state_is_tuple = nest.is_sequence(state_size) |
| 1305 | state_name_tuple = nest.is_sequence(state_name) |
| 1306 | |
| 1307 | if state_is_tuple != state_name_tuple: |
| 1308 | raise ValueError("state_name should be the same type as cell.state_size. " |
| 1309 | "state_name: %s, cell.state_size: %s" % (str(state_name), |
| 1310 | str(state_size))) |
| 1311 | |
| 1312 | if state_is_tuple: |
| 1313 | state_name_flat = nest.flatten(state_name) |
| 1314 | state_size_flat = nest.flatten(state_size) |
| 1315 | |
| 1316 | if len(state_name_flat) != len(state_size_flat): |
| 1317 | raise ValueError("#elems(state_name) != #elems(state_size): %d vs. %d" % |
| 1318 | (len(state_name_flat), len(state_size_flat))) |
| 1319 | |
| 1320 | initial_state = nest.pack_sequence_as( |
| 1321 | structure=state_size, |
| 1322 | flat_sequence=[state_saver.state(s) for s in state_name_flat]) |
| 1323 | else: |
| 1324 | initial_state = state_saver.state(state_name) |
| 1325 | |
| 1326 | (outputs, state) = static_rnn( |
| 1327 | cell, |
| 1328 | inputs, |
nothing calls this directly
no test coverage detected