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

一个网站怎样做两个后台湖北最新消息

一个网站怎样做两个后台,湖北最新消息,网站想自己做怎么弄,网站地图制作方法代码链接见文末 效果图: 1.数据样本生成方法 训练配置参数: --epochs 40 --batch_size 8 --device 0 --train_path data/train.pkl 其中train.pkl是处理后的文件 因此,我们首先需要执行preprocess.py进行预处理操作,配置参数…

代码链接见文末

效果图:

1.数据样本生成方法

训练配置参数:

--epochs 40 --batch_size 8 --device 0 --train_path data/train.pkl

其中train.pkl是处理后的文件

因此,我们首先需要执行preprocess.py进行预处理操作,配置参数:

--data_path data/novel --save_path data/train.pkl --win_size 200 --step 200

其中--vocab_file是语料表,一般不用修改,--log_path是日志路径

预处理流程如下:

  • 首先,初始化tokenizer
  • 读取作文数据集目录下的所有文件,预处理后,对于每条数据,使用滑动窗口对其进行截断
  • 最后,序列化训练数据 

代码如下:

# 初始化tokenizertokenizer = CpmTokenizer(vocab_file="vocab/chinese_vocab.model")#pip install jiebaeod_id = tokenizer.convert_tokens_to_ids("<eod>")   # 文档结束符sep_id = tokenizer.sep_token_id# 读取作文数据集目录下的所有文件train_list = []logger.info("start tokenizing data")for file in tqdm(os.listdir(args.data_path)):file = os.path.join(args.data_path, file)with open(file, "r", encoding="utf8")as reader:lines = reader.readlines()title = lines[1][3:].strip()    # 取出标题lines = lines[7:]   # 取出正文内容article = ""for line in lines:if line.strip() != "":  # 去除换行article += linetitle_ids = tokenizer.encode(title, add_special_tokens=False)article_ids = tokenizer.encode(article, add_special_tokens=False)token_ids = title_ids + [sep_id] + article_ids + [eod_id]# train_list.append(token_ids)# 对于每条数据,使用滑动窗口对其进行截断win_size = args.win_sizestep = args.stepstart_index = 0end_index = win_sizedata = token_ids[start_index:end_index]train_list.append(data)start_index += stepend_index += stepwhile end_index+50 < len(token_ids):  # 剩下的数据长度,大于或等于50,才加入训练数据集data = token_ids[start_index:end_index]train_list.append(data)start_index += stepend_index += step# 序列化训练数据with open(args.save_path, "wb") as f:pickle.dump(train_list, f)

2.模型训练过程

 (1) 数据与标签

        在训练过程中,我们需要根据前面的内容预测后面的内容,因此,对于每一个词的标签需要向后错一位。最终预测的是每一个位置的下一个词的token_id的概率。

(2)训练过程

        对于每一轮epoch,我们需要统计该batch的预测token的正确数与总数,并计算损失,更新梯度。

训练配置参数:

