Python chainer.links.NStepLSTM() Examples
The following are 30
code examples of chainer.links.NStepLSTM().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
chainer.links
, or try the search function
.
Example #1
Source File: model.py From TSNetVocoder with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, indim, outdim, normfac, fl=400, fs=80, fftl=512, fbsize=400): self.indim = indim self.outdim = outdim self.fl = fl self.fs = fs self.fftl = fftl self.fbsize = fbsize self.normfac = {'input' : {'mean' : cuda.to_gpu(normfac['input']['mean']), 'std' : cupy.fmax(cuda.to_gpu(normfac['input']['std']), 1.0E-6)}, 'output' : {'mean' : cuda.to_gpu(normfac['output']['mean']), 'std' : cupy.fmax(cuda.to_gpu(normfac['output']['std']), 1.0E-6)}} super(Model, self).__init__() with self.init_scope(): self.lx1 = L.NStepBiLSTM(1, self.indim, self.indim//2, 0.0) self.lx2 = L.Convolution2D(1, self.indim, (5, self.indim), (1, 1), (2, 0)) self.ly1 = L.NStepLSTM(3, self.fbsize+self.indim, 256, 0.0) self.ly2 = L.Linear(256, self.outdim)
Example #2
Source File: lstm_decoder.py From DSTC6-End-to-End-Conversation-Modeling with MIT License | 6 votes |
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)
Example #3
Source File: lstm_decoder.py From DSTC6-End-to-End-Conversation-Modeling with MIT License | 6 votes |
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)
Example #4
Source File: lstm_decoder.py From DSTC6-End-to-End-Conversation-Modeling with MIT License | 6 votes |
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)
Example #5
Source File: nets.py From vecto with Mozilla Public License 2.0 | 6 votes |
def __init__(self, n_layers, n_vocab, n_units, dropout=0.1, wv=None): super(RNNEncoder, self).__init__() with self.init_scope(): if wv is None: self.embed = L.EmbedID(n_vocab, n_units, ignore_label=-1, initialW=embed_init) else: # TODO: this implementation was allowing for dynamic embeddings # think about how to support both continuous embeddings # and function pointers # self.embed = self.get_embed_from_wv self.embed = L.EmbedID(n_vocab, n_units, ignore_label=-1, initialW=wv) self.encoder = L.NStepLSTM(n_layers, n_units, n_units, dropout) self.n_layers = n_layers self.out_units = n_units self.dropout = dropout
Example #6
Source File: lstm_decoder.py From DSTC6-End-to-End-Conversation-Modeling with MIT License | 6 votes |
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)
Example #7
Source File: initializer.py From chainer-compiler with MIT License | 6 votes |
def collect_inits(lk, pathname): res = [] for na, pa in lk.namedparams(): if isinstance(pa.data, type(None)): continue if na.count('/') == 1: res.append((pathname + na, pa)) if isinstance(lk, L.BatchNormalization): res.append((pathname + '/avg_mean', lk.avg_mean)) # TODO(satos) このままだと、nodeのテストは通るがResNetのテストがつらい # lk.avg_var = np.ones(lk.avg_var.shape).astype(np.float32) * 4.0 res.append((pathname + '/avg_var', lk.avg_var)) elif isinstance(lk, L.NStepLSTM) or isinstance(lk, L.NStepBiLSTM): # 先にこちらで集めてしまう for i, clk in enumerate(lk.children()): for param in clk.params(): res.append((pathname + '/%d/%s' % (i, param.name), param)) return res for clk in lk.children(): res += collect_inits(clk, pathname + '/' + clk.name) return res
Example #8
Source File: initializer.py From chainer-compiler with MIT License | 6 votes |
def collect_inits(lk, pathname): res = [] for na, pa in lk.namedparams(): if isinstance(pa.data, type(None)): continue if na.count('/') == 1: res.append((pathname + na, pa)) if isinstance(lk, L.BatchNormalization): res.append((pathname + '/avg_mean', lk.avg_mean)) # TODO(satos) このままだと、nodeのテストは通るがResNetのテストがつらい # lk.avg_var = np.ones(lk.avg_var.shape).astype(np.float32) * 4.0 res.append((pathname + '/avg_var', lk.avg_var)) elif isinstance(lk, L.NStepLSTM) or isinstance(lk, L.NStepBiLSTM): # 先にこちらで集めてしまう for i, clk in enumerate(lk.children()): for param in clk.params(): res.append((pathname + '/%d/%s' % (i, param.name), param)) return res for clk in lk.children(): res += collect_inits(clk, pathname + '/' + clk.name) return res
Example #9
Source File: links.py From chainer-compiler with MIT License | 6 votes |
def __init__(self, ch): super(Link_NStepLSTM, self).__init__(L.NStepLSTM(1, 1, 1, 0)) hd = ch.children().__next__() if not(hd.w0 is None): self.n_in = hd.w0.shape[1] else: self.n_in = None self.out_size = ch.out_size self.n_layers = ch.n_layers self.dropout = ch.dropout self.ws = [] self.bs = [] for i in range(self.n_layers): ws = [] bs = [] for j in range(8): ws.append(helper.make_tensor_value_info( ('/%d/w%d' % (i, j)), TensorProto.FLOAT, ["TODO"])) bs.append(helper.make_tensor_value_info( ('/%d/b%d' % (i, j)), TensorProto.FLOAT, ["TODO"])) self.ws.append(ws) self.bs.append(bs)
Example #10
Source File: model.py From chainer with MIT License | 6 votes |
def __init__(self, vocab_size, hidden_size, dropout_ratio, ignore_label): super(NStepLSTMLanguageModel, self).__init__() with self.init_scope(): self.embed_word = L.EmbedID( vocab_size, hidden_size, initialW=initializers.Normal(1.0), ignore_label=ignore_label ) self.embed_img = L.Linear( hidden_size, initialW=initializers.Normal(0.01) ) self.lstm = L.NStepLSTM(1, hidden_size, hidden_size, dropout_ratio) self.decode_caption = L.Linear( hidden_size, vocab_size, initialW=initializers.Normal(0.01) ) self.dropout_ratio = dropout_ratio
Example #11
Source File: test_link_n_step_lstm.py From chainer with MIT License | 6 votes |
def setUp(self): shape = (self.n_layers, len(self.lengths), self.out_size) if self.hidden_none: self.h = self.c = numpy.zeros(shape, 'f') else: self.h = numpy.random.uniform(-1, 1, shape).astype('f') self.c = numpy.random.uniform(-1, 1, shape).astype('f') self.xs = [ numpy.random.uniform(-1, 1, (l, self.in_size)).astype('f') for l in self.lengths] self.gh = numpy.random.uniform(-1, 1, shape).astype('f') self.gc = numpy.random.uniform(-1, 1, shape).astype('f') self.gys = [ numpy.random.uniform(-1, 1, (l, self.out_size)).astype('f') for l in self.lengths] self.rnn = links.NStepLSTM( self.n_layers, self.in_size, self.out_size, self.dropout) for layer in self.rnn: for p in layer.params(): p.array[...] = numpy.random.uniform(-1, 1, p.shape) self.rnn.cleargrads()
Example #12
Source File: test_link_n_step_lstm.py From chainer with MIT License | 6 votes |
def check_multi_gpu_forward(self, train=True): # See chainer/chainer#6262 # NStepLSTM w/ cudnn & dropout should work on not current device msg = None rnn = self.rnn.copy('copy') rnn.dropout = .5 with cuda.get_device_from_id(1): if self.hidden_none: h = None else: h = cuda.to_gpu(self.h) c = cuda.to_gpu(self.c) xs = [cuda.to_gpu(x) for x in self.xs] with testing.assert_warns(DeprecationWarning): rnn = rnn.to_gpu() with cuda.get_device_from_id(0),\ chainer.using_config('train', train),\ chainer.using_config('use_cudnn', 'always'): try: rnn(h, c, xs) except Exception as e: msg = e assert msg is None
Example #13
Source File: stateless_recurrent.py From chainerrl with MIT License | 6 votes |
def get_recurrent_state_at(link, recurrent_state, indices, unwrap_variable): if recurrent_state is None: return None if isinstance(link, L.NStepLSTM): h, c = recurrent_state if unwrap_variable: h = h.array c = c.array # shape: (n_layers, batch_size, out_size) assert h.ndim == 3 assert c.ndim == 3 return (h[:, indices], c[:, indices]) if isinstance(link, (L.NStepGRU, L.NStepRNNReLU, L.NStepRNNTanh)): h = recurrent_state if unwrap_variable: h = h.array # shape: (n_layers, batch_size, out_size) assert h.ndim == 3 return h[:, indices] if isinstance(link, StatelessRecurrent): return link.get_recurrent_state_at( recurrent_state, indices, unwrap_variable) else: raise ValueError('{} is not a recurrent link'.format(link))
Example #14
Source File: stateless_recurrent.py From chainerrl with MIT License | 6 votes |
def mask_recurrent_state_at(link, recurrent_state, indices): if recurrent_state is None: return None if isinstance(link, L.NStepLSTM): h, c = recurrent_state # shape: (n_layers, batch_size, out_size) assert h.ndim == 3 assert c.ndim == 3 mask = link.xp.ones_like(h.array) mask[:, indices] = 0 c = c * mask h = h * mask return (h, c) if isinstance(link, (L.NStepGRU, L.NStepRNNReLU, L.NStepRNNTanh)): h = recurrent_state # shape: (n_layers, batch_size, out_size) assert h.ndim == 3 mask = link.xp.ones_like(h.array) mask[:, indices] = 0 h = h * mask return h if isinstance(link, StatelessRecurrent): return link.mask_recurrent_state_at(recurrent_state, indices) else: raise ValueError('{} is not a recurrent link'.format(link))
Example #15
Source File: stateless_recurrent.py From chainerrl with MIT License | 6 votes |
def is_recurrent_link(layer): """Return True iff a given layer is recurrent and supported by ChainerRL. Args: layer (callable): Any callable object. Returns: bool: True iff a given layer is recurrent and supported by ChainerRL. """ return isinstance(layer, ( L.NStepLSTM, L.NStepGRU, L.NStepRNNReLU, L.NStepRNNTanh, StatelessRecurrent, ))
Example #16
Source File: model.py From pfio with MIT License | 6 votes |
def __init__(self, vocab_size, hidden_size, dropout_ratio, ignore_label): super(NStepLSTMLanguageModel, self).__init__() with self.init_scope(): self.embed_word = L.EmbedID( vocab_size, hidden_size, initialW=initializers.Normal(1.0), ignore_label=ignore_label ) self.embed_img = L.Linear( hidden_size, initialW=initializers.Normal(0.01) ) self.lstm = L.NStepLSTM(1, hidden_size, hidden_size, dropout_ratio) self.decode_caption = L.Linear( hidden_size, vocab_size, initialW=initializers.Normal(0.01) ) self.dropout_ratio = dropout_ratio
Example #17
Source File: lm_nets.py From models with MIT License | 5 votes |
def __init__(self, n_vocab, n_units, n_layers=2, dropout=0.5, share_embedding=False, adaptive_softmax=False, vocab_freq=None, norm_to_one=False, n_units_word=256): super(RNNForLM, self).__init__() with self.init_scope(): # n_units_word = 256 if vocab_freq is not None: self.embed = EmbedIDNormalized( n_vocab, n_units_word, vocab_freq=vocab_freq, norm_to_one=norm_to_one) else: self.embed = L.EmbedID(n_vocab, n_units_word) self.rnn = L.NStepLSTM(n_layers, n_units_word, n_units, dropout) assert(not (share_embedding)) if share_embedding: self.output = SharedOutputLayer(self.embed.W) elif adaptive_softmax: self.output = AdaptiveSoftmaxOutputLayer( n_units, n_vocab, cutoff=[2000, 10000], reduce_k=4) else: self.output = NormalOutputLayer(n_units, n_vocab) self.dropout = dropout self.n_units = n_units self.n_layers = n_layers self.norm_vecs_one = False for name, param in self.namedparams(): if param.ndim != 1: # This initialization is applied only for weight matrices param.data[...] = np.random.uniform( -0.1, 0.1, param.data.shape) self.loss = 0. self.reset_state()
Example #18
Source File: nets.py From contextual_augmentation with MIT License | 5 votes |
def __init__(self, n_vocab, n_units, n_layers=2, dropout=0.5): super(BiLanguageModel, self).__init__() with self.init_scope(): self.embed = L.EmbedID(n_vocab, n_units) RNN = L.NStepLSTM self.encoder_fw = RNN(n_layers, n_units, n_units, dropout) self.encoder_bw = RNN(n_layers, n_units, n_units, dropout) self.output = NormalOutputLayer(n_units, n_vocab) self.mlp = MLP(1, n_units * 2, n_units, n_units, dropout) self.dropout = dropout self.n_units = n_units self.n_layers = n_layers
Example #19
Source File: nets.py From treasure-boxes with MIT License | 5 votes |
def __init__(self, n_layers, n_vocab, n_units, dropout=0.1): super(RNNEncoder, self).__init__() with self.init_scope(): self.embed = L.EmbedID(n_vocab, n_units, initialW=embed_init) self.encoder = L.NStepLSTM(n_layers, n_units, n_units, dropout) self.n_layers = n_layers self.out_units = n_units self.dropout = dropout
Example #20
Source File: set2set.py From chainer-chemistry with MIT License | 5 votes |
def __init__(self, in_channels, n_layers=1): # type: (int, int) -> None super(Set2Set, self).__init__() with self.init_scope(): self.lstm_layer = links.NStepLSTM( n_layers=n_layers, in_size=in_channels * 2, out_size=in_channels, dropout=0) self.in_channels = in_channels self.n_layers = n_layers self.hx = None # type: Optional[chainer.Variable] self.cx = None # type: Optional[chainer.Variable] self.q_star = None # type: Optional[List]
Example #21
Source File: nets.py From contextual_augmentation with MIT License | 5 votes |
def __init__(self, n_layers, n_vocab, n_units, dropout=0.1): super(RNNEncoder, self).__init__( embed=L.EmbedID(n_vocab, n_units, initialW=embed_init), encoder=L.NStepLSTM(n_layers, n_units, n_units, dropout), ) self.n_layers = n_layers self.out_units = n_units self.dropout = dropout self.use_predict_embed = False
Example #22
Source File: encoders.py From espnet with Apache License 2.0 | 5 votes |
def __init__(self, idim, elayers, cdim, hdim, dropout, typ="lstm"): super(RNN, self).__init__() bidir = typ[0] == "b" if bidir: rnn = L.NStepBiLSTM if "lstm" in typ else L.NStepBiGRU else: rnn = L.NStepLSTM if "lstm" in typ else L.NStepGRU _cdim = 2 * cdim if bidir else cdim with self.init_scope(): self.nbrnn = rnn(elayers, idim, cdim, dropout) self.l_last = L.Linear(_cdim, hdim) self.typ = typ self.bidir = bidir
Example #23
Source File: encoders.py From espnet with Apache License 2.0 | 5 votes |
def __init__(self, idim, elayers, cdim, hdim, subsample, dropout, typ="blstm"): super(RNNP, self).__init__() bidir = typ[0] == "b" if bidir: rnn = L.NStepBiLSTM if "lstm" in typ else L.NStepBiGRU else: rnn = L.NStepLSTM if "lstm" in typ else L.NStepGRU rnn_label = "birnn" if bidir else "rnn" with self.init_scope(): for i in six.moves.range(elayers): if i == 0: inputdim = idim else: inputdim = hdim _cdim = 2 * cdim if bidir else cdim # bottleneck layer to merge setattr( self, "{}{:d}".format(rnn_label, i), rnn(1, inputdim, cdim, dropout) ) setattr(self, "bt%d" % i, L.Linear(_cdim, hdim)) self.elayers = elayers self.rnn_label = rnn_label self.cdim = cdim self.subsample = subsample self.typ = typ self.bidir = bidir
Example #24
Source File: dnn_model.py From jhu-neural-wpe with Apache License 2.0 | 5 votes |
def __init__(self, nfft=257, context=5): super(LSTM_dereverb, self).__init__() with self.init_scope(): self.lstm = L.NStepLSTM(1, (nfft*2*context)+nfft, 500, 0) self.ll1 = L.Linear(500, 2048) self.ll2 = L.Linear(2048, nfft) # This function is for forward propagation
Example #25
Source File: seq2seq.py From chainer with MIT License | 5 votes |
def __init__(self, n_layers, n_source_vocab, n_target_vocab, n_units): super(Seq2seq, self).__init__() with self.init_scope(): self.embed_x = L.EmbedID(n_source_vocab, n_units) self.embed_y = L.EmbedID(n_target_vocab, n_units) self.encoder = L.NStepLSTM(n_layers, n_units, n_units, 0.1) self.decoder = L.NStepLSTM(n_layers, n_units, n_units, 0.1) self.W = L.Linear(n_units, n_target_vocab) self.n_layers = n_layers self.n_units = n_units
Example #26
Source File: seq2seq.py From chainer with MIT License | 5 votes |
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
Example #27
Source File: seq2seq_chainerio.py From pfio with MIT License | 5 votes |
def __init__(self, n_layers, n_source_vocab, n_target_vocab, n_units): super(Seq2seq, self).__init__() with self.init_scope(): self.embed_x = L.EmbedID(n_source_vocab, n_units) self.embed_y = L.EmbedID(n_target_vocab, n_units) self.encoder = L.NStepLSTM(n_layers, n_units, n_units, 0.1) self.decoder = L.NStepLSTM(n_layers, n_units, n_units, 0.1) self.W = L.Linear(n_units, n_target_vocab) self.n_layers = n_layers self.n_units = n_units
Example #28
Source File: net.py From models with MIT License | 5 votes |
def __init__(self, n_vocab=None, emb_dim=256, hidden_dim=1024, use_dropout=0.50, n_layers=1, hidden_classifier=30, use_adv=0, xi_var=5.0, n_class=2, args=None): self.args = args super(uniLSTM_VAT, self).__init__( word_embed = L.EmbedID(n_vocab, emb_dim, ignore_label=-1), hidden_layer=L.Linear(hidden_dim, hidden_classifier), output_layer=L.Linear(hidden_classifier, n_class) ) uni_lstm = L.NStepLSTM(n_layers=n_layers, in_size=emb_dim, out_size=hidden_dim, dropout=use_dropout) # Forget gate bias => 1.0 # MEMO: Values 1 and 5 reference the forget gate. for w in uni_lstm: w.b1.data[:] = 1.0 w.b5.data[:] = 1.0 self.add_link('uni_lstm', uni_lstm) self.hidden_dim = hidden_dim self.train = True self.use_dropout = use_dropout self.n_layers = n_layers self.use_adv = use_adv self.xi_var = xi_var self.n_vocab = n_vocab self.grad_scale = None
Example #29
Source File: seq2seq_mp1.py From chainer with MIT License | 5 votes |
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
Example #30
Source File: seq2seq_mp1.py From chainer with MIT License | 5 votes |
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