<jupyter_start><jupyter_text>文本分类，把一句话分类为 消极 和 积极。<jupyter_code>import tensorflow as tf
from tensorflow import keras
import tensorflow_datasets as tfds

import matplotlib.pyplot as plt
dataset, info = tfds.load('imdb_reviews/subwords8k',
                          with_info=True,
                          as_supervised=True)
train_dataset, test_dataset = dataset['train'], dataset['test']
encoder = info.features['text'].encoder
# 词汇表大小
print(encoder.vocab_size)
# 词汇表的词
print(encoder.decode([1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
                      11, 12, 13, 14, 15, 16, 17, 18, 19, 20]))
sample_string = 'I am a sentence.'
encoded_string = encoder.encode(sample_string)
encoded_string
original_string = encoder.decode(encoded_string)
original_string
for index in encoded_string:
    print('{} --> {}'.format(index, encoder.decode([index])))<jupyter_output>12 --> I 
258 --> am 
4 --> a 
7745 --> sentence
7975 --> .<jupyter_text>Prepare the data<jupyter_code>train_batches = train_dataset.shuffle(10000).padded_batch(64, padded_shapes=([None],[]))
test_batches = test_dataset.shuffle(10000).padded_batch(64, padded_shapes=([None],[]))
model = keras.Sequential([
    keras.layers.Embedding(encoder.vocab_size, 64),
    keras.layers.Bidirectional(keras.layers.LSTM(64)),
    keras.layers.Dense(64, activation='relu'),
    keras.layers.Dense(1)
])

model.summary()
model.compile(loss=keras.losses.BinaryCrossentropy(from_logits=True),
              optimizer=keras.optimizers.Adam(1e-4),
              metrics=['accuracy'])
history = model.fit(train_batches, epochs=10,
                    validation_data=test_batches,
                    validation_steps=30)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'], '')
plt.xlabel("Epochs")
plt.ylabel('loss')
plt.legend(['loss', 'val_loss'])
plt.show()

plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'], '')
plt.xlabel("Epochs")
plt.ylabel('accuracy')
plt.legend(['accuracy', 'val_accuracy'])
plt.show()
model.save('colab_4-13-10-11.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1570528356761997, 'fraction_numerical': 0.03926320891904993, 'mean_word_length': 4.4603174603174605, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '282940', 'license': 'MIT', 'n_tokens_mistral': 819, 'n_tokens_neox': 751, 'n_words': 119, 'path': 'Tensorflow_official/RNN/text_classification/train.ipynb', 'repo_name': 'starkidstory/OmegaTensor', 'size': '2063'}