在“i2h”层上调用 set_weights(weights),权重列表长度为 3418,但该层期望权重为 0

dis*_*ame 8 keras

我正在尝试用 Keras 实现 word2vec 算法,但我得到了

ValueError: You called `set_weights(weights)` on layer "i2h" with a  weight list of length 3418, but the layer was expecting 2 weights. Provided weights: [[ 0.07142857  0.07142857  0.07142857 ...,  0.0714...
Run Code Online (Sandbox Code Playgroud)

当我尝试为从输入到隐藏层的共享矩阵设置权重时i2h

ValueError: You called `set_weights(weights)` on layer "i2h" with a  weight list of length 3418, but the layer was expecting 2 weights. Provided weights: [[ 0.07142857  0.07142857  0.07142857 ...,  0.0714...
Run Code Online (Sandbox Code Playgroud)

我不太明白如何设置这个权重矩阵。

我也尝试使用Dense()图层作为输入

class Word2Vec:

    def __init__(self, window_size, word_vectors):

        vocab_size = word_vectors.shape[0]
        embedding_size = word_vectors.shape[1]

        i2h = Dense(embedding_size, activation='linear', name='i2h')

        inputs = list()
        h_activations = list()

        for i in range(window_size):

            in_x = Input(shape=(vocab_size, 1), name='in_{:d}'.format(i))
            inputs.append(in_x)
            h_activation = i2h(in_x)
            h_activations.append(h_activation)

        i2h.set_weights(word_vectors)

        h = merge(h_activations, mode='ave')

        h2out = Dense(vocab_size, activation='softmax', name='out')(h)

        self.model = Model(input=inputs, output=[h2out])
        self.model.compile(optimizer='adam', loss='mse')
Run Code Online (Sandbox Code Playgroud)

但我遇到了同样的错误。

在这种情况下如何设置共享权重?

mos*_*shi 8

我遇到了类似的问题,发现解决方案是先将图层添加到现有模型中,然后调用set_weights. 因此,对于您的示例,我建议将该线移到该线i2h.set_weights(word_vectors)之后self.model = Model(input=inputs, output=[h2out])