LSTM with standard kernel implementation. This implementation can be run on all types for 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 simplif
(inputs, init_h, init_c, kernel, recurrent_kernel, bias,
activation, recurrent_activation, mask, time_major,
go_backwards)
| 1032 | |
| 1033 | |
| 1034 | def standard_lstm(inputs, init_h, init_c, kernel, recurrent_kernel, bias, |
| 1035 | activation, recurrent_activation, mask, time_major, |
| 1036 | go_backwards): |
| 1037 | """LSTM with standard kernel implementation. |
| 1038 | |
| 1039 | This implementation can be run on all types for hardware. |
| 1040 | |
| 1041 | This implementation lifts out all the layer weights and make them function |
| 1042 | parameters. It has same number of tensor input params as the CuDNN |
| 1043 | counterpart. The RNN step logic has been simplified, eg dropout and mask is |
| 1044 | removed since CuDNN implementation does not support that. |
| 1045 | |
| 1046 | Note that the first half of the bias tensor should be ignored by this impl. |
| 1047 | The CuDNN impl need an extra set of input gate bias. In order to make the both |
| 1048 | function take same shape of parameter, that extra set of bias is also feed |
| 1049 | here. |
| 1050 | |
| 1051 | Args: |
| 1052 | inputs: input tensor of LSTM layer. |
| 1053 | init_h: initial state tensor for the cell output. |
| 1054 | init_c: initial state tensor for the cell hidden state. |
| 1055 | kernel: weights for cell kernel. |
| 1056 | recurrent_kernel: weights for cell recurrent kernel. |
| 1057 | bias: weights for cell kernel bias and recurrent bias. Only recurrent bias |
| 1058 | is used in this case. |
| 1059 | activation: Activation function to use for output. |
| 1060 | recurrent_activation: Activation function to use for hidden recurrent state. |
| 1061 | mask: Boolean tensor for mask out the steps within sequence. |
| 1062 | time_major: boolean, whether the inputs are in the format of |
| 1063 | [time, batch, feature] or [batch, time, feature]. |
| 1064 | go_backwards: Boolean (default False). If True, process the input sequence |
| 1065 | backwards and return the reversed sequence. |
| 1066 | |
| 1067 | Returns: |
| 1068 | last_output: output tensor for the last timestep, which has shape |
| 1069 | [batch, units]. |
| 1070 | outputs: output tensor for all timesteps, which has shape |
| 1071 | [batch, time, units]. |
| 1072 | state_0: the cell output, which has same shape as init_h. |
| 1073 | state_1: the cell hidden state, which has same shape as init_c. |
| 1074 | runtime: constant string tensor which indicate real runtime hardware. This |
| 1075 | value is for testing purpose and should be used by user. |
| 1076 | """ |
| 1077 | input_shape = K.int_shape(inputs) |
| 1078 | timesteps = input_shape[0] if time_major else input_shape[1] |
| 1079 | |
| 1080 | def step(cell_inputs, cell_states): |
| 1081 | """Step function that will be used by Keras RNN backend.""" |
| 1082 | h_tm1 = cell_states[0] # previous memory state |
| 1083 | c_tm1 = cell_states[1] # previous carry state |
| 1084 | |
| 1085 | z = K.dot(cell_inputs, kernel) |
| 1086 | z += K.dot(h_tm1, recurrent_kernel) |
| 1087 | z = K.bias_add(z, bias) |
| 1088 | |
| 1089 | z0, z1, z2, z3 = array_ops.split(z, 4, axis=1) |
| 1090 | |
| 1091 | i = recurrent_activation(z0) |
no test coverage detected