python - 额外训练 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
我建立了一个层,需要训练,如何以正确的方式进行?(了解使用哪个优化器和损失函数尤为重要)
解决方案
如果您在卡门培尔奶酪上构建了一个层,即您正在尝试对其进行微调,则可以遵循微调的标准做法,即冻结预训练的大型模型并仅优化构建在其之上的层。我相信对于初学者来说,在它之上的MLP会做得很好。
如果正确(我相信这是一个分类任务),您可以使用交叉熵损失函数和亚当优化器
推荐阅读
- ruby - 在电子邮件正文中发送重音字符,如何解决编码问题
- python - 了解熊猫数据框中的 for 循环
- php - 如果当前日期在日期之间,WP_Query 获取数据
- reactjs - 当我需要将引用传递给另一个组件时,如何确保加载它?
- r - 在数据框中找到最小转折点
- java - 如果输入错误,请重新输入
- python - 在确定的 wx.StatusBar 的字段中显示 wx.MenuItems 的帮助字符串
- eiffel - 在所有断言都设置为 true 的情况下如何不进行检查?
- functional-programming - Curry 中的 N 元函数和 Prolog 中的 N+1 元关系之间有什么区别吗?
- python - 如何将字典术语转换为熊猫数据框?