首页 文章

在Keras中嵌入层的偏差权重

提问于
浏览
2

我正在研究前馈NN,我正在使用keras嵌入 . 我想为嵌入设置偏重,但我不知道该怎么做 .

Keras密集层允许指定use_bias = True,然后设置偏差权重 . 是否有嵌入图层的等效方法?

1 回答

  • 4

    你可以使用另一个矢量长度等于1的嵌入作为偏差 . 例如,下面的代码获取输入a和b的嵌入和偏差,取两个向量的点积,然后使用点积添加偏差 .

    from keras.models import Model
    from keras.layers import Embedding, Input, Add, Dot
    
    a = Input(shape=(1,))
    b = Input(shape=(1,))
    
    emb_a = Embedding(num_words+1, 50)(a)
    bias_a = Embedding(num_words+1, 1)(a)
    emb_b = Embedding(num_words+1, 50)(b)
    bias_b = Embedding(num_words+1, 1)(b)
    
    dot = Dot(axes=-1)([a,b])
    add = Add()([dot,bias_a,bias_b])
    

相关问题