Python Tensorflow Keras嵌入层错误:层权重形状不兼容

Python Tensorflow Keras嵌入层错误:层权重形状不兼容,python,python-3.x,numpy,tensorflow,keras,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 weig

有人能为我推荐修复此类错误的最佳路径吗?我不知道我的尺寸有什么问题。我有一个预训练的嵌入源于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。。。 单张尺寸:32186 输出尺寸:100 权重:(1,) 形状下的权重:(32186100) 输入长度:100 --------------------------------------------------------------------------- ValueError回溯(最近一次调用上次) 在() 31权重=[嵌入矩阵], 32输入长度=固定长度, --->33可培训=错误) 34 model.add(Conv1D(num_filters,kernel_size=kernel_size,activation='relu',padding='same')) 35型号。添加(MaxPoolg1d(2)) c:\users\tt\anaconda3b\lib\site packages\tensorflow\u core\python\training\tracking\base.py in\u method\u包装(self,*args,**kwargs) 455 self._self_setattr_tracking=False#pylint:disable=protected access 456试试: -->457结果=方法(自身、*args、**kwargs) 458最后: 459 self._self_setattr_tracking=上一个值#pylint:disable=受保护访问 c:\users\tt\anaconda3b\lib\site packages\tensorflow\u core\python\keras\engine\sequential.py(self,layer) 176#并创建连接当前层的节点 177#到我们刚刚创建的输入层。 -->178层(x) 179设置_输入=真 180 c:\users\tt\anaconda3b\lib\site packages\tensorflow\u core\python\keras\engine\base\u layer.py in\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu 815#构建层(如适用)(如果已使用“构建”方法 816(已覆盖)。 -->817自我构建(输入) 818投切输入=自身。投切输入(输入) 819 c:\users\tt\anaconda3b\lib\site packages\tensorflow\u core\python\keras\engine\base\u layer.py in\u maybe\u build(self,inputs) 2146#可选加载层实例化时指定的重量值。 2147如果getattr(自身,“初始权重”为“无”)不是无: ->2148自身设置权重(自身初始权重) 2149自身初始重量=无 2150 c:\users\tt\anaconda3b\lib\site packages\tensorflow\u core\python\keras\engine\base\u layer.py in set\u weights(self,weights) 1334提升值错误('层重量形状'+str(参考形状)+ 1335“不兼容” ->1336'提供重量形状'+str(w形)) 1337权重值元组。附加((p,w)) 1338后端。批处理设置值(权重值元组)
ValueError:层权重形状(32187100)与提供的权重形状(32186100)不兼容
能否将嵌入层中的
vocab\u size+1
参数更改为
vocab\u size
。我认为是+1导致了这个问题

答案是编码的句子包含的值高于在词典构建阶段编码的值。你的词典中应该有一个索引,用于记录你的训练和测试集的每一个值。如果没有,你必须在发送给CNN之前清理句子。

你的w2v体重是从哪里来的?你如何预处理你的数据?是否提供了标记器或至少单词索引来匹配w2v权重?顺便说一句,
input\u length
不是词汇表大小,而是您将给出模型的序列长度。因为您当前的模型体系结构可以处理任何长度,所以您可能不应该费心设置它。W2V权重来自我构建的gensim模型,并使用nltk.word_tokenize进行初始化,然后将W2V训练为100D。感谢您提供有关输入长度的提示。我对CNN管道的尺寸感到非常困惑。原因是所有示例都已经完成了预处理,并且在示例文件中设置了所有维度。然后我应用到现实世界中,它很快变得很糟糕。谢谢你看。gensim型号的尺寸为32186x100。我将尝试删除它,看看会发生什么。另外,如果您知道任何好的示例,我很乐意阅读其他代码。在Github上找不到除已预处理的示例之外的任何可供学习的内容。将+1添加到vocab_大小并删除输入_长度会产生:InvalidArgumentError:Indexes[3,91]=60729不在[032186]中[[节点顺序_35/嵌入_43/嵌入_查找](在c:\users\ttruse\anaconda3b\lib\site packages\tensorflow\u core\python\framework\ops.py:1751中定义)][Op:\推理\分布式\函数\u 12609]函数调用堆栈:分布式函数我只在纯Keras中使用过我自己的嵌入,因此我不确定如何最好地使用nltk/gensim。我建议阅读更多关于文本预处理在一般情况下如何工作的内容,以便您能够理解哪些数字必须与哪些其他数字匹配。ValueError:层权重形状(32187100)与提供的重量形状(32186100)不兼容