From 8131406d83acad62de49ad51f219b3d2dba562d8 Mon Sep 17 00:00:00 2001 From: zhang Date: Sun, 3 Jul 2022 12:49:17 +0800 Subject: torch.no_grad vs. requires_grad --- fine_tune/bert/tutorials/03_bert_input_embedding.py | 20 ++++++++++++++++++++ .../bert/tutorials/samples/BERT-embeddings-2.png | Bin 0 -> 23064 bytes 2 files changed, 20 insertions(+) create mode 100644 fine_tune/bert/tutorials/03_bert_input_embedding.py create mode 100644 fine_tune/bert/tutorials/samples/BERT-embeddings-2.png (limited to 'fine_tune/bert') diff --git a/fine_tune/bert/tutorials/03_bert_input_embedding.py b/fine_tune/bert/tutorials/03_bert_input_embedding.py new file mode 100644 index 0000000..41bdd29 --- /dev/null +++ b/fine_tune/bert/tutorials/03_bert_input_embedding.py @@ -0,0 +1,20 @@ + +from transformers import BertTokenizer, BertModel +from transformers.models.bert import BertModel +import torch +from torch import nn + + +model_name = 'bert-base-uncased' + +tokenizer = BertTokenizer.from_pretrained(model_name) +model = BertModel.from_pretrained(model_name, output_hidden_states=True) + +test_sent = 'this is a test sentence' + +model_input = tokenizer(test_sent, return_tensors='pt') + + +model.eval() +with torch.no_grad(): + output = model(**model_input) diff --git a/fine_tune/bert/tutorials/samples/BERT-embeddings-2.png b/fine_tune/bert/tutorials/samples/BERT-embeddings-2.png new file mode 100644 index 0000000..9b7072e Binary files /dev/null and b/fine_tune/bert/tutorials/samples/BERT-embeddings-2.png differ -- cgit v1.2.3