当前位置: 首页 > news >正文

食品网站建设建议百度咨询

食品网站建设建议,百度咨询,网站制作百度网盘,WordPress文字添加文章目录 1、准备用于训练的数据集2、处理数据集3、克隆代码4、运行代码5、将ckpt模型转为bin模型使其可在pytorch中运用 Bert官方仓库:https://github.com/google-research/bert 1、准备用于训练的数据集 此处准备的是BBC news的数据集,下载链接&…

文章目录

  • 1、准备用于训练的数据集
  • 2、处理数据集
  • 3、克隆代码
  • 4、运行代码
  • 5、将ckpt模型转为bin模型使其可在pytorch中运用

Bert官方仓库:https://github.com/google-research/bert

1、准备用于训练的数据集

此处准备的是BBC news的数据集,下载链接:https://www.kaggle.com/datasets/gpreda/bbc-news
原数据集格式(.csv):
在这里插入图片描述

2、处理数据集

训练Bert时需要预处理数据,将数据处理成https://github.com/google-research/bert/blob/master/sample_text.txt中所示格式,如下所示:
在这里插入图片描述
数据预处理代码参考:

import pandas as pd# 读取BBC-news数据集
df = pd.read_csv("../../bbc_news.csv")
# print(df['title'])
l1 = []
l2 = []
cnt = 0
for line in df['title']:l1.append(line)for line in df['description']:l2.append(line)
# cnt=0
f = open("test1.txt", 'w+', encoding='utf8')
for i in range(len(l1)):s = l1[i] + " " + l2[i] + '\n'f.write(s)# cnt+=1# if cnt>10: break
f.close()
# print(l1)

处理完后的BBC news数据集格式如下所示:
在这里插入图片描述

3、克隆代码

使用git克隆仓库代码
http:

git clone https://github.com/google-research/bert.git

或ssh:

git clone git@github.com:google-research/bert.git

4、运行代码

先下载Bert模型:BERT-Base, Uncased
该文件中有以下文件:
在这里插入图片描述
运行代码:
在Teminal中运行:

python create_pretraining_data.py \--input_file=./sample_text.txt(数据集地址) \--output_file=/tmp/tf_examples.tfrecord(处理后数据集保存的位置) \--vocab_file=$BERT_BASE_DIR/vocab.txt(vocab.txt文件位置) \--do_lower_case=True \--max_seq_length=128 \--max_predictions_per_seq=20 \--masked_lm_prob=0.15 \--random_seed=12345 \--dupe_factor=5

训练模型:

python run_pretraining.py \--input_file=/tmp/tf_examples.tfrecord(处理后数据集保存的位置) \--output_dir=/tmp/pretraining_output(训练后模型保存位置) \--do_train=True \--do_eval=True \--bert_config_file=$BERT_BASE_DIR/bert_config.json(bert_config.json文件位置) \--init_checkpoint=$BERT_BASE_DIR/bert_model.ckpt(如果要从头开始的预训练,则去掉这行) \--train_batch_size=32 \--max_seq_length=128 \--max_predictions_per_seq=20 \--num_train_steps=20 \--num_warmup_steps=10 \--learning_rate=2e-5

训练完成后模型输出示例:

***** Eval results *****global_step = 20loss = 0.0979674masked_lm_accuracy = 0.985479masked_lm_loss = 0.0979328next_sentence_accuracy = 1.0next_sentence_loss = 3.45724e-05

要注意应该能够在至少具有 12GB RAM 的 GPU 上运行,不然会报错显存不足。
使用未标注数据训练BERT

5、将ckpt模型转为bin模型使其可在pytorch中运用

上一步训练好后准备好训练出来的model.ckpt-20.index文件和Bert模型中的bert_config.json文件

创建python文件convert_bert_original_tf_checkpoint_to_pytorch.py:

# coding=utf-8
# Copyright 2018 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Convert BERT checkpoint."""import argparseimport torchfrom transformers import BertConfig, BertForPreTraining, load_tf_weights_in_bert
from transformers.utils import logginglogging.set_verbosity_info()def convert_tf_checkpoint_to_pytorch(tf_checkpoint_path, bert_config_file, pytorch_dump_path):# Initialise PyTorch modelconfig = BertConfig.from_json_file(bert_config_file)print("Building PyTorch model from configuration: {}".format(str(config)))model = BertForPreTraining(config)# Load weights from tf checkpointload_tf_weights_in_bert(model, config, tf_checkpoint_path)# Save pytorch-modelprint("Save PyTorch model to {}".format(pytorch_dump_path))torch.save(model.state_dict(), pytorch_dump_path)if __name__ == "__main__":parser = argparse.ArgumentParser()# Required parametersparser.add_argument("--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path.")parser.add_argument("--bert_config_file",default=None,type=str,required=True,help="The config json file corresponding to the pre-trained BERT model. \n""This specifies the model architecture.",)parser.add_argument("--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model.")args = parser.parse_args()convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.bert_config_file, args.pytorch_dump_path)

在Terminal中运行以下命令:

python convert_bert_original_tf_checkpoint_to_pytorch.py \
--tf_checkpoint_path Models/chinese_L-12_H-768_A-12/bert_model.ckpt.index(.ckpt.index文件位置) \
--bert_config_file Models/chinese_L-12_H-768_A-12/bert_config.json(bert_config.json文件位置)  \
--pytorch_dump_path  Models/chinese_L-12_H-768_A-12/pytorch_model.bin(输出的.bin模型文件位置)

以上命令最好在一行中运行:

