以下のコードをgoogle colab pro+にて動かしているのですが  
sentence_vectors = model.encode(sentences)  
のところで"使用可能な RAM をすべて使用した後で、セッションがクラッシュしました。"

どうすれば実行できるようになるのでしょうか？

nega.csvは一列目に文章を格納しています(約10000)

```

    !pip install -q transformers==4.7.0 fugashi ipadic
    
```

```

    from transformers import BertJapaneseTokenizer, BertModel
    import torch
    
    
    class SentenceBertJapanese:
        def __init__(self, model_name_or_path, device=None):
            self.tokenizer = BertJapaneseTokenizer.from_pretrained(model_name_or_path)
            self.model = BertModel.from_pretrained(model_name_or_path)
            self.model.eval()
    
            if device is None:
                device = "cuda" if torch.cuda.is_available() else "cpu"
            self.device = torch.device(device)
            self.model.to(device)
    
        def _mean_pooling(self, model_output, attention_mask):
            token_embeddings = model_output[0] #First element of model_output contains all token embeddings
            input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
            return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
    
    
        def encode(self, sentences, batch_size=8):
            all_embeddings = []
            iterator = range(0, len(sentences), batch_size)
            for batch_idx in iterator:
                batch = sentences[batch_idx:batch_idx + batch_size]
    
                encoded_input = self.tokenizer.batch_encode_plus(batch, padding="longest", 
                                               truncation=True, return_tensors="pt").to(self.device)
                model_output = self.model(**encoded_input)
                sentence_embeddings = self._mean_pooling(model_output, encoded_input["attention_mask"]).to('cpu')
    
                all_embeddings.extend(sentence_embeddings)
    
            # return torch.stack(all_embeddings).numpy()
            return torch.stack(all_embeddings)
    
```

```

    model = SentenceBertJapanese("sonoisa/sentence-bert-base-ja-mean-tokens-v2")
    
```

```

    import pandas as pd
    
```

```

    sentences = pd.read_csv('nega.csv').values.flatten().tolist()
    
```

```

    sentence_vectors = model.encode(sentences)
    
```

```

    import scipy.spatial
    
    queries =[]
    query_embeddings = model.encode(queries).numpy()
    
    closest_n = 5
    for query, query_embedding in zip(queries, query_embeddings):
        distances = scipy.spatial.distance.cdist([query_embedding], sentence_vectors, metric="cosine")[0]
    
        results = zip(range(len(distances)), distances)
        results = sorted(results, key=lambda x: x[1])
    
        print("\n\n======================\n\n")
        print("Query:", query)
        print("\nTop 5 most similar sentences in corpus:")
    
        for idx, distance in results[0:closest_n]:
            print(sentences[idx].strip(), "(Score: %.4f)" % (distance / 2))
    
```

```

    from psutil import virtual_memory
    ram_gb = virtual_memory().total / 1e9
    print('Your runtime has {:.1f} gigabytes of available RAM\n'.format(ram_gb))
    
    if ram_gb < 20:
        print('Not using a high-RAM runtime')
    else:
        print('You are using a high-RAM runtime!')
    
```

実行結果

```

    Your runtime has 54.8 gigabytes of available RAM
    
    You are using a high-RAM runtime!
    
```
CC BY-SA 4.0
2022-09-13T14:40:26.973
91085
2022-09-13T14:40:26.973
54381
question
google-colaboratory
Google colab pro+を使用しているがセッションがクラッシュしてしまう。