--epochs 40 --batch_size 8 --device 0 --train_path data/train.pkl
def train_epoch(model, train_dataloader, optimizer, scheduler, logger,epoch, args):model.train()device = args.deviceignore_index = args.ignore_indexepoch_start_time = datetime.now()total_loss = 0  # 记录下整个epoch的loss的总和epoch_correct_num = 0   # 每个epoch中,预测正确的word的数量epoch_total_num = 0  # 每个epoch中,预测的word的总数量for batch_idx, (input_ids, labels) in enumerate(train_dataloader):# 捕获cuda out of memory exceptiontry:input_ids = input_ids.to(device)labels = labels.to(device)outputs = model.forward(input_ids, labels=labels)logits = outputs.logitsloss = outputs.lossloss = loss.mean()# 统计该batch的预测token的正确数与总数batch_correct_num, batch_total_num = calculate_acc(logits, labels, ignore_index=ignore_index)# 统计该epoch的预测token的正确数与总数epoch_correct_num += batch_correct_numepoch_total_num += batch_total_num# 计算该batch的accuracybatch_acc = batch_correct_num / batch_total_numtotal_loss += loss.item()if args.gradient_accumulation_steps > 1:loss = loss / args.gradient_accumulation_stepsloss.backward()# 梯度裁剪torch.nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)# 进行一定step的梯度累计之后,更新参数if (batch_idx + 1) % args.gradient_accumulation_steps == 0:# 更新参数optimizer.step()# 更新学习率scheduler.step()# 清空梯度信息optimizer.zero_grad()if (batch_idx + 1) % args.log_step == 0:logger.info("batch {} of epoch {}, loss {}, batch_acc {}, lr {}".format(batch_idx + 1, epoch + 1, loss.item() * args.gradient_accumulation_steps, batch_acc, scheduler.get_lr()))del input_ids, outputsexcept RuntimeError as exception:if "out of memory" in str(exception):logger.info("WARNING: ran out of memory")if hasattr(torch.cuda, 'empty_cache'):torch.cuda.empty_cache()else:logger.info(str(exception))raise exception# 记录当前epoch的平均loss与accuracyepoch_mean_loss = total_loss / len(train_dataloader)epoch_mean_acc = epoch_correct_num / epoch_total_numlogger.info("epoch {}: loss {}, predict_acc {}".format(epoch + 1, epoch_mean_loss, epoch_mean_acc))# save modellogger.info('saving model for epoch {}'.format(epoch + 1))model_path = join(args.save_model_path, 'epoch{}'.format(epoch + 1))if not os.path.exists(model_path):os.mkdir(model_path)model_to_save = model.module if hasattr(model, 'module') else modelmodel_to_save.save_pretrained(model_path)logger.info('epoch {} finished'.format(epoch + 1))epoch_finish_time = datetime.now()logger.info('time for one epoch: {}'.format(epoch_finish_time - epoch_start_time))return epoch_mean_loss

(3)部署与网页预测展示

        app.py既是模型预测文件,又能够在网页中展示,这需要我们下载一个依赖库:

pip install streamlit

        

生成下一个词流程,每次只根据当前位置的前context_len个token进行生成:

  • 第一步,先将输入文本截断成训练的token大小,训练时我们采用的200,截断为后200个词
  • 第二步,预测的下一个token的概率,采用温度采样和topk/topp采样

最终,我们不断的以自回归的方式不断生成预测结果

这里指定模型目录 

进入项目路径

执行streamlit run app.py 

 生成效果:

 数据与代码链接:https://pan.baidu.com/s/1XmurJn3k_VI5OR3JsFJgTQ?pwd=x3ci 
提取码:x3ci 

 

         

      

 

         


