開始使用免費開始

嵌入向量能提升效能

嵌入層是否能提升模型的準確率?我們用同一份 IMDB 資料來驗證。

這個模型已經訓練了 10 個 epoch,設定與先前使用 simpleRNN 元件的模型相同。為了比較模型,環境中提供了測試集 (X_test, y_test),以及舊模型 simpleRNN_model。舊模型的準確率已載入在變數 acc_SimpleRNN

所有需要的模組與函式都已載入到環境中:keras.modelsSequential()keras.layersEmbeddingDense,以及 keras.layersSimpleRNN

本練習屬於課程

使用 Keras 建立語言模型的循環神經網路(RNN)

檢視課程

練習說明

  • 在模型中加入嵌入層。
  • 計算模型的準確率並存入變數 acc_embeddings
  • 印出舊模型與新模型的準確率。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create the model with embedding
model = Sequential(name="emb_model")
model.add(____(input_dim=max_vocabulary, output_dim=wordvec_dim, input_length=max_len))
model.add(SimpleRNN(units=128))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Load pre-trained weights
model.load_weights('embedding_model_weights.h5')

# Evaluate the models' performance (ignore the loss value)
_, ____ = model.evaluate(X_test, y_test, verbose=0)

# Print the results
print("SimpleRNN model's accuracy:\t{0}\nEmbeddings model's accuracy:\t{1}".format(acc_simpleRNN, ____))
編輯並執行程式碼