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

网站做二级域名干什么用分公司注册流程网上注册

网站做二级域名干什么用,分公司注册流程网上注册,信誉好的广州外贸网站,wordpress好用吗系列文章 【如何训练一个中英翻译模型】LSTM机器翻译seq2seq字符编码#xff08;一#xff09; 【如何训练一个中英翻译模型】LSTM机器翻译模型训练与保存#xff08;二#xff09; 【如何训练一个中英翻译模型】LSTM机器翻译模型部署#xff08;三#xff09; 【如何训练…系列文章 【如何训练一个中英翻译模型】LSTM机器翻译seq2seq字符编码一 【如何训练一个中英翻译模型】LSTM机器翻译模型训练与保存二 【如何训练一个中英翻译模型】LSTM机器翻译模型部署三 【如何训练一个中英翻译模型】LSTM机器翻译模型部署之onnxpython四 目录 一、事情准备二、模型转换三、ncnn模型加载与推理python版 一、事情准备 这篇是在【如何训练一个中译英翻译器】LSTM机器翻译模型部署之onnxpython四的基础上进行的要用到文件为 input_words.txt target_words.txt config.json encoder_model-sim.onnx decoder_model-sim.onnx 其中的onnx就是用来转为ncnn模型的这里借助了onnx这个中间商所以前面我们需要先通过onnxsim对模型进行simplify要不然在模型转换时会出现op不支持的情况模型转换不仅有中间商这个例子目前还可以通过pnnx直接将pytorch模型转为ncnn感兴趣的小伙伴可以去折腾下 老规矩先给出工具 onnx2ncnnhttps://github.com/Tencent/ncnn netronhttps://netron.app二、模型转换 这里进行onnx转ncnn通过命令进行转换 onnx2ncnn onnxModel/encoder_model-sim.onnx ncnnModel/encoder_model.param ncnnModel/encoder_model.bin onnx2ncnn onnxModel/decoder_model-sim.onnx ncnnModel/decoder_model.param ncnnModel/decoder_model.bin转换成功可以看到 转换之后可以对模型进行优化但是奇怪的是这里优化了不起作用去不了MemoryData这些没用的op ncnnoptimize ncnnModel/encoder_model.param ncnnModel/encoder_model.bin ncnnModel/encoder_model.param ncnnModel/encoder_model.bin 1 ncnnoptimize ncnnModel/decoder_model.param ncnnModel/decoder_model.bin ncnnModel/decoder_model.param ncnnModel/decoder_model.bin 1三、ncnn模型加载与推理python版 跟onnx的推理比较类似就是函数的调用方法有点不同这里先用python实现验证下是否没问题方面后面部署到其它端比如android。 主要包括模型加载、推理模型搭建跟模型推理但要注意的是这里的输入输出名称需要在param这个文件里面获取。 采用netron分别查看encoder与decoder的网络结构获取输入输出名称: encoder: 输入输出分别如图 decoder: 输入 输出 推理代码如下推理过程感觉没问题但是推理输出结果相差很大对比过第一层ncnn与onnx的推理结果了可能问题出在模型转换环节的精度损失上而且第二层模型转换后网络输出结果不一致了很迷还没找出原因但是以下的推理是能运行通过只不过输出结果有问题 import numpy as np import ncnn# 加载字符 # 从 input_words.txt 文件中读取字符串 with open(config/input_words.txt, r) as f:input_words f.readlines()input_characters [line.rstrip(\n) for line in input_words]# 从 target_words.txt 文件中读取字符串 with open(config/target_words.txt, r, newline) as f:target_words [line.strip() for line in f.readlines()]target_characters [char.replace(\\t, \t).replace(\\n, \n) for char in target_words]#字符处理以方便进行编码 input_token_index dict([(char, i) for i, char in enumerate(input_characters)]) target_token_index dict([(char, i) for i, char in enumerate(target_characters)])# something readable. reverse_input_char_index dict((i, char) for char, i in input_token_index.items()) reverse_target_char_index dict((i, char) for char, i in target_token_index.items()) num_encoder_tokens len(input_characters) # 英文字符数量 num_decoder_tokens len(target_characters) # 中文文字数量import json with open(config/config.json, r) as file:loaded_data json.load(file)# 从加载的数据中获取max_encoder_seq_length和max_decoder_seq_length的值 max_encoder_seq_length loaded_data[max_encoder_seq_length] max_decoder_seq_length loaded_data[max_decoder_seq_length]# Load the ncnn models for the encoder and decoder encoderNet ncnn.Net() encoderNet.load_param(ncnnModel/encoder_model.param) encoderNet.load_model(ncnnModel/encoder_model.bin)decoderNet ncnn.Net() decoderNet.load_param(ncnnModel/decoder_model.param) decoderNet.load_model(ncnnModel/decoder_model.bin)def decode_sequence(input_seq):# Encode the input as state vectors.# print(input_seq)ex_encoder encoderNet.create_extractor()ex_encoder.input(input_1, ncnn.Mat(input_seq))states_value []_, LSTM_1 ex_encoder.extract(lstm)_, LSTM_2 ex_encoder.extract(lstm_1)states_value.append(LSTM_1)states_value.append(LSTM_2)# print(ncnn.Mat(input_seq))# print(vgdgd)# Generate empty target sequence of length 1.target_seq np.zeros((1, 1, 849))# Populate the first character of target sequence with the start character.target_seq[0, 0, target_token_index[\t]] 1.# this target_seq you can treat as initial state# Sampling loop for a batch of sequences# (to simplify, here we assume a batch of size 1).stop_condition Falsedecoded_sentence ex_decoder decoderNet.create_extractor()while not stop_condition:#print(ncnn.Mat(target_seq))print(---------)ex_decoder.input(input_2, ncnn.Mat(target_seq))ex_decoder.input(input_3, states_value[0])ex_decoder.input(input_4, states_value[1])_, output_tokens ex_decoder.extract(dense)_, h ex_decoder.extract(lstm_1)_, c ex_decoder.extract(lstm_1_1)print(output_tokens)tk []for i in range(849):tk.append(output_tokens[849*i])tk np.array(tk)output_tokens tk.reshape(1,1,849)print(output_tokens)# print(fdgd)print(h)print(c)# output_tokens np.array(output_tokens)# output_tokens output_tokens.reshape(1, 1, -1)# # h np.array(h)# # c np.array(c)# print(output_tokens.shape)# print(h.shape)# print(c.shape)#output_tokens, h, c decoder_model.predict([target_seq] states_value)# Sample a token# argmax: Returns the indices of the maximum values along an axis# just like find the most possible charsampled_token_index np.argmax(output_tokens[0, -1, :])# find char using indexsampled_char reverse_target_char_index[sampled_token_index]# and append sentencedecoded_sentence sampled_char# Exit condition: either hit max length# or find stop character.if (sampled_char \n or len(decoded_sentence) max_decoder_seq_length):stop_condition True# Update the target sequence (of length 1).# append then ?# creating another new target_seq# and this time assume sampled_token_index to 1.0target_seq np.zeros((1, 1, num_decoder_tokens))target_seq[0, 0, sampled_token_index] 1.print(sampled_token_index)# Update states# update states, frome the front partsstates_value [h, c]return decoded_sentenceimport numpy as npinput_text Call me. encoder_input_data np.zeros((1,max_encoder_seq_length, num_encoder_tokens),dtypefloat32) for t, char in enumerate(input_text):print(char)# 3D vector only z-index has char its value equals 1.0encoder_input_data[0,t, input_token_index[char]] 1.input_seq encoder_input_data decoded_sentence decode_sequence(input_seq) print(-) print(Input sentence:, input_text) print(Decoded sentence:, decoded_sentence) decoder的模型输出为849*849感觉怪怪的然后我们把模型的输入固定下来看看是不是模型的问题。 打开decoder_model.param把输入层固定下来0w 1h 2c那么 input_20849 11 21 input_30256 11 input_40256 11 运行以下命令进行优化 ncnnoptimize ncnnModel/decoder_model.param ncnnModel/decoder_model.bin ncnnModel/decoder_model.param ncnnModel/decoder_model.bin 1 结果如下 打开网络来看一下 可以看到输出确实是849849红色框那就是模型转换有问题了 仔细看能够看到有两个shape蓝色框分别为849跟8491这两个不同维度的网络进行BinaryOP之后就变成849849了那么我们把Reshape这个网络去掉试试不把前面InnerProduct的输入维度有849reshape为8491下面来看手术刀怎么操作。 我们需要在没经过固定维度并ncnnoptimize的模型上操作也就是没经过上面0w 1h 2c修改的模型上操作 根据名字我们找到Reshape那一层 然后找到与reshape那一层相连接的上一层(红色框)与下一层(蓝色框) 通过红色框与蓝色框里面的名字我们找到了上层与下层分别为InnerProduct与BinaryOp 这时候把InnerProduct与BinaryOp接上把Reshape删掉 再改一下最上面的层数把19改为18因为我们删掉了一层 保存之后再次执行 ncnnoptimize ncnnModel/decoder_model.param ncnnModel/decoder_model.bin ncnnModel/decoder_model.param ncnnModel/decoder_model.bin 1执行后可以看到网络层数跟blob数都更新了 这时候改一下固定一下输入层数并运行ncnnoptimize再打开netron看一下网络结构可以看到输出维度正常了 但是通过推理结果还是不对没找到原因推理代码如下 import numpy as np import ncnn# 加载字符 # 从 input_words.txt 文件中读取字符串 with open(config/input_words.txt, r) as f:input_words f.readlines()input_characters [line.rstrip(\n) for line in input_words]# 从 target_words.txt 文件中读取字符串 with open(config/target_words.txt, r, newline) as f:target_words [line.strip() for line in f.readlines()]target_characters [char.replace(\\t, \t).replace(\\n, \n) for char in target_words]#字符处理以方便进行编码 input_token_index dict([(char, i) for i, char in enumerate(input_characters)]) target_token_index dict([(char, i) for i, char in enumerate(target_characters)])# something readable. reverse_input_char_index dict((i, char) for char, i in input_token_index.items()) reverse_target_char_index dict((i, char) for char, i in target_token_index.items()) num_encoder_tokens len(input_characters) # 英文字符数量 num_decoder_tokens len(target_characters) # 中文文字数量import json with open(config/config.json, r) as file:loaded_data json.load(file)# 从加载的数据中获取max_encoder_seq_length和max_decoder_seq_length的值 max_encoder_seq_length loaded_data[max_encoder_seq_length] max_decoder_seq_length loaded_data[max_decoder_seq_length]# Load the ncnn models for the encoder and decoder encoderNet ncnn.Net() encoderNet.load_param(ncnnModel/encoder_model.param) encoderNet.load_model(ncnnModel/encoder_model.bin)decoderNet ncnn.Net() decoderNet.load_param(ncnnModel/decoder_model.param) decoderNet.load_model(ncnnModel/decoder_model.bin)def decode_sequence(input_seq):# Encode the input as state vectors.# print(input_seq)ex_encoder encoderNet.create_extractor()ex_encoder.input(input_1, ncnn.Mat(input_seq))states_value []_, LSTM_1 ex_encoder.extract(lstm)_, LSTM_2 ex_encoder.extract(lstm_1)states_value.append(LSTM_1)states_value.append(LSTM_2)# print(ncnn.Mat(input_seq))# print(vgdgd)# Generate empty target sequence of length 1.target_seq np.zeros((1, 1, 849))# Populate the first character of target sequence with the start character.target_seq[0, 0, target_token_index[\t]] 1.# this target_seq you can treat as initial state# Sampling loop for a batch of sequences# (to simplify, here we assume a batch of size 1).stop_condition Falsedecoded_sentence ex_decoder decoderNet.create_extractor()while not stop_condition:#print(ncnn.Mat(target_seq))print(---------)ex_decoder.input(input_2, ncnn.Mat(target_seq))ex_decoder.input(input_3, states_value[0])ex_decoder.input(input_4, states_value[1])_, output_tokens ex_decoder.extract(dense)_, h ex_decoder.extract(lstm_1)_, c ex_decoder.extract(lstm_1_1)print(output_tokens)# print(ghfhf)# tk []# for i in range(849):# tk.append(output_tokens[849*i])# tk np.array(tk)# output_tokens tk.reshape(1,1,849)# print(output_tokens)# print(fdgd)print(h)print(c)output_tokens np.array(output_tokens)output_tokens output_tokens.reshape(1, 1, -1)# # h np.array(h)# # c np.array(c)# print(output_tokens.shape)# print(h.shape)# print(c.shape)#output_tokens, h, c decoder_model.predict([target_seq] states_value)# Sample a token# argmax: Returns the indices of the maximum values along an axis# just like find the most possible charsampled_token_index np.argmax(output_tokens[0, -1, :])# find char using indexsampled_char reverse_target_char_index[sampled_token_index]# and append sentencedecoded_sentence sampled_char# Exit condition: either hit max length# or find stop character.if (sampled_char \n or len(decoded_sentence) max_decoder_seq_length):stop_condition True# Update the target sequence (of length 1).# append then ?# creating another new target_seq# and this time assume sampled_token_index to 1.0target_seq np.zeros((1, 1, num_decoder_tokens))target_seq[0, 0, sampled_token_index] 1.print(sampled_token_index)# Update states# update states, frome the front partsstates_value [h, c]return decoded_sentenceimport numpy as npinput_text Call me. encoder_input_data np.zeros((1,max_encoder_seq_length, num_encoder_tokens),dtypefloat32) for t, char in enumerate(input_text):print(char)# 3D vector only z-index has char its value equals 1.0encoder_input_data[0,t, input_token_index[char]] 1.input_seq encoder_input_data decoded_sentence decode_sequence(input_seq) print(-) print(Input sentence:, input_text) print(Decoded sentence:, decoded_sentence) 参考文献https://github.com/Tencent/ncnn/issues/2586
http://www.w-s-a.com/news/431751/

