import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
num_words = 30000 #单词数量
maxlen = 200 #每个句子最大的长度,在下面会把训练集的每个句子进行长切,短的补0
# x_train:25000 x None
(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:25000 x 200
#数据转化为25000条,每条长度是200个词
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)
#建立模型model=>编译模型compile(训练在后面的fit函数)
def lstm_model():
model = keras.Sequential([
# input_dim代表字典长度,output_dim 转化为32维的词嵌入
#input_length:每个句子的长度
layers.Embedding(input_dim=30000, output_dim=32, input_length=maxlen),
#return_sequences:默认 False。在输出序列中,返回单个 hidden state值还是返回
#全部time step 的 hidden state值。 False 返回单个, true 返回全部
layers.LSTM(32, return_sequences=True),
layers.LSTM(1, activation='sigmoid', return_sequences=False)
])
model.compile(optimizer=keras.optimizers.Adam(),#优化算法
loss=keras.losses.BinaryCrossentropy(),#loss计算方法
metrics=['accuracy'])#衡量标准
return model
model = lstm_model()
model.summary() #把model展示出来
#训练
history = model.fit(x_train, y_train, batch_size=64, epochs=5,validation_split=0.1)
import matplotlib.pyplot as plt
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['training', 'valivation'], loc='upper left')
plt.show()
LSTM——tensorflow2.0学习
于 2022-08-25 10:53:43 首次发布