Skip to content

Commit fb166e3

Browse files
committed
lint fix
1 parent 02641bf commit fb166e3

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

docs/hpc/08_ml_ai_hpc/03_llm_on_hpc.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ You can find more information about using Singularity and Conda on our HPC syste
7676
## Prepare script
7777
Create a python script using the following code from sections 1-9 and save it in a file called `huggingface.py`:
7878

79-
1. Import necessary modules:
79+
1. Import necessary modules:
8080
```python
8181
import torch
8282
import numpy as np
8383
from transformers import AutoTokenizer, AutoModel
8484
```
8585

86-
1. Create a list of reviews:
86+
1. Create a list of reviews:
8787
```python
8888
texts = ["How do I get a replacement Medicare card?",
8989
"What is the monthly premium for Medicare Part B?",
@@ -93,43 +93,43 @@ Create a python script using the following code from sections 1-9 and save it in
9393
"How do I sign up for Medicare Part B if I already have Part A?"]
9494
```
9595

96-
1. Choose the model name from huggingface’s model hub and instantiate the model and tokenizer object for the given model. We are setting `output_hidden_states` as `True` as we want the output of the model to not only have loss, but also the embeddings for the sentences.
96+
1. Choose the model name from huggingface’s model hub and instantiate the model and tokenizer object for the given model. We are setting `output_hidden_states` as `True` as we want the output of the model to not only have loss, but also the embeddings for the sentences.
9797
```python
9898
model_name = 'cardiffnlp/twitter-roberta-base-sentiment'
9999
tokenizer = AutoTokenizer.from_pretrained(model_name)
100100
model = AutoModel.from_pretrained(model_name, output_hidden_states=True)
101101
```
102102

103-
1. Create the ids to be used in the model using the tokenizer object. We set the return_tensors as “pt” as we want to return the pytorch tensor of the ids:
103+
1. Create the ids to be used in the model using the tokenizer object. We set the return_tensors as “pt” as we want to return the pytorch tensor of the ids:
104104
```python
105105
ids = tokenizer(texts, padding=True, return_tensors="pt")
106106
```
107107

108-
1. Set the device to cuda, and move the model and the tokenizer to cuda as well. Since, we will be extracting embeddings, we will only be performing a forward pass of the model and hence we will set the model to validation mode using `eval()`:
108+
1. Set the device to cuda, and move the model and the tokenizer to cuda as well. Since, we will be extracting embeddings, we will only be performing a forward pass of the model and hence we will set the model to validation mode using `eval()`:
109109
```python
110110
device = 'cuda' if torch.cuda.is_available() else 'cpu'
111111
model.to(device)
112112
ids = ids.to(device)
113113
model.eval()
114114
```
115115

116-
1. Performing the forward pass and storing the output tuple in out:
116+
1. Performing the forward pass and storing the output tuple in out:
117117
```python
118118
with torch.no_grad():
119119
out = model(**ids)
120120
```
121121

122-
1. Extracting the embeddings of each review from the last layer:
122+
1. Extracting the embeddings of each review from the last layer:
123123
```python
124124
last_hidden_states = out.last_hidden_state
125125
```
126126

127-
1. For the purpose of classification, we are extracting the CLS token which is the first embedding in the embedding list for each review:
127+
1. For the purpose of classification, we are extracting the CLS token which is the first embedding in the embedding list for each review:
128128
```python
129129
sentence_embedding = last_hidden_states[:, 0, :]
130130
```
131131

132-
1. We can check the shape of the final sentence embeddings for all the reviews. The output should look like `torch.Size([6, 768])`, where 6 is the batch size as we input 6 reviews as shown in step `2b`, and 768 is the embedding size of the RoBERTa model used.
132+
1. We can check the shape of the final sentence embeddings for all the reviews. The output should look like `torch.Size([6, 768])`, where 6 is the batch size as we input 6 reviews as shown in step `2b`, and 768 is the embedding size of the RoBERTa model used.
133133
```python
134134
print("Shape of the batch embedding: {}".format(sentence_embedding.shape))
135135
```

0 commit comments

Comments
 (0)