-
I'm facing another problem while testing my fine tuned model. I trained bert tokenizer and transformer model with python, and saved the model with these files.
and torch jit sentiment_model_jit.pt by tracing with following python code.
In my go code, I tried to load my trained models by following code.
and tried to forward the sentence with this code
I tested this code in a sample of 100 text (I can't scale this up further since the memory leaking is still happening... have to work on later...) , and hit by an unexpected result. The model does not effectively sorting the text into three labels. Every time I run the code, all of the texts classified into single label, does not show consistent probability, neither consistent labels (it sometimes classified all sample texts into long, sometimes into neutral, sometimes into short, for example). I'm thinking that I need to load additional configuration, but cannot get any hint forward. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The model should be created before you can load pretrained weights . As having BERT model constructed, you have more options to load pretrained weights.
import("github.com/sugarme/gotch/pickle")
device := gotch.CPU // or device := gotch.CudaIfAvailable() if using CUDA
vs := nn.NewVarStore(device)
err = pickle.LoadAll(vs, modelFile)
if err != nil {
log.Fatalf("Load model weight error: \n%v", err)
}
Hope that helps. |
Beta Was this translation helpful? Give feedback.
-
Hi @luxiant , The error means there was either an issue mapping named tensors from your model and your weights or the I often print out Python and Go variable names to compare. import torch
from kobert.pytorch_kobert import get_pytorch_kobert_model
model, vocab = get_pytorch_kobert_model()
for key, value in model.state_dict().items():
print(f"{key} - {value.shape}") Also, you can print out your Python pickled weight names and shapes from Go by using err := pickle.LoadInfo(modelFile)
if err != nil {
log.Fatal(err)
} device := gotch.CPU
vs := nn.NewVarStore(device)
path := vs.Root()
model = bert.NewBertForSequenceClassification(path, config, false)
// print out model infor
vs.Summary() If Python weights (names and shapes) contain Go weights (names and shapes), then you are safe to load weights partially: numMissingVariables, err := vs.LoadPartial(modelFile)
if err != nil{
panic(err)
} I can't find my time to get an older Pytorch version as required by |
Beta Was this translation helpful? Give feedback.
Hi @luxiant ,
The error means there was either an issue mapping named tensors from your model and your weights or the
gotch/pickle
can not handle some pickled weights.I often print out Python and Go variable names to compare.
Also, you can print out your Python pickled weight names and shapes from Go by using
gotch/pickle
(if it can handle it) as following: