【问题标题】:Pre-trained embedding layer: tf.constant with unsupported shape预训练嵌入层:tf.constant,形状不受支持
【发布时间】:2021-03-03 16:55:00
【问题描述】:

我将在 Keras 模型中使用预训练的词嵌入。我的矩阵权重存储在 ;matrix.w2v.wv.vectors.npy;它有形状 (150854, 100)。

现在当我在 Keras 模型中添加嵌入层时,使用不同的参数如下:

model.add(Embedding(5000, 100,
    embeddings_initializer=keras.initializers.Constant(emb_matrix),
    input_length=875, trainable=False))

我收到以下错误:

---------------------------------------------------------------------------
TypeError                         Traceback (most recent call last)
<ipython-input-61-8731e904e60a> in <module>()
  1 model = Sequential()
  2 
----> 3 model.add(Embedding(5000,100,
   embeddings_initializer=keras.initializers.Constant(emb_matrix),
   input_length=875,trainable=False))
  4 model.add(Conv1D(128, 10, padding='same', activation='relu'))
  5 model.add(MaxPooling1D(10))

  22 frames
 
 /usr/local/lib/python3.7/dist- 
 packages/tensorflow/python/framework/constant_op.py in 
_constant_eager_impl(ctx, value, dtype, shape, verify_shape)
  323   raise TypeError("Eager execution of tf.constant with unsupported shape 
             "
  324                   "(value has %d elements, shape is %s with %d 
                        elements)." %
--> 325                   (num_t, shape, shape.num_elements()))
  326 
  327 

  TypeError: Eager execution of tf.constant with unsupported shape (value has 
  15085400 elements, shape is (5000, 100) with 500000 elements).

请告诉我哪里做错了。

【问题讨论】:

    标签: keras word2vec keras-layer embedding


    【解决方案1】:

    您的嵌入层需要 5,000 个单词的词汇表,并初始化形状为 5000×100 的嵌入矩阵。然而。您尝试加载的 word2vec 模型有 150,854 个单词的词汇表。

    您要么需要增加嵌入层的容量,要么截断嵌入矩阵以仅允许最常用的词。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-18
      • 2021-07-03
      • 2020-05-29
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      • 2018-12-21
      • 2018-10-19
      相关资源
      最近更新 更多