Loading...
墨滴

希仔

2021/04/10  阅读:36  主题:默认主题

Multiple Layer LSTM

Multiple Layer LSTM

from __future__ import absolute_import, division, print_function, unicode_literals


import tensorflow_datasets as tfds
import tensorflow as tf
print(tf.__version__)
2.4.1
import tensorflow_datasets as tfds
import tensorflow as tf
print(tf.__version__)
2.4.1
# Get the data
# dataset, info = tfds.load('imdb_reviews/subwords8k', with_info=True, as_supervised=True)
# train_dataset, test_dataset = dataset['train'], dataset['test']
dataset, info = tfds.load('imdb_reviews/subwords8k', with_info=True, as_supervised=True)
train_dataset, test_dataset = dataset['train'].take(4000), dataset['test'].take(1000)

WARNING:absl:TFDS datasets with text encoding are deprecated and will be removed in a future version. Instead, you should use the plain text version and tokenize the text using `tensorflow_text` (See: https://www.tensorflow.org/tutorials/tensorflow_text/intro#tfdata_example)
tokenizer = info.features['text'].encoder
print(info)
tfds.core.DatasetInfo(
    name='imdb_reviews',
    full_name='imdb_reviews/subwords8k/1.0.0',
    description="""
    Large Movie Review Dataset.
    This is a dataset for binary sentiment classification containing substantially more data than previous benchmark datasets. We provide a set of 25,000 highly polar movie reviews for training, and 25,000 for testing. There is additional unlabeled data for use as well.
    """,
    config_description="""
    Uses `tfds.deprecated.text.SubwordTextEncoder` with 8k vocab size
    """,
    homepage='http://ai.stanford.edu/~amaas/data/sentiment/',
    data_path='/home/yunshu/tensorflow_datasets/imdb_reviews/subwords8k/1.0.0',
    download_size=80.23 MiB,
    dataset_size=54.72 MiB,
    features=FeaturesDict({
        'label': ClassLabel(shape=(), dtype=tf.int64, num_classes=2),
        'text': Text(shape=(None,), dtype=tf.int64, encoder=<SubwordTextEncoder vocab_size=8185>),
    }),
    supervised_keys=('text', 'label'),
    splits={
        'test': <SplitInfo num_examples=25000, num_shards=1>,
        'train': <SplitInfo num_examples=25000, num_shards=1>,
        'unsupervised': <SplitInfo num_examples=50000, num_shards=1>,
    },
    citation="""@InProceedings{maas-EtAl:2011:ACL-HLT2011,
      author    = {Maas, Andrew L.  and  Daly, Raymond E.  and  Pham, Peter T.  and  Huang, Dan  and  Ng, Andrew Y.  and  Potts, Christopher},
      title     = {Learning Word Vectors for Sentiment Analysis},
      booktitle = {Proceedings of the 49th Annual Meeting of the Association for Computational Linguistics: Human Language Technologies},
      month     = {June},
      year      = {2011},
      address   = {Portland, Oregon, USA},
      publisher = {Association for Computational Linguistics},
      pages     = {142--150},
      url       = {http://www.aclweb.org/anthology/P11-1015}
    }""",
)
BUFFER_SIZE = 100
BATCH_SIZE = 100

train_dataset = train_dataset.shuffle(BUFFER_SIZE).take(1000)
train_dataset = train_dataset.padded_batch(BATCH_SIZE)
test_dataset = test_dataset.padded_batch(BATCH_SIZE).take(1000)
vocab_size = 1000
model = tf.keras.Sequential([
    tf.keras.layers.Embedding(tokenizer.vocab_size, 8),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(8, return_sequences=True)),
    tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(8)),
    tf.keras.layers.Dense(16, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding (Embedding)        (None, None, 8)           65480     
_________________________________________________________________
bidirectional (Bidirectional (None, None, 16)          1088      
_________________________________________________________________
bidirectional_1 (Bidirection (None, 16)                1600      
_________________________________________________________________
dense (Dense)                (None, 16)                272       
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 17        
=================================================================
Total params: 68,457
Trainable params: 68,457
Non-trainable params: 0
_________________________________________________________________
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
NUM_EPOCHS = 10
history = model.fit(train_dataset, epochs=NUM_EPOCHS, validation_data=test_dataset)
Epoch 1/10
10/10 [==============================] - 13s 975ms/step - loss: 0.6932 - accuracy: 0.4875 - val_loss: 0.6933 - val_accuracy: 0.4970
Epoch 2/10
10/10 [==============================] - 7s 738ms/step - loss: 0.6921 - accuracy: 0.5104 - val_loss: 0.6933 - val_accuracy: 0.4970
Epoch 3/10
10/10 [==============================] - 7s 776ms/step - loss: 0.6902 - accuracy: 0.5088 - val_loss: 0.6929 - val_accuracy: 0.4970
Epoch 4/10
10/10 [==============================] - 7s 740ms/step - loss: 0.6853 - accuracy: 0.5119 - val_loss: 0.6915 - val_accuracy: 0.4970
Epoch 5/10
10/10 [==============================] - 7s 732ms/step - loss: 0.6717 - accuracy: 0.5249 - val_loss: 0.6869 - val_accuracy: 0.5120
Epoch 6/10
10/10 [==============================] - 7s 725ms/step - loss: 0.6245 - accuracy: 0.6617 - val_loss: 0.6652 - val_accuracy: 0.6010
Epoch 7/10
10/10 [==============================] - 7s 754ms/step - loss: 0.5250 - accuracy: 0.8814 - val_loss: 0.6589 - val_accuracy: 0.6580
Epoch 8/10
10/10 [==============================] - 7s 735ms/step - loss: 0.4295 - accuracy: 0.9559 - val_loss: 0.6577 - val_accuracy: 0.6690
Epoch 9/10
10/10 [==============================] - 7s 767ms/step - loss: 0.3467 - accuracy: 0.9799 - val_loss: 0.7183 - val_accuracy: 0.6810
Epoch 10/10
10/10 [==============================] - 8s 812ms/step - loss: 0.2967 - accuracy: 0.9850 - val_loss: 0.8752 - val_accuracy: 0.6610
import matplotlib.pyplot as plt


def plot_graphs(history, string):
  plt.plot(history.history[string])
  plt.plot(history.history['val_'+string])
  plt.xlabel("Epochs")
  plt.ylabel(string)
  plt.legend([string, 'val_'+string])
  plt.show()
plot_graphs(history, 'accuracy')


png

plot_graphs(history, 'loss')


png

希仔

2021/04/10  阅读:36  主题:默认主题

作者介绍

希仔