<jupyter_start><jupyter_text>Train the RNN model:<jupyter_code># TextRNN Parameter
n_step = 2 # number of cells (=number of Step)
n_hidden = 5 # number of hidden units in on cell

model = Sequential()
model.add(LSTM(n_hidden))# output shape [batch_size, n_step, n_hidden])
model.add(Dense(n_class,activation='softmax'))

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
model.fit(input_batch, target_batch, epochs=100)
input_ = np.array([sen.split()[:2] for sen in sentences])

predict = model.predict(input_batch)
predict = np.argmax(predict, axis=1)
print([sen.split()[:2] for sen in sentences], '->', [number_dict[n] for n in predict])<jupyter_output>[['i', 'like'], ['i', 'love'], ['i', 'hate']] -> ['dog', 'dog', 'dog']<jupyter_text>- 输入维度(3, 2, 7) ` [batch_size, sequence_lens, embedding_size]`没有使用嵌入层，所以`embedding_size` = `vocab_size`  - RNN`h_t`向量维度为`n_hidden = 5`，所以输出维度为(3, 5) ` [batch_size, n_hidden]`      (根据`h_t = tanh(W_x X_t + W_h h_{t-1} + b)`得参数为(7+5)*5+5= 65)  - 将RNN输出Reshape成维度(3, -1) ` [batch_size, -1]`  - Dence层输出维度为(3, 7) ` [batch_size, vocab_size]`  **RNN/GRU/LSTM 参数量计算**:  SimpleRNN:$m\times n + n\times n + n = (m+n)\times n + n = (m+n+1)*n$  GRU: 多了两个门，所以是$3\teims$<jupyter_code>model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm (LSTM)                  (None, 5)                 260       
_________________________________________________________________
dense_3 (Dense)              (None, 7)                 42        
=================================================================
Total params: 302
Trainable params: 302
Non-trainable params: 0
_________________________________________________________________
{'fraction_non_alphanumeric': 0.21351931330472104, 'fraction_numerical': 0.023068669527896997, 'mean_word_length': 4.977564102564102, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '270892', 'license': 'MIT', 'n_tokens_mistral': 693, 'n_tokens_neox': 664, 'n_words': 131, 'path': '3-1.TextRNN/TextRNN_Keras.ipynb', 'repo_name': 'nocater/nlp-tutorial', 'size': '1864'}