相关疑难解决方法(0)

如何在Keras模型中使用TensorFlow的采样softmax损失函数?

我正在Keras训练语言模型,并希望通过使用采样softmax作为我网络中的最终激活功能来加速训练.从TF文档中,看起来我需要为weights和提供参数biases,但我不确定这些的输入是什么.好像我可以在Keras中编写自定义函数,如下所示:

import keras.backend as K

def sampled_softmax(weights, biases, y_true, y_pred, num_sampled, num_classes):
    return K.sampled_softmax(weights, biases, y_true, y_pred, num_sampled, num_classes)
Run Code Online (Sandbox Code Playgroud)

但是,我不确定如何"插入"到我现有的网络.LM的架构非常简单:

model = Sequential()
model.add(Embedding(input_dim=len(vocab), output_dim=256))
model.add(LSTM(1024, return_sequence=True))
model.add(Dense(output_dim=len(vocab), activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')
Run Code Online (Sandbox Code Playgroud)

鉴于这种架构,我可以在模型上调用编译方法时将sampled_softmax函数作为loss参数传递吗?或者这需要写为最终完全连接层之后的层.这里的任何指导将不胜感激.谢谢.

deep-learning keras tensorflow loss-function

6
推荐指数
1
解决办法
1440
查看次数

标签 统计

deep-learning ×1

keras ×1

loss-function ×1

tensorflow ×1