当前位置:   article > 正文

bert 的输出格式详解_bert的输出

bert的输出

输出是一个元组类型的数据 ,包含四部分,

last hidden state shape是(batch_size, sequence_length, hidden_size),hidden_size=768,它是模型最后一层的隐藏状态

pooler_output:shape是(batch_size, hidden_size),这是序列的第一个token (cls) 的最后一层的隐藏状态,它是由线性层和Tanh激活函数进一步处理的,这个输出不是对输入的语义内容的一个很好的总结,对于整个输入序列的隐藏状态序列的平均化或池化可以更好的表示一句话。


hidden_states:这是输出的一个可选项,如果输出,需要指定config.output_hidden_states=True,它是一个元组,含有13个元素,第一个元素可以当做是embedding,其余12个元素是各层隐藏状态的输出,每个元素的形状是(batch_size, sequence_length, hidden_size),


attentions:这也是输出的一个可选项,如果输出,需要指定config.output_attentions=True,它也是一个元组,含有12个元素,包含每的层注意力权重,用于计算self-attention heads的加权平均值

  1. import torch
  2. from torch import tensor
  3. from transformers import BertConfig, BertTokenizer, BertModel
  4. model_path = 'model/chinese-roberta-wwm-ext/'#已下载的预训练模型文件路径
  5. config = BertConfig.from_pretrained(model_path, output_hidden_states = True, output_attentions=True)
  6. assert config.output_hidden_states == True
  7. assert config.output_attentions == True
  8. model = BertModel.from_pretrained(model_path, config = config)
  9. tokenizer = BertTokenizer.from_pretrained(model_path)
  10. text = '我热爱这个世界'
  11. # input = tokenizer(text)
  12. # {'input_ids': [101, 2769, 4178, 4263, 6821, 702, 686, 4518, 102],
  13. #'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0],
  14. #'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1]}
  15. # input = tokenizer.encode(text)
  16. # [101, 2769, 4178, 4263, 6821, 702, 686, 4518, 102]
  17. # input = tokenizer.encode_plus(text)
  18. # {'input_ids': [101, 2769, 4178, 4263, 6821, 702, 686, 4518, 102],
  19. #'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0],
  20. #'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1]}
  21. input_ids = torch.tensor([tokenizer.encode(text)], dtype=torch.long)#一个输入也需要组batch
  22. print(input_ids.shape)
  23. #torch.Size([1, 9])
  24. model.eval()
  25. output = model(input_ids)
  26. print(len(output))
  27. print(output[0].shape) #最后一层的隐藏状态 (batch_size, sequence_length, hidden_size)
  28. print(output[1].shape) #第一个token即(cls)最后一层的隐藏状态 (batch_size, hidden_size)
  29. print(len(output[2])) #需要指定 output_hidden_states = True, 包含所有隐藏状态,第一个元素是embedding, 其余元素是各层的输出 (batch_size, sequence_length, hidden_size)
  30. print(len(output[3])) #需要指定output_attentions=True,包含每一层的注意力权重,用于计算self-attention heads的加权平均值(batch_size, layer_nums, sequence_length, sequence_legth)
  31. # 4
  32. # torch.Size([1, 9, 768])
  33. # torch.Size([1, 768])
  34. # 13
  35. # 12
  36. all_hidden_state = output[2]
  37. print(all_hidden_state[0].shape)
  38. print(all_hidden_state[1].shape)
  39. print(all_hidden_state[2].shape)
  40. # torch.Size([1, 9, 768])
  41. # torch.Size([1, 9, 768])
  42. # torch.Size([1, 9, 768])
  43. attentions = output[3]
  44. print(attentions[0].shape)
  45. print(attentions[1].shape)
  46. print(attentions[2].shape)
  47. # torch.Size([1, 12, 9, 9])
  48. # torch.Size([1, 12, 9, 9])
  49. # torch.Size([1, 12, 9, 9])

后续补充,

  1. text = '我热爱这个世界'
  2. input = tokenizer(text)
  3. #input分词后是一个字典
  4. # {'input_ids': [101, 2769, 4178, 4263, 6821, 702, 686, 4518, 102],
  5. #'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0],
  6. #'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1]}
  7. #input_ids = torch.tensor([tokenizer.encode(text)], dtype=torch.long)
  8. #一个输入也需要组batch
  9. input_ids = torch.tensor([input["input_ids"])
  10. token_type_ids = torch.tensor([input["token_type_ids"])
  11. attention_mask = torch.tensor([input["attention_mask"]]
  12. output = model(input_ids, token_type_ids, attention_mask)
  13. # 可以同时输入input_ids token_type_ids 和 attention_mask得到输出
  14. #另一种写法,直接在分词的过程中返回张量
  15. input_tensor = tokenizer(input, return_tensors = "pt")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/235692
推荐阅读
相关标签
  

闽ICP备14008679号