python - Tensorflow Keras 嵌入层错误 : Layer weight shape not compatible

标签 python python-3.x numpy tensorflow keras

任何人都可以推荐我修复此类错误的最佳路径吗?我不明白我的尺寸做错了什么。我有一个源自 Word2Vec gensim 模型的预训练嵌入,我想用它来初始化 CNN。抱歉,这个问题相对简单,但对 Keras 和 Tensorflow 来说都很陌生

#CNN architecture

num_classes = num_labels

#Training params
batch_size = 8 
num_epochs = 25

#Model parameters
num_filters = 64  
weight_decay = 1e-4
kernel_size = 7 #this is the size of the window during convolution...making match the window size in Word2Vec...unsure if needed

print("training CNN ...")

model = Sequential()

#------------------------
FIXED_LENGTH=embedding_matrix.shape[1]
#------------------------

print('Vocab size:', vocab_size)
print('Output_Dim size:', w2v.vector_size)
print('Weights:', pd.Series([embedding_matrix]).shape)
print('Weights underlying shape:', embedding_matrix.shape)
print("Input Length:", FIXED_LENGTH)

#Model add word2vec embedding

model.add(Embedding(vocab_size+1, 
                      output_dim=w2v.vector_size, 
                      weights=[embedding_matrix], 
                      input_length=FIXED_LENGTH, 
                      trainable=False))
model.add(Conv1D(num_filters, kernel_size=kernel_size, activation='relu', padding='same'))
model.add(MaxPooling1D(2))
model.add(Conv1D(num_filters, 7, activation='relu', padding='same'))
model.add(GlobalMaxPooling1D())
model.add(Dropout(0.5))
model.add(Dense(32, activation='relu', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(Dense(num_classes, activation='softmax'))  #multi-label (k-hot encoding)

adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(loss='sparse_categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
model.summary()

#define callbacks
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0.01, patience=4, verbose=1)
callbacks_list = [early_stopping]

print('Batch size:', batch_size)
print('Num of Epochs:', num_epochs)
print('X Train Size:', x_train_pad.shape)
print('Y Train Size:', y_train.shape)

hist = model.fit(x_train_pad, 
                 y_train, 
                 batch_size=batch_size, 
                 epochs=num_epochs, 
                 callbacks=callbacks_list, 
                 validation_split=0.1, 
                 shuffle=True, 
                 verbose=2)

输出是:

training CNN ...
Vocab size: 32186
Output_Dim size: 100
Weights: (1,)
Weights underlying shape: (32186, 100)
Input Length: 100
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-326-36db7b551866> in <module>()
     31                       weights=[embedding_matrix],
     32                       input_length=FIXED_LENGTH,
---> 33                       trainable=False))
     34 model.add(Conv1D(num_filters, kernel_size=kernel_size, activation='relu', padding='same'))
     35 model.add(MaxPooling1D(2))

c:\users\tt\anaconda3b\lib\site-packages\tensorflow_core\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
    455     self._self_setattr_tracking = False  # pylint: disable=protected-access
    456     try:
--> 457       result = method(self, *args, **kwargs)
    458     finally:
    459       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

c:\users\tt\anaconda3b\lib\site-packages\tensorflow_core\python\keras\engine\sequential.py in add(self, layer)
    176           # and create the node connecting the current layer
    177           # to the input layer we just created.
--> 178           layer(x)
    179           set_inputs = True
    180 

c:\users\tt\anaconda3b\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
    815           # Build layer if applicable (if the `build` method has been
    816           # overridden).
--> 817           self._maybe_build(inputs)
    818           cast_inputs = self._maybe_cast_inputs(inputs)
    819 

c:\users\tt\anaconda3b\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py in _maybe_build(self, inputs)
   2146     # Optionally load weight values specified at layer instantiation.
   2147     if getattr(self, '_initial_weights', None) is not None:
-> 2148       self.set_weights(self._initial_weights)
   2149       self._initial_weights = None
   2150 

c:\users\tt\anaconda3b\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py in set_weights(self, weights)
   1334         raise ValueError('Layer weight shape ' + str(ref_shape) +
   1335                          ' not compatible with '
-> 1336                          'provided weight shape ' + str(w.shape))
   1337       weight_value_tuples.append((p, w))
   1338     backend.batch_set_value(weight_value_tuples)

ValueError: Layer weight shape (32187, 100) not compatible with provided weight shape (32186, 100)

最佳答案

答案是编码的句子包含的值高于词典构建阶段编码的值。您的词典中应该有一个针对训练和测试集的每个值的索引。如果没有,您必须先清理句子,然后再将其发送到 CNN。

关于python - Tensorflow Keras 嵌入层错误 : Layer weight shape not compatible,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58548520/

相关文章:

python - 如何对数据框中的 groupby 行应用计算并将结果 append 到数据框的底部?

python - python for循环和3D numpy矩阵加法之间的等价

python - 如何沿给定轴取元素,由它们的索引给出?

python-3.x - 带圆形图案的相机校准

python - 从字符串数组中删除 nan

python - 在Python中将列表转换为字典

python:如何起床直到我的代码出现最后一个错误

python - 如何使用 python configparser 读取缩进部分

python - 如果有很多小部件,则使按钮向左对齐

python-3.x - 调用一个函数,产生两次