When keras builds the deep learning model, there are two API s, LSTMcell and LSTM, in the loop layer. How to distinguish?
1 first look at the source code
Source code of LSTMcell:
class LSTMCell(Layer): def __init__(self): *****pass***** def build(self, input_shape): *****pass***** self.built = True def call(self, inputs, states, training=None): *****pass***** h_tm1 = states[0] # previous memory state c_tm1 = states[1] # previous carry state if self.implementation == 1: x_i = K.dot(inputs_i, self.kernel_i) x_i = K.bias_add(x_i, self.bias_i) i = self.recurrent_activation(x_i + K.dot(h_tm1_i,self.recurrent_kernel_i)) z0 = z[:, :self.units] i = self.recurrent_activation(z0) h = o * self.activation(c) return h, [h, c] def get_config(self): *****pass*****
In this code, I extract part of it, which is different from the source code comparison. Let's first look at the class inheritance. The LSTMcell class inherits from the base class Layer of keras. In its call method, you can see that there is a very detailed single step calculation process.
Next, look at the source code of LSTM:
class LSTM(RNN): @interfaces.legacy_recurrent_support def __init__(self): *****pass***** cell = LSTMCell(units, activation=activation, recurrent_activation=recurrent_activation, use_bias=use_bias, kernel_initializer=kernel_initializer, recurrent_initializer=recurrent_initializer, unit_forget_bias=unit_forget_bias, bias_initializer=bias_initializer, kernel_regularizer=kernel_regularizer, recurrent_regularizer=recurrent_regularizer, bias_regularizer=bias_regularizer, kernel_constraint=kernel_constraint, recurrent_constraint=recurrent_constraint, bias_constraint=bias_constraint, dropout=dropout, recurrent_dropout=recurrent_dropout, implementation=implementation) super(LSTM, self).__init__(cell, return_sequences=return_sequences, return_state=return_state, go_backwards=go_backwards, stateful=stateful, unroll=unroll, **kwargs) self.activity_regularizer = regularizers.get(activity_regularizer) def call(self, inputs, mask=None, training=None, initial_state=None): return super(LSTM, self).call(inputs, mask=mask, training=training, initial_state=initial_state) *****pass*****
In the LSTM method, it can be clearly seen that it inherits from the RNN class, that is, a loop layer. In its init method, it calls LSTMcell, which uses LSTMcell as the calculation unit of its loop process.
The loop layer contains cell objects. The cell contains the core code for calculating each step, while the loop layer commands the cell and performs the actual loop calculation.
2 implementation method
Generally, people use layers in their code. Or they use the layer LSTMCell that RNN contains.
#LSTM model = Sequential() model.add(LSTM(10)) ... #RNN cells = [LSTMCell(32), LSTMCell(64)] x = keras.Input((None, 5)) layer = RNN(cells) y = layer(x)
3 difference
LSTMcell is the implementation unit of LSTM layer, which is fixed as its computing unit. LSTMcell is a single step computing unit. -LSTM is a regular layer -LSTMCell is the object used by the LSTM layer (which happens to be a layer), and it contains one-step calculation logic.