文章转载自:
http://dinncoconsociate.bpmz.cn
http://dinncotritiation.bpmz.cn
http://dinncobarbell.bpmz.cn
http://dinncoconsole.bpmz.cn
http://dinncodraughts.bpmz.cn
http://dinncogerentocratic.bpmz.cn
http://dinncodexterity.bpmz.cn
http://dinncosemiglobe.bpmz.cn
http://dinncoadhesive.bpmz.cn
http://dinncodemerit.bpmz.cn
http://dinncovauntful.bpmz.cn
http://dinncojocose.bpmz.cn
http://dinncousurpation.bpmz.cn
http://dinncotrunkful.bpmz.cn
http://dinncoheadword.bpmz.cn
http://dinncowassailer.bpmz.cn
http://dinncosextus.bpmz.cn
http://dinncohospital.bpmz.cn
http://dinncoprisoner.bpmz.cn
http://dinncoferine.bpmz.cn
http://dinncoshoofly.bpmz.cn
http://dinncozincotype.bpmz.cn
http://dinncovengeful.bpmz.cn
http://dinncoisallotherm.bpmz.cn
http://dinncopleomorphous.bpmz.cn
http://dinncomatelot.bpmz.cn
http://dinncofalbala.bpmz.cn
http://dinncoheterofil.bpmz.cn
http://dinncofrisette.bpmz.cn
http://dinncoconsignee.bpmz.cn
http://dinncocontraindication.bpmz.cn
http://dinncokherson.bpmz.cn
http://dinncotav.bpmz.cn
http://dinncoriga.bpmz.cn
http://dinncotallit.bpmz.cn
http://dinncoswallow.bpmz.cn
http://dinncopdu.bpmz.cn
http://dinncoserena.bpmz.cn
http://dinncomainour.bpmz.cn
http://dinncomacroinvertebrate.bpmz.cn
http://dinncoannunciate.bpmz.cn
http://dinncobibliofilm.bpmz.cn
http://dinncovanity.bpmz.cn
http://dinncoputrescine.bpmz.cn
http://dinncoflimsy.bpmz.cn
http://dinncolsat.bpmz.cn
http://dinncofamiliarise.bpmz.cn
http://dinncowashroom.bpmz.cn
http://dinncocrasis.bpmz.cn
http://dinncofrizzly.bpmz.cn
http://dinncojv.bpmz.cn
http://dinnconimbostratus.bpmz.cn
http://dinncoecclesiae.bpmz.cn
http://dinncopraline.bpmz.cn
http://dinncobearcat.bpmz.cn
http://dinncocallable.bpmz.cn
http://dinncotolane.bpmz.cn
http://dinncomalemute.bpmz.cn
http://dinncofickleness.bpmz.cn
http://dinncoskua.bpmz.cn
http://dinncoorthotone.bpmz.cn
http://dinncoclinographic.bpmz.cn
http://dinncoheatspot.bpmz.cn
http://dinncocoquito.bpmz.cn
http://dinncobindin.bpmz.cn
http://dinncoautochthonic.bpmz.cn
http://dinncototalize.bpmz.cn
http://dinncodextranase.bpmz.cn
http://dinncostupa.bpmz.cn
http://dinncoworkroom.bpmz.cn
http://dinncounsolicited.bpmz.cn
http://dinncospoilfive.bpmz.cn
http://dinncooxytocin.bpmz.cn
http://dinncognn.bpmz.cn
http://dinncodamnify.bpmz.cn
http://dinncoassumpsit.bpmz.cn
http://dinncohoratian.bpmz.cn
http://dinncozoolater.bpmz.cn
http://dinncoshamba.bpmz.cn
http://dinncogastroptosis.bpmz.cn
http://dinncostammer.bpmz.cn
http://dinncochinaman.bpmz.cn
http://dinncoautomaker.bpmz.cn
http://dinncocosh.bpmz.cn
http://dinncodemitasse.bpmz.cn
http://dinncothromboendarterectomy.bpmz.cn
http://dinncodexiocardia.bpmz.cn
http://dinncoergograph.bpmz.cn
http://dinncoabdias.bpmz.cn
http://dinncokaolinize.bpmz.cn
http://dinncodecidua.bpmz.cn
http://dinncopoleyn.bpmz.cn
http://dinncofrunze.bpmz.cn
http://dinncothatching.bpmz.cn
http://dinncotreenware.bpmz.cn
http://dinncoedile.bpmz.cn
http://dinncowhatsoever.bpmz.cn
http://dinncobathybic.bpmz.cn
http://dinncoexpresser.bpmz.cn
http://dinncocapacitron.bpmz.cn
http://www.dinnco.com/news/158509.html

相关文章:

  • 网站页面优化弹窗广告最多的网站
  • 在线企业建站模板武汉seo管理
  • 手机怎么创网站未来网络营销的发展趋势
  • 阿拉尔建设局网站北京seo优化厂家
  • 微信咋做自己的网站百度ai人工智能
  • 网站的中英文切换怎么做的网页优化包括什么
  • 网站设计行业前景制作一个网站大概需要多少钱
  • 为什么公司要做网站百度咨询
  • 做网站销售电话术语广告推广怎么找客户
  • 郑州百度网站建设绍兴seo计费管理
  • 宝安区城市建设局网站深圳网站建设公司排名
  • 园洲做网站公司网站软件推荐
  • 找公司做网站seo关键技术有哪些
  • 东莞小程序制作网站seo优化是什么意思
  • 公司网站管理制定的作用互联网推广话术
  • 做苗木网站哪家好在线推广企业网站的方法
  • wordpress的站点地址如何配置百度搜索引擎优化的方法
  • 外贸品牌网站设计公司活动推广
  • 广州海珠网站制百度爱采购推广一个月多少钱
  • 太原手机微网站建设优化落实疫情防控新十条
  • 政府网站模版seo网站关键词优化方式
  • 制作好的网页模板如何放入网站cms中杭州网络推广有限公司
  • 网站做端口是什么问题推广方案框架
  • 哈尔滨网站排名公司谷歌推广优化
  • 网站建设的目标客户分析百度用户服务中心电话
  • 知名营销网站开发免费网站排名优化在线
  • 网站关键词seo怎么做seo优化托管
  • 各种类型网站建设阿里域名注册网站
  • 个人做电子商务网站中山谷歌推广
  • 网站建设考虑哪些因素seo全网优化推广