简单的keras密集模型在拟合时冻结

时间:2019-11-05 12:28:10

标签: nlp tensorflow2.0 data-fitting keras-2

我正在与Keras学习NLP,并且正在阅读教程。代码如下:

import tensorflow_datasets as tfds
imdb, info = tfds.load("imdb_reviews", with_info=True, as_supervised=True)


import numpy as np

train_data, test_data = imdb['train'], imdb['test']

training_sentences = []
training_labels = []

testing_sentences = []
testing_labels = []

# str(s.tonumpy()) is needed in Python3 instead of just s.numpy()
for s,l in train_data:
  training_sentences.append(str(s.numpy()))
  training_labels.append(l.numpy())


training_labels_final = np.array(training_labels)
testing_labels_final = np.array(testing_labels)

vocab_size = 10000
embedding_dim = 16
max_length = 120
trunc_type='post'
oov_tok = "<OOV>"


from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

# CREATE AN INSTANCE OF THE Tokenizer.  WE DECIDE HOW MANY WORDS THE TOKENIZER WILL READ.

tokenizer = Tokenizer(num_words = vocab_size, oov_token=oov_tok)


# FIT THE TOKENIZER ON THE TEXTS. NOW THE TOKENIZER HAS SEEN THE WORDS IN THE TEXT.

tokenizer.fit_on_texts(training_sentences) # the training_sentences is a list of words.  Each word is considered a token.


# CREATE A DICTIONARY THAT INCLUDES ALL THE WORDS IN THE TEXT (UP TO THE MAXIMUM NUMBER DEFINED WHEN CREATING THE INSTANCE
# OF THE TOKENIZER)

word_index = tokenizer.word_index # the tokenizer creates a word_index with the words encountered in the text.  Each word
                                  # is assigned an integer which is the key while the word is the value.

# CONVERT THE SEQUENCES OF WORDS TO SEQUENCES OF INTEGERS

sequences = tokenizer.texts_to_sequences(training_sentences)  # the texts_to_sequences method converts the sequences of
                                            # words to sequences of integers using the key of each word in the dictionary

# PAD THE SEQUENCES OR TRUNCATE THEM ACCORDINGLY SO THAT ALL HAVE THE GIVEN max_length. NOW ALL SEQUENCES HAVE THE SAME LENGTH.

padded = pad_sequences(sequences,maxlen=max_length, truncating=trunc_type)

# THE SAME FOR THE SEQUENCES WHICH WILL BE USED FOR TESTING

testing_sequences = tokenizer.texts_to_sequences(testing_sentences)
testing_padded = pad_sequences(testing_sequences,maxlen=max_length)

# REVERSE THE DICTIONARY, MAKING KEYS THE WORDS AND VALUES THE INTEGERS WHICH REPRESENT THE WORDS

reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])




# CREATE A FUNCTION THAT TURNS THE INTEGERS COMPRISING A SEQUENCE TO WORDS THUS DECODING THE SEQUENCE AND CONVERTING IT TO
# NATURAL LANGUAGE

def decode_review(text):
    return ' '.join([reverse_word_index.get(i, '?') for i in text])


