<jupyter_start><jupyter_text>1. 導入神經網路函式庫<jupyter_code>from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Embedding
from tensorflow.keras.layers import LSTM
from tensorflow.keras.datasets import imdb<jupyter_output><empty_output><jupyter_text>2. 獲取數據<jupyter_code>(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=10000)<jupyter_output><empty_output><jupyter_text>3. x_train的長度<jupyter_code>len(x_train)<jupyter_output><empty_output><jupyter_text>4. x_train前十項個別長度<jupyter_code>for i in range(10):
    print(len(x_train[i]))<jupyter_output>218
189
141
550
147
43
123
562
233
130<jupyter_text>5. 資料前處理<jupyter_code>x_train = sequence.pad_sequences(x_train, maxlen=100)
x_test = sequence.pad_sequences(x_test, maxlen=100)<jupyter_output><empty_output><jupyter_text>6. 架設神經網路<jupyter_code>model = Sequential()
model.add(Embedding(10000, 128))                            ##降至128維
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.1))
model.add(Dense(1, activation='sigmoid'))                   ## 輸出值介於0~1值
model.compile(loss='binary_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding (Embedding)        (None, None, 128)         1280000   
_________________________________________________________________
lstm (LSTM)                  (None, 128)               131584    
_________________________________________________________________
dense (Dense)                (None, 1)                 129       
=================================================================
Total params: 1,411,713
Trainable params: 1,411,713
Non-trainable params: 0
_________________________________________________________________<jupyter_text>訓練<jupyter_code>model.fit(x_train, y_train, batch_size=64, epochs=10,
         validation_data=(x_test, y_test))<jupyter_output>Train on 25000 samples, validate on 25000 samples
Epoch 1/10
25000/25000 [==============================] - 224s 9ms/sample - loss: 0.4512 - accuracy: 0.7820 - val_loss: 0.3628 - val_accuracy: 0.8404
Epoch 2/10
25000/25000 [==============================] - 220s 9ms/sample - loss: 0.3051 - accuracy: 0.8742 - val_loss: 0.3637 - val_accuracy: 0.8378
Epoch 3/10
25000/25000 [==============================] - 209s 8ms/sample - loss: 0.2393 - accuracy: 0.9055 - val_loss: 0.3697 - val_accuracy: 0.8404
Epoch 4/10
25000/25000 [==============================] - 218s 9ms/sample - loss: 0.1890 - accuracy: 0.9287 - val_loss: 0.4108 - val_accuracy: 0.8350
Epoch 5/10
25000/25000 [==============================] - 212s 8ms/sample - loss: 0.1551 - accuracy: 0.9407 - val_loss: 0.4635 - val_accuracy: 0.8139
Epoch 6/10
24960/25000 [============================>.] - ETA: 0s - loss: 0.1213 - accuracy: 0.9566
{'fraction_non_alphanumeric': 0.20124753775443205, 'fraction_numerical': 0.10833880499015101, 'mean_word_length': 4.75992438563327, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 27, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '135618', 'license': 'MIT', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1029, 'n_words': 181, 'path': 'Artificial Neural Network/RNN.ipynb', 'repo_name': 'kkzzo1o1/kkzzo1o1', 'size': '3046'}