import matplotlib.pyplot as plt import tensorflow as tf from tensorflow import keras import numpy as np #加载IMDB数据 imdb = keras.datasets.imdb (train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=100) print("训练记录数量:{},标签数量:{}".format(len(train_data),len(train_labels))) print(train_data[0]) #数据标准化 train_data = keras.preprocessing.sequence.pad_sequences(train_data,value=0,padding='post',maxlen=256) #text_data = keras.preprocessing.sequence.pad_sequences(train_data,value=0,padding='post',maxlen=256) text_data = keras.preprocessing.sequence.pad_sequences(test_data,value=0,padding='post',maxlen=256) print(train_data[0]) #构建模型 vocab_size = 10000 model = tf.keras.Sequential([tf.keras.layers.Embedding(vocab_size, 64), tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)), tf.keras.layers.Dense(64,activation='relu'), tf.keras.layers.Dense(1) ]) model.summary() #配置并训练模型 model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy']) x_val = train_data[:10000] partial_x_train = train_data[10000:] y_val = train_labels[:10000] partial_y_train = train_labels[10000:] history = model.fit(partial_x_train,partial_y_train,epochs=1,batch_size=512,validation_data=(x_val,y_val),verbose=1) #测试性能 results = model.evaluate(test_data, test_labels, verbose=2) print(results) #训练过程可视化 history_dict = history.history print(history_dict.keys()) 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") plot_graphs(history,"loss")
时间: 2024-03-14 11:49:03 浏览: 145
import numpy as np import matplotlib.pyplot as plt import math
这段代码使用了IMDB电影评论数据集,通过对评论进行情感分析,将评论分为正面或负面两类。其中,数据标准化使用了keras.preprocessing.sequence.pad_sequences函数,将每条评论的长度都设置为256,模型使用了嵌入层、双向LSTM层和2个全连接层,其中嵌入层的大小为10000,LSTM层的大小为64,全连接层的大小为64和1。模型的优化器使用了Adam,损失函数使用了二元交叉熵,评价指标使用了准确率。并且,使用了matplotlib库,对模型的训练过程进行可视化。
阅读全文