import tensorflow as tf import numpy as np curr_dir = os.path.dirname(os.path.realpath(__file__)) from qiznlp.common.modules.common_layers import mask_nonpad_from_embedding, add_timing_signal_1d, label_smoothing, softmax_cross_entropy from qiznlp.common.modules.embedding import embedding from qiznlp.common.modules.encoder import transformer_encoder, mean_pool, multi_head_attention_base_pool import qiznlp.common.utils as utils conf = utils.dict2obj({ 'vocab_size': 14180, 'label_size': 16, "embed_size": 300, 'hidden_size': 300, 'num_heads': 6, 'num_encoder_layers': 6, 'dropout_rate': 0.2, 'lr': 1e-3, 'pretrain_emb': None, # 'pretrain_emb': np.load(f'{curr_dir}/pretrain_word_emb300.npy'), }) class Model(object): def __init__(self, build_graph=True, **kwargs): self.conf = conf self.run_model = kwargs.get('run_model', None) # acquire outside run_model instance if build_graph: # build placeholder self.build_placeholder() # build model
sys.path.append(curr_dir + '/..') # 添加上级目录即默认qiznlp根目录 from model.mch_model import Model as MCH_Model try: import horovod.tensorflow as hvd # 示例:horovodrun -np 2 -H localhost:2 python run_mch.py # 可根据local_rank设置shard的数据,保证各个gpu采样的数据不重叠。 except: HVD_ENABLE = False else: HVD_ENABLE = True conf = utils.dict2obj({ 'early_stop_patience': None, 'just_save_best': False, 'n_epochs': 20, 'data_type': 'tfrecord', # 'data_type': 'pkldata', }) class Run_Model_Mch(Run_Model_Base): def __init__(self, model_name, tokenize=None, pbmodel_dir=None, use_hvd=False): # 维护sess graph config saver self.model_name = model_name if tokenize is None: self.jieba = jieba.Tokenizer()
from qiznlp.common.modules.common_layers import shape_list, mask_nonpad_from_embedding, add_timing_signal_1d, get_timing_signal_1d, shift_right, split_heads from qiznlp.common.modules.embedding import embedding, proj_logits from qiznlp.common.modules.encoder import transformer_encoder, transformer_decoder, EncDecAttention from qiznlp.common.modules.birnn import Bi_RNN, RNN from qiznlp.common.modules.beam_search import beam_search, greedy_search, get_state_shape_invariants import qiznlp.common.utils as utils conf = utils.dict2obj({ 'max_turn': 5, 'vocab_size': 4000, 'embed_size': 300, 'hidden_size': 300, 'num_heads': 6, 'num_encoder_layers': 6, 'num_decoder_layers': 6, 'dropout_rate': 0.2, 'lr': 1e-3, 'pretrain_emb': None, 'beam_size': 5, 'max_decode_len': 50, 'eos_id': 2, }) class Model(object): def __init__(self, build_graph=True, **kwargs): self.conf = conf self.run_model = kwargs.get('run_model', None) # acquire outside run_model instance if build_graph: # build placeholder
import os import tensorflow as tf import numpy as np curr_dir = os.path.dirname(os.path.realpath(__file__)) from qiznlp.common.modules.common_layers import mask_nonpad_from_embedding from qiznlp.common.modules.embedding import embedding from qiznlp.common.modules.birnn import Bi_RNN import qiznlp.common.utils as utils conf = utils.dict2obj({ 'vocab_size': 1142, 'embed_size': 300, 'birnn_hidden_size': 300, 'l2_reg': 0.0001, 'dropout_rate': 0.2, 'lr': 1e-3, 'pretrain_emb': None, }) class Model(object): def __init__(self, build_graph=True, **kwargs): self.conf = conf self.run_model = kwargs.get('run_model', None) # acquire outside run_model instance if build_graph: # build placeholder self.build_placeholder() # build model self.model_name = kwargs.get('model_name', 'esim')
from qiznlp.common.modules.common_layers import shape_list, mask_nonpad_from_embedding, add_timing_signal_1d, get_timing_signal_1d, shift_right, split_heads from qiznlp.common.modules.embedding import embedding, proj_logits from qiznlp.common.modules.encoder import transformer_encoder, transformer_decoder, EncDecAttention from qiznlp.common.modules.birnn import Bi_RNN from qiznlp.common.modules.beam_search import beam_search, greedy_search, get_state_shape_invariants import qiznlp.common.utils as utils conf = utils.dict2obj({ 'vocab_size': 4000, 'embed_size': 300, 'hidden_size': 300, 'num_heads': 6, 'num_encoder_layers': 6, 'num_decoder_layers': 6, 'dropout_rate': 0.2, 'lr': 1e-3, 'pretrain_emb': None, 'beam_size': 40, 'max_decode_len': 50, 'eos_id': 2, 'gamma': 1, # 多样性鼓励因子 'num_group': 1, # 分组beam_search 'top_k': 30 # 详见beam_search }) class Model(object): def __init__(self, build_graph=True, **kwargs): self.conf = conf self.run_model = kwargs.get('run_model', None) # acquire outside run_model instance if build_graph:
import tensorflow as tf import numpy as np curr_dir = os.path.dirname(os.path.realpath(__file__)) from qiznlp.common.modules.common_layers import mask_nonpad_from_embedding from qiznlp.common.modules.embedding import embedding from qiznlp.common.modules.birnn import Bi_RNN from qiznlp.common.modules.idcnn import IDCNN import qiznlp.common.utils as utils conf = utils.dict2obj({ 'vocab_size': 1847, 'embed_size': 300, 'label_size': 30, 'birnn_hidden_size': 300, 'dropout_rate': 0.2, 'lr': 1e-3, 'pretrain_emb': None, # 'pretrain_emb': np.load(f'{curr_dir}/pretrain_word_emb300.npy'), }) class Model(object): def __init__(self, build_graph=True, **kwargs): self.conf = conf self.run_model = kwargs.get('run_model', None) # acquire outside run_model instance if build_graph: # build placeholder self.build_placeholder() # build model