keras add external trainable variable to graph

后端 未结 1 850
野的像风
野的像风 2021-01-13 03:54

I am working on language modelling and the vocabulary is large. So I want to use sampled_softmax_loss from tensorflow. The problem is that weights and

相关标签:
1条回答
  • 2021-01-13 04:23

    I have finally found a workaround

    Let's say we need to train weights W and biases b with our model.

    So the workaround is just add them to one of the trainable layers of our model.

    model.layers[-1].trainable_weights.extend([W, b])
    

    When we can compile the model

    model.compile(...)
    

    It is extremely important to add variables to trainable layer, for example I've experimented with Sequential model, and adding [W, b] to the Activation layer does not make them actually trainable.

    0 讨论(0)
提交回复
热议问题