Question: Can I get help modifying this code so I can pass the perplexity check, sanity check, and casual masking. Also, i had an error in
Can I get help modifying this code so I can pass the perplexity check, sanity check, and casual masking. Also, i had an error in the training loop when trying to calculate the output for the loss function.
code:
def trainlmargs traintext, devtext, vocabindex:
:param args: commandline args, passed through here for your convenience
:param traintext: train text as a sequence of characters
:param devtext: dev text as a sequence of characters
:param vocabindex: an Indexer of the character vocabulary characters
:return: a NeuralLanguageModel instance trained on the given data
printtraining text length:" lentraintext
# Set default values for missing args if necessary
lr getattrargslr
epochs getattrargs 'epochs',
batchsize getattrargs 'batchsize',
seqlen getattrargs 'seqlen',
model TransformerModelvocabsizelenvocabindex #this is necesssary to call the transfer from pytorch
# Set up optimizer and loss function
optimizer torch.optim.Adammodelparameters lrlr
scheduler torch.optim.lrscheduler.ReduceLROnPlateauoptimizer 'min', patience
lossfunction nnCrossEntropyLoss
# Convert training text to indices
traininds torch.tensorvocabindex.indexofc for c in traintext dtype torch.long
#training loop
for epoch in rangeepochs:
model.train
totalloss
#numbatches lentraintext batchsize
# Train over batches of characters from the training data size args.batchsize
for i in range lentraininds seqlen, batchsize:
# Ensure the batch size fits within the available training data
if i batchsize seqlen lentraininds:
break # Skip the last incomplete batch
# Get the input context and target next character for each batch Prepare input and target tensors
batchinput trainindsi:i batchsize seqlen
#viewbatchsize, seqlen# batchsize seqlen
batchtarget trainindsi :i batchsize seqlen
#viewbatchsize, seqlen# seqlen,
# printBatchsize: batchsize
# printBatchinput: batchinput
# printBatchtarget: batchtarget
# Ensure batch sizes are consistent
if batchinput.size batchsize or batchtarget.size batchsize:
continue # Skip incomplete batches
# Reshape the batches into the correct format
batchinput batchinput.viewbatchsize, seqlen
batchtarget batchtarget.viewbatchsize, seqlen
# if lenbatchinput seqlen:
# padsize seqlen lenbatchinput
# batchinput torch.nnfunctional.padbatchinput, padsize 'constant',
optimizer.zerograd
#forward pass trough the model
output modelbatchinput # Shape: batchsize seqlen, vocabsize
# Calculate the loss comparing model output to batchtarget
loss lossfunctionoutputreshape lenvocabindex batchtarget.reshape
loss.backward
optimizer.step
totalloss loss.item
printf Epoch epoch epochs loss:totalloss lentraintext
scheduler.steptotalloss lentraintext
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
