首页 > 解决方案 > 额外训练 NLP

问题描述

我开始使用 Camembert 深度学习模型,这是法语的 Roberta 的类似物,我有一个问题,如何为特定任务重新训练这样的模型?具体来说,任务是让模型学习如何评估输入句子的正确性

class newModel(nn.Module):
    def __init__(self, numFeatures=768):
        super(newModel, self).__init__()
        self.camembert = CamembertModel.from_pretrained('camembert-base')
        self.GAP = nn.AdaptiveAvgPool2d((1, numFeatures))
        self.predictionLayer = nn.Linear(numFeatures, 2)
        self.softmax = nn.Softmax()

    def forward(self, x):
        camembertFeatures = self.camembert(**tokenized_sentence) # [BS, inputShape] -> [BS, numToken, numFeatures]
        camembertFeatures = camembertFeatures[0]
        GAPvalues = self.GAP(camembertFeatures) # [BS, numToken, numFeatures] -> [BS, 1, numFeatures]
        GAPshape = GAPvalues.shape
        sentenceFeatures = GAPvalues.view(GAPshape[0], GAPshape[2]) # [BS, 1, numFeatures] -> [BS, numFeatures]
        predictions = self.predictionLayer(sentenceFeatures) # [BS, numFeatures] -> [BS, 2]
        predictions = self.softmax(predictions) # [BS, 2] -> [BS, 2]
        return predictions

我建立了一个层,需要训练,如何以正确的方式进行?(了解使用哪个优化器和损失函数尤为重要)

标签: pythonnlp

解决方案


如果您在卡门培尔奶酪上构建了一个层,即您正在尝试对其进行微调,则可以遵循微调的标准做法,即冻结预训练的大型模型并仅优化构建在其之上的层。我相信对于初学者来说,在它之上的MLP会做得很好。

如果正确(我相信这是一个分类任务),您可以使用交叉熵损失函数和亚当优化器


推荐阅读