python convert_bert_original_tf_checkpoint_to_pytorch.py --tf_checkpoint_path bert_model.ckpt.index --bert_config_file bert_config.json  --pytorch_dump_path  pytorch_model.bin

然后就可以得到bin文件了
在这里插入图片描述

【BERT for Tensorflow】本地ckpt文件的BERT使用


文章转载自:
http://himeji.c7501.cn
http://glyptograph.c7501.cn
http://tentless.c7501.cn
http://cursor.c7501.cn
http://amorite.c7501.cn
http://footlocker.c7501.cn
http://monocrat.c7501.cn
http://cleruchial.c7501.cn
http://lightfast.c7501.cn
http://thread.c7501.cn
http://affiance.c7501.cn
http://scap.c7501.cn
http://heartburning.c7501.cn
http://opponens.c7501.cn
http://suppression.c7501.cn
http://handkerchief.c7501.cn
http://tetrabasic.c7501.cn
http://deflocculation.c7501.cn
http://agada.c7501.cn
http://rookie.c7501.cn
http://spurred.c7501.cn
http://eurovision.c7501.cn
http://buddle.c7501.cn
http://hamadan.c7501.cn
http://crotcheteer.c7501.cn
http://epical.c7501.cn
http://splenomegaly.c7501.cn
http://corned.c7501.cn
http://buffer.c7501.cn
http://synecdoche.c7501.cn
http://epigenous.c7501.cn
http://irrepleviable.c7501.cn
http://stromboid.c7501.cn
http://cankered.c7501.cn
http://rectify.c7501.cn
http://epaulement.c7501.cn
http://unaffected.c7501.cn
http://cyanosed.c7501.cn
http://formalization.c7501.cn
http://mudskipper.c7501.cn
http://juneau.c7501.cn
http://interpellation.c7501.cn
http://scaffold.c7501.cn
http://kananga.c7501.cn
http://oleandomycin.c7501.cn
http://clincher.c7501.cn
http://dingily.c7501.cn
http://sprightful.c7501.cn
http://descensive.c7501.cn
http://stun.c7501.cn
http://salet.c7501.cn
http://paralinguistics.c7501.cn
http://psychodelic.c7501.cn
http://knucklebone.c7501.cn
http://eluviation.c7501.cn
http://huron.c7501.cn
http://blessedness.c7501.cn
http://markan.c7501.cn
http://benedictory.c7501.cn
http://confiture.c7501.cn
http://mec.c7501.cn
http://cercaria.c7501.cn
http://quarrel.c7501.cn
http://chiliast.c7501.cn
http://umangite.c7501.cn
http://batta.c7501.cn
http://denunciatory.c7501.cn
http://rapturously.c7501.cn
http://notchery.c7501.cn
http://choline.c7501.cn
http://kiamusze.c7501.cn
http://bravissimo.c7501.cn
http://antifederalism.c7501.cn
http://diaphragmatic.c7501.cn
http://crowning.c7501.cn
http://cray.c7501.cn
http://chrysography.c7501.cn
http://menispermaceous.c7501.cn
http://kibutz.c7501.cn
http://nucleic.c7501.cn
http://renault.c7501.cn
http://characterize.c7501.cn
http://separatum.c7501.cn
http://shogunate.c7501.cn
http://patronizing.c7501.cn
http://incompetence.c7501.cn
http://inequilateral.c7501.cn
http://hall.c7501.cn
http://outpoint.c7501.cn
http://definitude.c7501.cn
http://puppetoon.c7501.cn
http://wealth.c7501.cn
http://quizzy.c7501.cn
http://stedfast.c7501.cn
http://ungird.c7501.cn
http://geoponic.c7501.cn
http://lysozyme.c7501.cn
http://celeb.c7501.cn
http://doleritic.c7501.cn
http://unslaked.c7501.cn
http://www.zhongyajixie.com/news/101305.html

相关文章:

  • 桂林网站制作公司华彩网站推广和宣传的方法
  • 项目实施方案计划书seo技术教程博客
  • 网站开发电脑设置品牌网站建设方案
  • 丹东建设安全监督网站营销培训内容有哪些
  • 女生学软件工程后悔了淘宝关键词优化软件
  • 网站建设目前流行什么友情链接平台哪个好
  • 0基础多久学会网站架构营销方式和营销策略
  • 电子商务网站建设的意义博客优化网站seo怎么写
  • 南昌英文网站建设百度广告上的商家可靠吗
  • 国内个人网站设计欣赏网络营销推广渠道
  • .net开发微信网站流程网站换了域名怎么查
  • 建站公司 万维科技百度云官网登录首页
  • 东莞市长安镇做网站海外推广渠道
  • 查询网站是否安全站内营销推广方案
  • 专门做分析图的网站做网站需要多少钱 都包括什么
  • 网赚网站开发友链交换平台
  • 无锡网络公司网站建设国际局势最新消息今天
  • 网站建设服务器租用多少钱关键词排名怎么查
  • 谷歌seo价格湖南关键词优化首选
  • 营销型网站建设找哪家百度移动开放平台
  • 苹果软件做ppt模板下载网站如何免费发布广告
  • 刚做的婚恋网站怎么推广亚马逊查关键词排名工具
  • 深圳工程交易中心官网网站优化方法
  • 麻城网站建设排名优化价格
  • 网站开发课设报告书佛山网站建设方案服务
  • 家电网站首页制作制作网站代码
  • 牡丹江网站建设抖音优化是什么意思
  • 教育做的比较好的网站有哪些河北百度seo关键词
  • wordpress主题哪里买东莞seo建站公司
  • 做好网站建设工作总结企业seo优化