能否修改Keras训练后的Embedding层并扩展词向量维度?
Absolutely, this is totally doable! Let me walk you through exactly how to pull this off:
Step 1: Extract the trained embedding matrix
First, you need to get hold of the word embedding weights your model learned. The Embedding layer's weights are stored as a numpy array, which you can access like this:
import numpy as np # Get the original 8-dimensional embedding matrix (shape: (100, 8)) original_embedding = model.layers[0].get_weights()[0]
Note: Since your Embedding layer is the first layer in the model, we use layers[0] here. Adjust the index if your layer order is different.
Step 2: Prepare your extra 2-dimensional data
Assuming you've already calculated the 2 additional values for each of your 100 words, store them in a numpy array with shape (100, 2). For example:
# Replace this with your actual computed values extra_dims = np.random.rand(100, 2) # Simulating 2 extra dimensions for 100 words
Make sure the first dimension (100) matches the number of words in your original embedding—this is critical for successful concatenation.
Step 3: Combine the original embedding with extra dimensions
Use numpy's hstack to append the extra dimensions to each word vector:
# New embedding matrix with shape (100, 10) (8 original + 2 extra) new_embedding_matrix = np.hstack((original_embedding, extra_dims))
Step 4: Update your model with the new embedding
You have two options here, depending on whether you want to keep your original model structure or build a new one:
Option 1: Build a new model with the updated embedding layer
from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Embedding, Flatten, Dense new_model = Sequential() # Define the new Embedding layer with output_dim=10 (instead of 8) new_model.add(Embedding( input_dim=100, output_dim=10, input_length=max_length, weights=[new_embedding_matrix] # Load the combined embedding matrix )) new_model.add(Flatten()) new_model.add(Dense(1, activation='sigmoid')) # Recompile the model new_model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) print(new_model.summary())
Option 2: Modify the original model in-place
If you want to tweak your existing model instead of creating a new one:
# Remove the original Embedding layer from the model model.pop(0) # Insert the new Embedding layer at the front model.insert( 0, Embedding( input_dim=100, output_dim=10, input_length=max_length, weights=[new_embedding_matrix] ) ) # Recompile to apply changes model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc']) print(model.summary())
Key Notes
- Double-check that the shape of your
extra_dimsarray matches the number of words in your embedding (100 in your case). Mismatched shapes will cause errors during concatenation. - When updating the model, ensure the
output_dimof the newEmbeddinglayer matches the size of your combined embedding matrix (10, in this case). - The
Flattenlayer will automatically adapt to the new input size (frommax_length*8tomax_length*10), so you don't need to adjust subsequentDenselayers unless they had hard-coded input dimensions.
内容的提问来源于stack exchange,提问作者Ziqi

