我在 GPU 上使用 pickle 保存了 Bert 的最后一个隐藏层,以供后续流程使用。
# output is the last hidden layer of bert, transformed on GPU
with open(filename, 'wb') as f:
pk.dump(output, f)
Run Code Online (Sandbox Code Playgroud)
是否可以在没有 GPU 的情况下将其加载到我的个人笔记本电脑上?我尝试了下面的代码,但都失败了。
# 1st try
with open(filename, 'rb') as f:
torch.load(f, map_location='cpu')
# 2nd
torch.load(filename, map_location=torch.device('cpu'))
Run Code Online (Sandbox Code Playgroud)
全部出现以下错误
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.
Run Code Online (Sandbox Code Playgroud)
可以将文件加载到我的笔记本电脑上吗?
我想通过使用 transform.BertTokenizer 对多个句子进行编码来创建一个小批量。它似乎适用于单个句子。如何使它适用于几个句子?
from transformers import BertTokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
# tokenize a single sentence seems working
tokenizer.encode('this is the first sentence')
>>> [2023, 2003, 1996, 2034, 6251]
# tokenize two sentences
tokenizer.encode(['this is the first sentence', 'another setence'])
>>> [100, 100] # expecting 7 tokens
Run Code Online (Sandbox Code Playgroud) word-embedding huggingface-transformers huggingface-tokenizers
我想将 tmux 的前缀键从 Cb 更改为仅 Ctrl,并尝试在 .tmux.conf 中执行以下操作。但这不起作用。
unbind C-b
set -g prefix C
bind C send-prefix
Run Code Online (Sandbox Code Playgroud)
谢谢
我想看看是否numpy.random.exponential使用F ^ { - 1}(U)方法实现,其中F是指数分布的cdf,U是均匀分布.
我试过了numpy.source(random.exponential),但是回复了' 不适用于这个对象'.这是不是意味着这个函数不是用Python编写的?
我也试过了inspect.getsource(random.exponential),但是回复了一个错误,说它不是模块,功能等.
我很好奇transformers.BertModel 的内存使用情况。我想使用预训练模型来转换文本并保存标记 [CLS] 的输出。没有训练,只有推理。
我对 bert 的输入是 511 个令牌。由于批量大小为 16,我的代码内存不足。GPU 有 32GB 内存。我的问题是如何估计Bert的内存使用量。
奇怪的是,批处理大小为 32 的另一个作业成功完成,设置相同。我的代码如下。
# Create dataloader
bs = 16
train_comb = ConcatDataset([train_data, valid_data])
train_dl = DataLoader(train_comb, sampler=RandomSampler(train_data), batch_size=bs)
model = BertModel.from_pretrained('/my_dir/bert_base_uncased/',
output_attentions=False,
output_hidden_states=False)
model.cuda()
out_list = []
model.eval()
with torch.no_grad():
for d in train_dl:
d = [i.cuda() for i in d]. # d = [input_ids, attention_mask, token_type_ids, labels]
inputs, labels = d[:3], d[3] # input_ids has shape 16 x 511
output = model(*inputs)[0][:, 0, :]
out_list.append(output)
outputs …Run Code Online (Sandbox Code Playgroud)