<jupyter_start><jupyter_text>RNN 变体<jupyter_code>from tensorflow import keras
from tensorflow.keras import layers<jupyter_output><empty_output><jupyter_text>导入数据<jupyter_code>num_words = 10000
maxlen = 100
(x_train, y_train), (x_test, y_test) = keras.datasets.imdb.load_data(num_words=num_words)
print(x_train.shape, ' ', y_train.shape)
print(x_test.shape, ' ', y_test.shape)
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen, padding='post')
x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen, padding='post')
print(x_train.shape, ' ', y_train.shape)
print(x_test.shape, ' ', y_test.shape)<jupyter_output>(25000, 100)   (25000,)
(25000, 100)   (25000,)<jupyter_text>LSTM<jupyter_code>import tensorflow as tf

def lstm_model():
    model = keras.Sequential([
        layers.Embedding(input_dim=num_words, output_dim=32, input_length=maxlen),
#         layers.LSTM(32, return_sequences=True),
        tf.compat.v1.keras.layers.CuDNNLSTM(32, return_sequences=True),
#         layers.LSTM(1, activation='sigmoid', return_sequences=False)
        tf.compat.v1.keras.layers.CuDNNLSTM(1, return_sequences=False)
    ])
    model.compile(optimizer=keras.optimizers.Adam(),
                 loss=keras.losses.BinaryCrossentropy(),
                 metrics=['accuracy'])
    return model

model = lstm_model()
model.summary()
%%time
history = model.fit(x_train, y_train, batch_size=64, epochs=10, validation_split=0.1)
import matplotlib.pyplot as plt

%matplotlib inline
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['training', 'valivation'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>GRU<jupyter_code>def lstm_model():
    model = keras.Sequential([
        layers.Embedding(input_dim=num_words, output_dim=32, input_length=maxlen),
#         layers.GRU(32, return_sequences=True),
#         layers.GRU(1, activation='sigmoid', return_sequences=False)
        tf.compat.v1.keras.layers.CuDNNGRU(32, return_sequences=True),
        tf.compat.v1.keras.layers.CuDNNGRU(1, return_sequences=False)
    ])
    model.compile(optimizer=keras.optimizers.Adam(),
                 loss=keras.losses.BinaryCrossentropy(),
                 metrics=['accuracy'])
    return model

model = lstm_model()
model.summary()
%%time
history = model.fit(x_train, y_train, batch_size=64, epochs=10, validation_split=0.1)
import matplotlib.pyplot as plt

%matplotlib inline
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['training', 'valivation'], loc='upper left')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14182509505703422, 'fraction_numerical': 0.025095057034220533, 'mean_word_length': 5.294258373205742, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '293167', 'license': 'MIT', 'n_tokens_mistral': 1017, 'n_tokens_neox': 948, 'n_words': 128, 'path': '107_RNN_variance.ipynb', 'repo_name': 'smile901122/tensorflow2.0_learning', 'size': '2630'}