def definite_model_no_GloVe(): title = "不使用预训练词嵌入" model = Sequential() model.add(Embedding(max_words, embedding_dim, input_length=max_len)) model.add(Flatten()) model.add(Dense(32, activation=relu)) model.add(Dense(1, activation=sigmoid)) model.summary() history = train_model(model) plot_classes_results(history, title, epochs) print(title + "评估测试集", model.evaluate(x_test, y_test))
def definite_dense(): title = "将 Dense 应用于 IMDB" model = Sequential(name=title) model.add(Dense(16, activation=relu, input_shape=(500, ))) model.add(Dense(16, activation=relu)) model.add(Dense(1, activation=sigmoid)) model.summary() model.compile(optimizer=rmsprop(lr=0.001), loss=binary_crossentropy, metrics=[binary_accuracy]) history = train_model(model, x_train, y_train) plot_classes_results(history, title, epochs) print(title + "评估测试集", model.evaluate(x_test, y_test, verbose=2, use_multiprocessing=True)) pass
def definite_rnn(): title = "将 SimpleRNN 应用于 IMDB " model = Sequential(name=title) model.add(Embedding(max_features, 64)) model.add(SimpleRNN(64)) model.add(Dense(1, activation=sigmoid)) model.summary() model.compile(optimizer=rmsprop(lr=0.001), loss=binary_crossentropy, metrics=[binary_accuracy]) history = train_model(model, x_train, y_train) plot_classes_results(history, title, epochs) print(title + "评估测试集", model.evaluate(x_test, y_test, verbose=2, use_multiprocessing=True)) pass
def simple_conv1d(): print("Listing 6.46:在 IMDB 数据上训练并且评估一个简单的一维卷积神经网络") model = Sequential(name = "简单的一维卷积神经网络") model.add(Embedding(max_features, embedding_size, input_length = max_len)) model.add(Conv1D(32, 7, activation = relu)) model.add(MaxPooling1D(5)) model.add(Conv1D(32, 7, activation = relu)) model.add(GlobalMaxPooling1D()) model.add(Dense(1)) model.summary() model.compile(optimizer = rmsprop(lr = 1e-4), loss = binary_crossentropy, metrics = [binary_accuracy]) history = model.fit(x_train, y_train, epochs = epochs, batch_size = batch_size, validation_split = validation_split, verbose = verbose, use_multiprocessing = True) title = "应用简单的一维卷积神经网络在 IMDB 数据集" plot_classes_results(history, title, epochs) pass
def definite_model_use_GloVe(): # Listing 6.12 模型定义 title = "使用预训练词嵌入" model = Sequential() model.add(Embedding(max_words, embedding_dim, input_length=max_len)) model.add(Flatten()) model.add(Dense(32, activation=relu)) model.add(Dense(1, activation=sigmoid)) # Listing 6.13 将 GloVe 训练好的词嵌入加载到 Embedding 层中 model.layers[0].set_weights([embedding_matrix]) model.layers[0].trainable = False model.summary() history = train_model(model) plot_classes_results(history, title, epochs) model.save_weights('pre_trained_glove_model.h5') model.load_weights('pre_trained_glove_model.h5') print(title + "评估测试集", model.evaluate(x_test, y_test))
def bidirectional_GRU(): print("Listing 6.44:训练并且评估一个双向 GRU") title = "使用双向 GRU 模型" model = Sequential(name=title) model.add(Embedding(max_features, 32)) model.add(Bidirectional(GRU(32))) model.add(Dense(1, activation=sigmoid)) model.summary() model.compile(optimizer=rmsprop(lr=0.001), loss=binary_crossentropy, metrics=[binary_accuracy]) history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_split=validation_split, verbose=verbose, use_multiprocessing=True) plot_classes_results(history, title, epochs)
def reversed_lstm(): print("Listing 6.42:使用逆序序列训练并且评估一个 LSTM 模型") title = "使用逆序序列的 LSTM 模型" model = Sequential(name=title) model.add(Embedding(max_features, 128)) model.add(LSTM(32)) model.add(Dense(1, activation=sigmoid)) model.summary() model.compile(optimizer=rmsprop(lr=0.001), loss=binary_crossentropy, metrics=[binary_accuracy]) history = model.fit(x_train_reversed, y_train, epochs=epochs, batch_size=batch_size, validation_split=validation_split, verbose=verbose, use_multiprocessing=True) plot_classes_results(history, title, epochs) pass