我们从Python开源项目中,提取了以下30个代码示例,用于说明如何使用chainer.links.NStepLSTM()。
def __init__(self, n_layers, in_size, out_size, embed_size, hidden_size, proj_size, dropout=0.5): """Initialize encoder with structure parameters Args: n_layers (int): Number of layers. in_size (int): Dimensionality of input vectors. out_size (int): Dimensionality of output vectors. embed_size (int): Dimensionality of word embedding. hidden_size (int) : Dimensionality of hidden vectors. proj_size (int) : Dimensionality of projection before softmax. dropout (float): Dropout ratio. """ super(LSTMDecoder, self).__init__( embed = L.EmbedID(in_size, embed_size), lstm = L.NStepLSTM(n_layers, embed_size, hidden_size, dropout), proj = L.Linear(hidden_size, proj_size), out = L.Linear(proj_size, out_size) ) self.dropout = dropout for param in self.params(): param.data[...] = np.random.uniform(-0.1, 0.1, param.data.shape)
def __init__(self, n_layers, n_source_vocab, n_target_vocab, n_units): super(Seq2seq, self).__init__( embed_x=L.EmbedID(n_source_vocab, n_units), embed_y=L.EmbedID(n_target_vocab, n_units), encoder=L.NStepLSTM(n_layers, n_units, n_units, 0.1), decoder=L.NStepLSTM(n_layers, n_units, n_units, 0.1), W=L.Linear(n_units, n_target_vocab), ) self.n_layers = n_layers self.n_units = n_units
def __init__(self, model_path, word_dim=None, afix_dim=None, nlayers=2, hidden_dim=128, elu_dim=64, dep_dim=100, dropout_ratio=0.5): self.model_path = model_path defs_file = model_path + "/tagger_defs.txt" if word_dim is None: self.train = False Param.load(self, defs_file) self.extractor = FeatureExtractor(model_path) else: # training self.train = True p = Param(self) p.dep_dim = dep_dim p.word_dim = word_dim p.afix_dim = afix_dim p.hidden_dim = hidden_dim p.elu_dim = elu_dim p.nlayers = nlayers p.n_words = len(read_model_defs(model_path + "/words.txt")) p.n_suffixes = len(read_model_defs(model_path + "/suffixes.txt")) p.n_prefixes = len(read_model_defs(model_path + "/prefixes.txt")) p.targets = read_model_defs(model_path + "/target.txt") p.dump(defs_file) self.in_dim = self.word_dim + 8 * self.afix_dim self.dropout_ratio = dropout_ratio super(LSTMParser, self).__init__( emb_word=L.EmbedID(self.n_words, self.word_dim), emb_suf=L.EmbedID(self.n_suffixes, self.afix_dim, ignore_label=IGNORE), emb_prf=L.EmbedID(self.n_prefixes, self.afix_dim, ignore_label=IGNORE), lstm_f=L.NStepLSTM(nlayers, self.in_dim, self.hidden_dim, self.dropout_ratio), lstm_b=L.NStepLSTM(nlayers, self.in_dim, self.hidden_dim, self.dropout_ratio), linear_cat1=L.Linear(2 * self.hidden_dim, self.elu_dim), linear_cat2=L.Linear(self.elu_dim, len(self.targets)), linear_dep=L.Linear(2 * self.hidden_dim, self.dep_dim), linear_head=L.Linear(2 * self.hidden_dim, self.dep_dim), biaffine=Biaffine(self.dep_dim) )
def __init__(self, model_path, word_dim=None, afix_dim=None, nlayers=2, hidden_dim=128, relu_dim=64, dropout_ratio=0.5): self.model_path = model_path defs_file = model_path + "/tagger_defs.txt" if word_dim is None: self.train = False Param.load(self, defs_file) self.extractor = FeatureExtractor(model_path) else: self.train = True p = Param(self) p.word_dim = word_dim p.afix_dim = afix_dim p.hidden_dim = hidden_dim p.relu_dim = relu_dim p.nlayers = nlayers p.dump(defs_file) self.targets = read_model_defs(model_path + "/target.txt") self.words = read_model_defs(model_path + "/words.txt") self.suffixes = read_model_defs(model_path + "/suffixes.txt") self.prefixes = read_model_defs(model_path + "/prefixes.txt") self.in_dim = self.word_dim + 8 * self.afix_dim self.dropout_ratio = dropout_ratio super(LSTMTagger, self).__init__( emb_word=L.EmbedID(len(self.words), self.word_dim), emb_suf=L.EmbedID(len(self.suffixes), self.afix_dim, ignore_label=IGNORE), emb_prf=L.EmbedID(len(self.prefixes), self.afix_dim, ignore_label=IGNORE), lstm_f=L.NStepLSTM(nlayers, self.in_dim, self.hidden_dim, 0.), lstm_b=L.NStepLSTM(nlayers, self.in_dim, self.hidden_dim, 0.), linear1=L.Linear(2 * self.hidden_dim, self.relu_dim), linear2=L.Linear(self.relu_dim, len(self.targets)), )
def __init__(self, model_path, word_dim=None, char_dim=None, nlayers=2, hidden_dim=128, dep_dim=100, dropout_ratio=0.5): self.model_path = model_path defs_file = model_path + "/tagger_defs.txt" if word_dim is None: self.train = False Param.load(self, defs_file) self.extractor = FeatureExtractor(model_path) else: self.train = True p = Param(self) p.dep_dim = dep_dim p.word_dim = word_dim p.char_dim = char_dim p.hidden_dim = hidden_dim p.nlayers = nlayers p.n_words = len(read_model_defs(model_path + "/words.txt")) p.n_chars = len(read_model_defs(model_path + "/chars.txt")) p.targets = read_model_defs(model_path + "/target.txt") p.dump(defs_file) self.in_dim = self.word_dim + self.char_dim self.dropout_ratio = dropout_ratio super(BiaffineJaLSTMParser, self).__init__( emb_word=L.EmbedID(self.n_words, self.word_dim), emb_char=L.EmbedID(self.n_chars, 50, ignore_label=IGNORE), conv_char=L.Convolution2D(1, self.char_dim, (3, 50), stride=1, pad=(1, 0)), lstm_f=L.NStepLSTM(self.nlayers, self.in_dim, self.hidden_dim, 0.32), lstm_b=L.NStepLSTM(self.nlayers, self.in_dim, self.hidden_dim, 0.32), arc_dep=L.Linear(2 * self.hidden_dim, self.dep_dim), arc_head=L.Linear(2 * self.hidden_dim, self.dep_dim), rel_dep=L.Linear(2 * self.hidden_dim, self.dep_dim), rel_head=L.Linear(2 * self.hidden_dim, self.dep_dim), biaffine_arc=Biaffine(self.dep_dim), biaffine_tag=L.Bilinear(self.dep_dim, self.dep_dim, len(self.targets)) )
def __init__(self, model_path, word_dim=None, afix_dim=None, nlayers=2, hidden_dim=128, dep_dim=100, dropout_ratio=0.5): self.model_path = model_path defs_file = model_path + "/tagger_defs.txt" if word_dim is None: self.train = False Param.load(self, defs_file) self.extractor = FeatureExtractor(model_path) else: # training self.train = True p = Param(self) p.dep_dim = dep_dim p.word_dim = word_dim p.afix_dim = afix_dim p.hidden_dim = hidden_dim p.nlayers = nlayers p.n_words = len(read_model_defs(model_path + "/words.txt")) p.n_suffixes = len(read_model_defs(model_path + "/suffixes.txt")) p.n_prefixes = len(read_model_defs(model_path + "/prefixes.txt")) p.targets = read_model_defs(model_path + "/target.txt") p.dump(defs_file) self.in_dim = self.word_dim + 8 * self.afix_dim self.dropout_ratio = dropout_ratio super(FastBiaffineLSTMParser, self).__init__( emb_word=L.EmbedID(self.n_words, self.word_dim, ignore_label=IGNORE), emb_suf=L.EmbedID(self.n_suffixes, self.afix_dim, ignore_label=IGNORE), emb_prf=L.EmbedID(self.n_prefixes, self.afix_dim, ignore_label=IGNORE), lstm_f=L.NStepLSTM(self.nlayers, self.in_dim, self.hidden_dim, 0.32), lstm_b=L.NStepLSTM(self.nlayers, self.in_dim, self.hidden_dim, 0.32), arc_dep=L.Linear(2 * self.hidden_dim, self.dep_dim), arc_head=L.Linear(2 * self.hidden_dim, self.dep_dim), rel_dep=L.Linear(2 * self.hidden_dim, self.dep_dim), rel_head=L.Linear(2 * self.hidden_dim, self.dep_dim), biaffine_arc=Biaffine(self.dep_dim), biaffine_tag=Bilinear(self.dep_dim, self.dep_dim, len(self.targets)) )
def __init__( self, comm, n_layers, n_source_vocab, n_target_vocab, n_units): super(Encoder, self).__init__( embed_x=L.EmbedID(n_source_vocab, n_units), # Corresponding decoder LSTM will be invoked on process 1. mn_encoder=chainermn.links.create_multi_node_n_step_rnn( L.NStepLSTM(n_layers, n_units, n_units, 0.1), comm, rank_in=None, rank_out=1 ), ) self.comm = comm self.n_layers = n_layers self.n_units = n_units
def __init__( self, comm, n_layers, n_source_vocab, n_target_vocab, n_units): super(Decoder, self).__init__( embed_y=L.EmbedID(n_target_vocab, n_units), # Corresponding encoder LSTM will be invoked on process 0. mn_decoder=chainermn.links.create_multi_node_n_step_rnn( L.NStepLSTM(n_layers, n_units, n_units, 0.1), comm, rank_in=0, rank_out=None), W=L.Linear(n_units, n_target_vocab), ) self.comm = comm self.n_layers = n_layers self.n_units = n_units
def __init__(self, input_dimension: int, output_dimension: int, hidden_dimension: int, attention: bool = False): super().__init__() with super().init_scope(): self._embed_input = L.EmbedID(input_dimension, hidden_dimension) self._embed_output = L.EmbedID(output_dimension, hidden_dimension) self._encoder = L.NStepLSTM( n_layers=1, in_size=hidden_dimension, out_size=hidden_dimension, dropout=0.1) self._decoder = L.NStepLSTM( n_layers=1, in_size=hidden_dimension, out_size=hidden_dimension, dropout=0.1) # Embed ??????????????????? self._extract_output = L.Linear(hidden_dimension, output_dimension) self._use_attention = attention if attention: self._attention_layer = L.Linear(2 * hidden_dimension, hidden_dimension) else: self._attention_layer = None self._hyper_params = (input_dimension, output_dimension, hidden_dimension, attention)
def __init__(self, n_layers, in_size, out_size, dropout=0.5, use_cudnn=True): super(NStepLSTM, self).__init__(n_layers, in_size, out_size, dropout, use_cudnn) self.state_size = out_size self.reset_state()
def to_cpu(self): super(NStepLSTM, self).to_cpu() if self.cx is not None: self.cx.to_cpu() if self.hx is not None: self.hx.to_cpu()
def to_gpu(self, device=None): super(NStepLSTM, self).to_gpu(device) if self.cx is not None: self.cx.to_gpu(device) if self.hx is not None: self.hx.to_gpu(device)
def __call__(self, xs, train=True): batch = len(xs) if self.hx is None: xp = self.xp self.hx = Variable( xp.zeros((self.n_layers, batch, self.state_size), dtype=xs[0].dtype), volatile='auto') if self.cx is None: xp = self.xp self.cx = Variable( xp.zeros((self.n_layers, batch, self.state_size), dtype=xs[0].dtype), volatile='auto') hy, cy, ys = super(NStepLSTM, self).__call__(self.hx, self.cx, xs, train) self.hx, self.cx = hy, cy return ys
def __init__(self, n_layers, n_unit, n_vocab): super(BiNstepLstm, self).__init__( embed = L.EmbedID(n_vocab, n_unit), nstep_lstm_f = NStepLSTM(n_layers, n_unit, n_unit), nstep_lstm_b = NStepLSTM(n_layers, n_unit, n_unit), )
def __init__(self, n_layers, in_size, out_size, embed_size, dropout=0.5): """Initialize encoder with structure parameters Args: n_layers (int): Number of layers. in_size (int): Dimensionality of input vectors. out_size (int) : Dimensionality of hidden vectors to be output. embed_size (int): Dimensionality of word embedding. dropout (float): Dropout ratio. """ super(LSTMEncoder, self).__init__( embed = L.EmbedID(in_size, embed_size), lstm = L.NStepLSTM(n_layers, embed_size, out_size, dropout) ) for param in self.params(): param.data[...] = np.random.uniform(-0.1, 0.1, param.data.shape)
def __init__(self, vocaburary_size, img_feature_dim=2048, hidden_dim=512,dropout_ratio=0.5,train=True, n_layers=1): super(Image2CaptionDecoder, self).__init__( embed_word= L.EmbedID(vocaburary_size, hidden_dim), embed_image= L.Linear(img_feature_dim, hidden_dim), lstm = L.NStepLSTM(n_layers=n_layers,in_size=hidden_dim,out_size=hidden_dim,dropout=dropout_ratio), decode_word = L.Linear(hidden_dim, vocaburary_size), ) self.dropout_ratio = dropout_ratio self.train = train self.n_layers=n_layers self.hidden_dim=hidden_dim
def __init__(self, model_path, word_dim=None, char_dim=None, nlayers=2, hidden_dim=128, relu_dim=64, dep_dim=100, dropout_ratio=0.5): self.model_path = model_path defs_file = model_path + "/tagger_defs.txt" if word_dim is None: # use as supertagger with open(defs_file) as f: defs = json.load(f) self.dep_dim = defs["dep_dim"] self.word_dim = defs["word_dim"] self.char_dim = defs["char_dim"] self.hidden_dim = defs["hidden_dim"] self.relu_dim = defs["relu_dim"] self.nlayers = defs["nlayers"] self.train = False self.extractor = FeatureExtractor(model_path) else: # training self.dep_dim = dep_dim self.word_dim = word_dim self.char_dim = char_dim self.hidden_dim = hidden_dim self.relu_dim = relu_dim self.nlayers = nlayers self.train = True with open(defs_file, "w") as f: json.dump({"model": self.__class__.__name__, "word_dim": self.word_dim, "char_dim": self.char_dim, "hidden_dim": hidden_dim, "relu_dim": relu_dim, "nlayers": nlayers, "dep_dim": dep_dim}, f) self.targets = read_model_defs(model_path + "/target.txt") self.words = read_model_defs(model_path + "/words.txt") self.chars = read_model_defs(model_path + "/chars.txt") self.in_dim = self.word_dim + self.char_dim self.dropout_ratio = dropout_ratio super(JaLSTMParser, self).__init__( emb_word=L.EmbedID(len(self.words), self.word_dim), emb_char=L.EmbedID(len(self.chars), 50, ignore_label=IGNORE), conv_char=L.Convolution2D(1, self.char_dim, (3, 50), stride=1, pad=(1, 0)), lstm_f=L.NStepLSTM(nlayers, self.in_dim, self.hidden_dim, self.dropout_ratio), lstm_b=L.NStepLSTM(nlayers, self.in_dim, self.hidden_dim, self.dropout_ratio), linear_cat1=L.Linear(2 * self.hidden_dim, self.relu_dim), linear_cat2=L.Linear(self.relu_dim, len(self.targets)), linear_dep=L.Linear(2 * self.hidden_dim, self.dep_dim), linear_head=L.Linear(2 * self.hidden_dim, self.dep_dim), biaffine=Biaffine(self.dep_dim) )
def __init__(self, model_path, word_dim=None, char_dim=None, nlayers=2, hidden_dim=128, relu_dim=64, dropout_ratio=0.5): self.model_path = model_path defs_file = model_path + "/tagger_defs.txt" if word_dim is None: # use as supertagger with open(defs_file) as f: defs = json.load(f) self.word_dim = defs["word_dim"] self.char_dim = defs["char_dim"] self.hidden_dim = defs["hidden_dim"] self.relu_dim = defs["relu_dim"] self.nlayers = defs["nlayers"] self.train = False self.extractor = FeatureExtractor(model_path) else: # training self.word_dim = word_dim self.char_dim = char_dim self.hidden_dim = hidden_dim self.relu_dim = relu_dim self.nlayers = nlayers self.train = True with open(defs_file, "w") as f: json.dump({"model": self.__class__.__name__, "word_dim": self.word_dim, "char_dim": self.char_dim, "hidden_dim": hidden_dim, "relu_dim": relu_dim, "nlayers": nlayers}, f) self.targets = read_model_defs(model_path + "/target.txt") self.words = read_model_defs(model_path + "/words.txt") self.chars = read_model_defs(model_path + "/chars.txt") self.in_dim = self.word_dim + self.char_dim self.dropout_ratio = dropout_ratio super(JaLSTMTagger, self).__init__( emb_word=L.EmbedID(len(self.words), self.word_dim), emb_char=L.EmbedID(len(self.chars), 50, ignore_label=IGNORE), conv_char=L.Convolution2D(1, self.char_dim, (3, 50), stride=1, pad=(1, 0)), lstm_f=L.NStepLSTM(nlayers, self.in_dim, self.hidden_dim, 0.), lstm_b=L.NStepLSTM(nlayers, self.in_dim, self.hidden_dim, 0.), conv1=L.Convolution2D(1, 2 * self.hidden_dim, (7, 2 * self.hidden_dim), stride=1, pad=(3, 0)), linear1=L.Linear(2 * self.hidden_dim, self.relu_dim), linear2=L.Linear(self.relu_dim, len(self.targets)), )