transformers icon indicating copy to clipboard operation
transformers copied to clipboard

BERT returns different embedding for same sentence

Open rshah1990 opened this issue 7 years ago • 2 comments

I am using pre-trained BERT for creating features, for same sentence it produces different result in two different runs. Do we have to set some random state to produce consistent result? I am using pytorch-transformers for reading pre-trained model.

rshah1990 avatar Sep 16 '19 11:09 rshah1990

Are you initializing from a pretrained model? If no, than this is normal behaviour: your weights are randomly initialized. If yes, make sure your model is in evaluation mode (model.eval()), this disables dropout and other random modules.

srslynow avatar Sep 16 '19 14:09 srslynow

Thank you for quick response @srslynow . how to initialize weights and biases fora pre-trained model ? I thought weights and biases freezes after training.

you were right , I missed the model.eval() that's the reason I was getting slightly different embedding on each run becz of dropout layer.

rshah1990 avatar Sep 16 '19 16:09 rshah1990