相关文章:

  • 网站建设费属于广告费小猪网站怎么做的
  • 国内优秀设计网站站长哈尔滨微网站建设
  • 如何建设一个优秀的电商网站沐风seo
  • 从零开始学网站建设知乎安防网站下载
  • 打开网站弹出qq应用软件有哪些
  • 温州网站建设seo网站 如何做 中英文切换
  • 聊城做网站的公司资讯信阳 网站建设
  • 天津市工程建设交易网站查汗国珠海 网页设计
  • 龙果学院大型网站稳定性建设汾阳做网站
  • 湖北 个人网站备案时间域名查询备案查询
  • 网站推广方式校园网站怎么建
  • 长沙seo网站排名怎么在百度发帖
  • 织梦贷款网站模板做印章网站
  • 彭州做网站上海百度网络推广
  • 广州网站搭建快速提升网站排名荧光字网站
  • 15年做那些网站能致富做seo是什么意思
  • 各电商网站的特点网站制作2007
  • 用html做一号店网站怎么做公众号注册平台官网
  • 做盈利网站怎么备案vs做网站如何调试
  • 嘉兴做营销型网站廊坊做网站外包
  • 双语网站模板常州做网站的公司
  • 广州市车管所网站建设全国做网站公司前十名
  • 太原手手工网站建设公司视频直播服务
  • 雷达图 做图网站wordpress首页怎么美化
  • 四川做网站设计公司价格vip解析网站怎么做的
  • 网站建设流程域名申请做化工的 有那些网站
  • 软件开发设计流程图seo搜索引擎官网
  • 外国小孩和大人做网站东富龙科技股份有限公司
  • 上线倒计时单页网站模板做网站的资金来源
  • 泸州市建设厅网站中小企业网络需求分析