<jupyter_start><jupyter_text>数据导入：<jupyter_code>import os
imdb_dir = 'data\\aclImdb'
train_dir = os.path.join(imdb_dir, 'train')
labels = []
texts = []

for label_type in ['neg', 'pos']:
    dir_name=os.path.join(train_dir, label_type)
    
    count =0 
    for fname in os.listdir(dir_name):
        if fname[-4:] == '.txt':
            with open(os.path.join(dir_name, fname), encoding='utf-8') as f:
                texts.append(f.read())

            count += 1
            if label_type == 'neg':
                if count %100 == 0:
                    print('neg')
                labels.append(0)
            else:
                labels.append(1)
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import numpy as np
maxlen = 200
max_words = 9000

tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(texts)

sequences = tokenizer.texts_to_sequences(texts)
word_index = tokenizer.word_index

data = pad_sequences(sequences, maxlen=maxlen)
labels = np.asarray(labels) # label是正负评论，正是1，负是0

indices = np.arange(data.shape[0])
np.random.shuffle(indices)
data = data[indices]
labels = labels[indices]
training_samples = 10000
validation_samples = 2000

x_train = data[:training_samples] 
y_train = labels[:training_samples]
x_val = data[training_samples: training_samples + validation_samples]
y_val = labels[training_samples: training_samples + validation_samples]<jupyter_output><empty_output><jupyter_text>[Keras中文文档](https://keras-cn.readthedocs.io/en/latest/preprocessing/text/)<jupyter_code>import keras
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Embedding, SimpleRNN
from keras.layers import LSTM
max_features = max_words

model = Sequential()
model.add(Embedding(max_features, 32, input_length=None))
model.add(SimpleRNN(32))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history = model.fit(x_train, y_train,
                    epochs=3,
                    batch_size=128,
                    validation_split=0.2)
# word_index = tokenizer.word_index
# index_word = tokenizer.index_word
comment = 'this is a stupid movie'.split(' ')
INDEX=[]
for i in comment:
    INDEX.append(word_index[i])
INDEX = np.array(INDEX)
model.predict_classes(pad_sequences(INDEX[None,], maxlen=maxlen))
import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12931323283082077, 'fraction_numerical': 0.01407035175879397, 'mean_word_length': 4.4489051094890515, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '591053', 'license': 'MIT', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1010, 'n_words': 203, 'path': 'RNN/RNN&LSTM.ipynb', 'repo_name': 'beidongjiedeguang/my-deep-learning', 'size': '2985'}