model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),

    # The Embedding layer gets as first argumnet the vocab_size which has been set to 10,000 and which was the value
    # passed to the Tokenizer.  On the other hand the vocabulary that was created using the training text was less
    # than vocab_size, it was 86539

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(6, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.summary()


num_epochs = 10
model.fit(padded, training_labels_final, epochs=num_epochs, validation_data=(testing_padded, testing_labels_final))

在第一个时期结束时,模型冻结并且没有进一步的进展:

enter image description here

当我删除最后一千个句子并重复该过程时,我遇到了同样的情况,但现在更早了:

enter image description here

我重新启动了PC(Windows 10),但这不能解决问题。

然后我卸载了tensorflow并重新安装。然后我运行在tensorflow 2.0的官方文档中找到的以下代码:

enter image description here

但是当我再次运行NLP代码时,模型在拟合数据时冻结了:

num_epochs = 10
model.fit(padded, training_labels_final, epochs=num_epochs, validation_data=(testing_padded, testing_labels_final))
Train on 24000 samples
Epoch 1/10
23968/24000 [============================>.] - ETA: 6:09 - loss: 0.6878 - accuracy: 0.53 - ETA: 1:22 - loss: 0.6904 - accuracy: 0.56 - ETA: 50s - loss: 0.6927 - accuracy: 0.5069 - ETA: 37s - loss: 0.6932 - accuracy: 0.495 - ETA: 31s - loss: 0.6925 - accuracy: 0.492 - ETA: 26s - loss: 0.6923 - accuracy: 0.492 - ETA: 24s - loss: 0.6925 - accuracy: 0.490 - ETA: 21s - loss: 0.6925 - accuracy: 0.493 - ETA: 20s - loss: 0.6929 - accuracy: 0.490 - ETA: 19s - loss: 0.6931 - accuracy: 0.487 - ETA: 18s - loss: 0.6929 - accuracy: 0.490 - ETA: 17s - loss: 0.6929 - accuracy: 0.492 - ETA: 16s - loss: 0.6930 - accuracy: 0.489 - ETA: 15s - loss: 0.6927 - accuracy: 0.494 - ETA: 15s - loss: 0.6925 - accuracy: 0.498 - ETA: 14s - loss: 0.6925 - accuracy: 0.501 - ETA: 14s - loss: 0.6924 - accuracy: 0.504 - ETA: 14s - loss: 0.6925 - accuracy: 0.502 - ETA: 13s - loss: 0.6926 - accuracy: 0.503 - ETA: 13s - loss: 0.6925 - accuracy: 0.503 - ETA: 13s - loss: 0.6924 - accuracy: 0.506 - ETA: 12s - loss: 0.6926 - accuracy: 0.506 - ETA: 12s - loss: 0.6924 - accuracy: 0.508 - ETA: 12s - loss: 0.6924 - accuracy: 0.508 - ETA: 12s - loss: 0.6922 - accuracy: 0.508 - ETA: 12s - loss: 0.6920 - accuracy: 0.509 - ETA: 11s - loss: 0.6921 - accuracy: 0.509 - ETA: 11s - loss: 0.6917 - accuracy: 0.514 - ETA: 11s - loss: 0.6917 - accuracy: 0.513 - ETA: 11s - loss: 0.6918 - accuracy: 0.512 - ETA: 11s - loss: 0.6915 - accuracy: 0.515 - ETA: 11s - loss: 0.6911 - accuracy: 0.517 - ETA: 10s - loss: 0.6911 - accuracy: 0.517 - ETA: 10s - loss: 0.6911 - accuracy: 0.516 - ETA: 10s - loss: 0.6910 - accuracy: 0.517 - ETA: 10s - loss: 0.6909 - accuracy: 0.517 - ETA: 10s - loss: 0.6907 - accuracy: 0.516 - ETA: 10s - loss: 0.6902 - accuracy: 0.518 - ETA: 10s - loss: 0.6900 - accuracy: 0.518 - ETA: 9s - loss: 0.6896 - accuracy: 0.518 - ETA: 9s - loss: 0.6898 - accuracy: 0.51 - ETA: 9s - loss: 0.6893 - accuracy: 0.51 - ETA: 9s - loss: 0.6891 - accuracy: 0.52 - ETA: 9s - loss: 0.6887 - accuracy: 0.52 - ETA: 9s - loss: 0.6883 - accuracy: 0.52 - ETA: 9s - loss: 0.6880 - accuracy: 0.52 - ETA: 9s - loss: 0.6878 - accuracy: 0.52 - ETA: 9s - loss: 0.6874 - accuracy: 0.52 - ETA: 8s - loss: 0.6868 - accuracy: 0.52 - ETA: 8s - loss: 0.6863 - accuracy: 0.52 - ETA: 8s - loss: 0.6857 - accuracy: 0.52 - ETA: 8s - loss: 0.6853 - accuracy: 0.52 - ETA: 8s - loss: 0.6851 - accuracy: 0.52 - ETA: 8s - loss: 0.6845 - accuracy: 0.52 - ETA: 8s - loss: 0.6838 - accuracy: 0.53 - ETA: 8s - loss: 0.6829 - accuracy: 0.53 - ETA: 8s - loss: 0.6819 - accuracy: 0.53 - ETA: 8s - loss: 0.6803 - accuracy: 0.53 - ETA: 8s - loss: 0.6799 - accuracy: 0.53 - ETA: 8s - loss: 0.6791 - accuracy: 0.54 - ETA: 7s - loss: 0.6785 - accuracy: 0.54 - ETA: 7s - loss: 0.6779 - accuracy: 0.54 - ETA: 7s - loss: 0.6773 - accuracy: 0.54 - ETA: 7s - loss: 0.6765 - accuracy: 0.55 - ETA: 7s - loss: 0.6751 - accuracy: 0.55 - ETA: 7s - loss: 0.6745 - accuracy: 0.55 - ETA: 7s - loss: 0.6732 - accuracy: 0.55 - ETA: 7s - loss: 0.6722 - accuracy: 0.56 - ETA: 7s - loss: 0.6714 - accuracy: 0.56 - ETA: 7s - loss: 0.6699 - accuracy: 0.56 - ETA: 7s - loss: 0.6691 - accuracy: 0.56 - ETA: 7s - loss: 0.6680 - accuracy: 0.57 - ETA: 7s - loss: 0.6667 - accuracy: 0.57 - ETA: 6s - loss: 0.6657 - accuracy: 0.57 - ETA: 6s - loss: 0.6642 - accuracy: 0.57 - ETA: 6s - loss: 0.6632 - accuracy: 0.57 - ETA: 6s - loss: 0.6624 - accuracy: 0.58 - ETA: 6s - loss: 0.6614 - accuracy: 0.58 - ETA: 6s - loss: 0.6598 - accuracy: 0.58 - ETA: 6s - loss: 0.6591 - accuracy: 0.58 - ETA: 6s - loss: 0.6580 - accuracy: 0.59 - ETA: 6s - loss: 0.6573 - accuracy: 0.59 - ETA: 6s - loss: 0.6563 - accuracy: 0.59 - ETA: 6s - loss: 0.6554 - accuracy: 0.59 - ETA: 6s - loss: 0.6548 - accuracy: 0.59 - ETA: 6s - loss: 0.6538 - accuracy: 0.60 - ETA: 6s - loss: 0.6524 - accuracy: 0.60 - ETA: 6s - loss: 0.6521 - accuracy: 0.60 - ETA: 5s - loss: 0.6506 - accuracy: 0.60 - ETA: 5s - loss: 0.6497 - accuracy: 0.60 - ETA: 5s - loss: 0.6485 - accuracy: 0.61 - ETA: 5s - loss: 0.6472 - accuracy: 0.61 - ETA: 5s - loss: 0.6461 - accuracy: 0.61 - ETA: 5s - loss: 0.6451 - accuracy: 0.61 - ETA: 5s - loss: 0.6438 - accuracy: 0.61 - ETA: 5s - loss: 0.6428 - accuracy: 0.61 - ETA: 5s - loss: 0.6426 - accuracy: 0.62 - ETA: 5s - loss: 0.6416 - accuracy: 0.62 - ETA: 5s - loss: 0.6403 - accuracy: 0.62 - ETA: 5s - loss: 0.6393 - accuracy: 0.62 - ETA: 5s - loss: 0.6380 - accuracy: 0.62 - ETA: 5s - loss: 0.6368 - accuracy: 0.62 - ETA: 5s - loss: 0.6354 - accuracy: 0.63 - ETA: 4s - loss: 0.6345 - accuracy: 0.63 - ETA: 4s - loss: 0.6335 - accuracy: 0.63 - ETA: 4s - loss: 0.6323 - accuracy: 0.63 - ETA: 4s - loss: 0.6313 - accuracy: 0.63 - ETA: 4s - loss: 0.6300 - accuracy: 0.63 - ETA: 4s - loss: 0.6291 - accuracy: 0.64 - ETA: 4s - loss: 0.6281 - accuracy: 0.64 - ETA: 4s - loss: 0.6270 - accuracy: 0.64 - ETA: 4s - loss: 0.6260 - accuracy: 0.64 - ETA: 4s - loss: 0.6246 - accuracy: 0.64 - ETA: 4s - loss: 0.6238 - accuracy: 0.64 - ETA: 4s - loss: 0.6228 - accuracy: 0.64 - ETA: 4s - loss: 0.6216 - accuracy: 0.65 - ETA: 4s - loss: 0.6204 - accuracy: 0.65 - ETA: 4s - loss: 0.6199 - accuracy: 0.65 - ETA: 4s - loss: 0.6189 - accuracy: 0.65 - ETA: 3s - loss: 0.6176 - accuracy: 0.65 - ETA: 3s - loss: 0.6165 - accuracy: 0.65 - ETA: 3s - loss: 0.6150 - accuracy: 0.66 - ETA: 3s - loss: 0.6144 - accuracy: 0.66 - ETA: 3s - loss: 0.6132 - accuracy: 0.66 - ETA: 3s - loss: 0.6118 - accuracy: 0.66 - ETA: 3s - loss: 0.6108 - accuracy: 0.66 - ETA: 3s - loss: 0.6096 - accuracy: 0.66 - ETA: 3s - loss: 0.6088 - accuracy: 0.66 - ETA: 3s - loss: 0.6077 - accuracy: 0.66 - ETA: 3s - loss: 0.6064 - accuracy: 0.67 - ETA: 3s - loss: 0.6054 - accuracy: 0.67 - ETA: 3s - loss: 0.6046 - accuracy: 0.67 - ETA: 3s - loss: 0.6037 - accuracy: 0.67 - ETA: 3s - loss: 0.6028 - accuracy: 0.67 - ETA: 3s - loss: 0.6021 - accuracy: 0.67 - ETA: 3s - loss: 0.6012 - accuracy: 0.67 - ETA: 2s - loss: 0.6006 - accuracy: 0.67 - ETA: 2s - loss: 0.5997 - accuracy: 0.67 - ETA: 2s - loss: 0.5988 - accuracy: 0.67 - ETA: 2s - loss: 0.5974 - accuracy: 0.68 - ETA: 2s - loss: 0.5965 - accuracy: 0.68 - ETA: 2s - loss: 0.5960 - accuracy: 0.68 - ETA: 2s - loss: 0.5952 - accuracy: 0.68 - ETA: 2s - loss: 0.5941 - accuracy: 0.68 - ETA: 2s - loss: 0.5925 - accuracy: 0.68 - ETA: 2s - loss: 0.5918 - accuracy: 0.68 - ETA: 2s - loss: 0.5907 - accuracy: 0.68 - ETA: 2s - loss: 0.5900 - accuracy: 0.68 - ETA: 2s - loss: 0.5893 - accuracy: 0.69 - ETA: 2s - loss: 0.5888 - accuracy: 0.69 - ETA: 2s - loss: 0.5880 - accuracy: 0.69 - ETA: 2s - loss: 0.5873 - accuracy: 0.69 - ETA: 2s - loss: 0.5867 - accuracy: 0.69 - ETA: 1s - loss: 0.5854 - accuracy: 0.69 - ETA: 1s - loss: 0.5848 - accuracy: 0.69 - ETA: 1s - loss: 0.5844 - accuracy: 0.69 - ETA: 1s - loss: 0.5837 - accuracy: 0.69 - ETA: 1s - loss: 0.5829 - accuracy: 0.69 - ETA: 1s - loss: 0.5822 - accuracy: 0.69 - ETA: 1s - loss: 0.5817 - accuracy: 0.69 - ETA: 1s - loss: 0.5810 - accuracy: 0.70 - ETA: 1s - loss: 0.5803 - accuracy: 0.70 - ETA: 1s - loss: 0.5798 - accuracy: 0.70 - ETA: 1s - loss: 0.5789 - accuracy: 0.70 - ETA: 1s - loss: 0.5786 - accuracy: 0.70 - ETA: 1s - loss: 0.5782 - accuracy: 0.70 - ETA: 1s - loss: 0.5772 - accuracy: 0.70 - ETA: 1s - loss: 0.5767 - accuracy: 0.70 - ETA: 1s - loss: 0.5761 - accuracy: 0.70 - ETA: 1s - loss: 0.5756 - accuracy: 0.70 - ETA: 0s - loss: 0.5751 - accuracy: 0.70 - ETA: 0s - loss: 0.5747 - accuracy: 0.70 - ETA: 0s - loss: 0.5739 - accuracy: 0.70 - ETA: 0s - loss: 0.5733 - accuracy: 0.70 - ETA: 0s - loss: 0.5727 - accuracy: 0.71 - ETA: 0s - loss: 0.5720 - accuracy: 0.71 - ETA: 0s - loss: 0.5712 - accuracy: 0.71 - ETA: 0s - loss: 0.5705 - accuracy: 0.71 - ETA: 0s - loss: 0.5698 - accuracy: 0.71 - ETA: 0s - loss: 0.5692 - accuracy: 0.71 - ETA: 0s - loss: 0.5685 - accuracy: 0.71 - ETA: 0s - loss: 0.5680 - accuracy: 0.71 - ETA: 0s - loss: 0.5676 - accuracy: 0.71 - ETA: 0s - loss: 0.5670 - accuracy: 0.71 - ETA: 0s - loss: 0.5663 - accuracy: 0.71 - ETA: 0s - loss: 0.5657 - accuracy: 0.71 - ETA: 0s - loss: 0.5650 - accuracy: 0.71 - ETA: 0s - loss: 0.5643 - accuracy: 0.7185

1 个答案:

答案 0 :(得分:2)

您忘了像使用test_data一样检索train_data。.您应该在training_labels_final = np.array(training_labels)之前添加以下代码

for s,l in test_data:
  testing_sentences.append(str(s.numpy()))
  testing_labels.append(l.numpy())