边学边记录,备忘
1. 安装包
!pip install package # 安装包
!pip install bert-tensorflow natural-questions --no-dependencies
!pip install tensorflow-gpu==1.14.0
2. !pip commit时报错,使用以下命令解决:
import subprocess
subprocess.run("pip install bert-tensorflow natural-questions --no-dependencies", shell=True, check=True)
subprocess.run("pip install tensorflow-gpu==1.14.0", shell=True, check=True)
3. keggle 生成的文件(只有文件名称) 存放的路径是:
with tf.io.gfile.GFile(“predictions.json”, "w") as f:
json.dump(predictions_json, f, indent=4)
生成的"predictions.json"文件 读取路径为:
"/kaggle/working/predictions.json"
提交的csv按照 名称 要求使用!
4、commit 时 生成文件 无法提交,问题: "Your Notebook cannot use internet access in this competition"
internet 为off才行
5. pytroch BERT 用法
self.bertModel = BertModel.from_pretrained(pretrained_weights, config=config) # , config=config
self.bertModel(input_ids=input_ids, attention_mask=input_mask,
token_type_ids=segment_ids)
input_ids: 输入 token Id
attention_mask: 掩码, 每句话的token标记为1, 填充字段标记为0;
token_type_ids: 分段标记, 第一个句子标记为0,第二个句子标记为1
[CLS] A B C [SEP] B C D[SEP]
0 0 0 0 0 1 1 1 1
6. pytroch 生成独热编码:
position_1 = torch.tensor([[2], [3], [1], [1]]) # 每上为1的位置
torch.zeros(4, 4).scatter_(1, position_1, 1)
tensor([[0., 0., 1., 0.],
[0., 0., 0., 1.],
[0., 1., 0., 0.],
[0., 1., 0., 0.]])
torch.zeros(4, 4).scatter_(dim, position_1, value)
dim: 指定纬度
position_1: 指定位置
value: 指定值
7. pytorch tensor 获取给定值 的 索引:
t = torch.Tensor([1, 2, 3])
print ((t == 2).nonzero())
Out[13]: tensor([[1]])
8. kaggle Commit时要求 Internet Off,这就导致 提交时 无法使用Internet, pip安装的包不能使用,所以commit时,必须Internet OFF, 需要的包 必须上传到 input文件中, 达到离线安装才行。【很蛋疼!】
重点:竞赛中Notebooks中 有很多大牛 发布的 公开代码!!! 一定要去看!!! 从中必有收获!!!【站在巨人肩膀上】
我模型中使用pytorch的transformers库,库离线安装, 看到notebook中有人公布的解决办法: https://www.kaggle.com/axel81/pytorch-bert-baseline/data#Build-Model
将transformers库离线安装包 上传到 Datas中,
当然 有很多大牛 已经上传过了, 找到 Add进去即可。
Pytroch BERT 预训练模型已经上传的库:
Pretrained BERT Models for PyTorch
transformers
sacremoses-master
将库添加之后,(相当于)本地安装
import subprocess
subprocess.run("pip install ../input/sacremoses/sacremoses-master/", shell=True, check=True)
subprocess.run("pip install ../input/transformers/transformers-master/", shell=True, check=True)
subprocess.run("ls ../input/pretrained-bert-models-for-pytorch/bert-base-uncased", shell=True, check=True)
subprocess.run("ls ../input/pretrained-bert-models-for-pytorch/bert-large-uncased", shell=True, check=True)
from transformers import BertModel, BertConfig, BertPreTrainedModel, BertTokenizer, AdamW
模型加载:
bert_large_model = 'bert-large-uncased'
do_lower_case = 'uncased' in bert_large_model
bert_large_model_config = '../input/pretrained-bert-models-for-pytorch/bert-large-uncased/bert_config.json'
tokenizer_bert_large_uncased_vocab = "../input/pretrained-bert-models-for-pytorch/bert-large-uncased-vocab.txt"
model_large_path = "../input/pretrained-bert-models-for-pytorch/bert-large-uncased/"
pretrained_weights = "bert-large-uncased"
# config = BertConfig.from_pretrained(pretrained_weights)
config = BertConfig.from_json_file(bert_large_model_config)
tokenizer = BertTokenizer.from_pretrained(tokenizer_bert_large_uncased_vocab, do_lower_case=do_lower_case)
print("BERT 模型参数 加载完毕!")
克服困难,迎难而上!