GRU with standard kernel implementation. This implementation can be run on all types of hardware. This implementation lifts out all the layer weights and make them function parameters. It has same number of tensor input params as the CuDNN counterpart. The RNN step logic has been simplifie
(inputs, init_h, kernel, recurrent_kernel, bias, activation,
recurrent_activation, mask, time_major, go_backwards)
| 408 | |
| 409 | |
| 410 | def standard_gru(inputs, init_h, kernel, recurrent_kernel, bias, activation, |
| 411 | recurrent_activation, mask, time_major, go_backwards): |
| 412 | """GRU with standard kernel implementation. |
| 413 | |
| 414 | This implementation can be run on all types of hardware. |
| 415 | |
| 416 | This implementation lifts out all the layer weights and make them function |
| 417 | parameters. It has same number of tensor input params as the CuDNN |
| 418 | counterpart. The RNN step logic has been simplified, eg dropout and mask is |
| 419 | removed since CuDNN implementation does not support that. |
| 420 | |
| 421 | Arguments: |
| 422 | inputs: Input tensor of GRU layer. |
| 423 | init_h: Initial state tensor for the cell output. |
| 424 | kernel: Weights for cell kernel. |
| 425 | recurrent_kernel: Weights for cell recurrent kernel. |
| 426 | bias: Weights for cell kernel bias and recurrent bias. The bias contains the |
| 427 | combined input_bias and recurrent_bias. |
| 428 | activation: Activation function to use for output. |
| 429 | recurrent_activation: Activation function to use for hidden recurrent state. |
| 430 | mask: Binary tensor of shape `(samples, timesteps)` indicating whether |
| 431 | a given timestep should be masked. |
| 432 | time_major: Boolean, whether the inputs are in the format of |
| 433 | [time, batch, feature] or [batch, time, feature]. |
| 434 | go_backwards: Boolean (default False). If True, process the input sequence |
| 435 | backwards and return the reversed sequence. |
| 436 | |
| 437 | Returns: |
| 438 | last_output: output tensor for the last timestep, which has shape |
| 439 | [batch, units]. |
| 440 | outputs: output tensor for all timesteps, which has shape |
| 441 | [batch, time, units]. |
| 442 | state_0: the cell output, which has same shape as init_h. |
| 443 | runtime: constant string tensor which indicate real runtime hardware. This |
| 444 | value is for testing purpose and should be used by user. |
| 445 | """ |
| 446 | input_shape = K.int_shape(inputs) |
| 447 | timesteps = input_shape[0] if time_major else input_shape[1] |
| 448 | |
| 449 | input_bias, recurrent_bias = array_ops.unstack(bias) |
| 450 | |
| 451 | def step(cell_inputs, cell_states): |
| 452 | """Step function that will be used by Keras RNN backend.""" |
| 453 | h_tm1 = cell_states[0] |
| 454 | |
| 455 | # inputs projected by all gate matrices at once |
| 456 | matrix_x = K.dot(cell_inputs, kernel) |
| 457 | matrix_x = K.bias_add(matrix_x, input_bias) |
| 458 | |
| 459 | x_z, x_r, x_h = array_ops.split(matrix_x, 3, axis=1) |
| 460 | |
| 461 | # hidden state projected by all gate matrices at once |
| 462 | matrix_inner = K.dot(h_tm1, recurrent_kernel) |
| 463 | matrix_inner = K.bias_add(matrix_inner, recurrent_bias) |
| 464 | |
| 465 | recurrent_z, recurrent_r, recurrent_h = array_ops.split(matrix_inner, 3, |
| 466 | axis=1) |
| 467 | z = recurrent_activation(x_z + recurrent_z) |
no test coverage detected