目录
import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers
import matplotlib.pyplot as plt %matplotlib inline
加载Tensorflow的dataset数据
data = keras.datasets.imdb#电影评论数据
max_word = 10000
(x_train,y_train),(x_test,y_test) = data.load_data(num_words=max_word)
文本训练场密集向量
x_train = keras.preprocessing.sequence.pad_sequences(x_train,300)
x_test = keras.preprocessing.sequence.pad_sequences(x_test,300)
创建模型
model = keras.models.Sequential()
model.add(layers.Embedding(10000,50,input_length=300))
#(None,300,50)
#model.add(layers.Flatten())#Flatten转换为二维
model.add(layers.GlobalAveragePooling1D())#Flatten转换为二维
model.add(layers.Dense(64,activation='relu'))
model.add(layers.Dense(1,activation='sigmoid'))#二分类问题,y_train
配置模型
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=['acc']
)
模型训练
history = model.fit(x_train,y_train,epochs=15,batch_size=256,validation_data=(x_test,y_test))
查看history中的参数
history.history.keys()
dict_keys(['loss', 'acc', 'val_loss', 'val_acc'])
绘制loss变化曲线
plt.plot(history.epoch,history.history['loss'],'r')
plt.plot(history.epoch,history.history['val_loss'],'b--')
绘制accuracy变化曲线
plt.plot(history.epoch,history.history['acc'],'r')
plt.plot(history.epoch,history.history['val_acc'],'b--')