Python chainer.links.LSTM Examples

The following are 30 code examples of chainer.links.LSTM(). 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 chainer with MIT License 6 votes vote down vote up
def __init__(self, vocab_size, hidden_size, dropout_ratio, ignore_label):
        super(LSTMLanguageModel, 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.LSTM(hidden_size, hidden_size)
            self.out_word = L.Linear(
                hidden_size,
                vocab_size,
                initialW=initializers.Normal(0.01)
            )

        self.dropout_ratio = dropout_ratio 
Example #2
Source File: model.py    From pfio with MIT License 6 votes vote down vote up
def step(self, hx, cx, xs):
        """Batch of word tokens to word tokens and hidden LSTM states.

        Predict the next set of tokens given previous tokens.
        """
        # Concatenate all input captions and pass them through the model in a
        # single pass
        caption_lens = [len(x) for x in xs]
        caption_sections = np.cumsum(caption_lens[:-1])
        xs = F.concat(xs, axis=0)

        xs = self.embed_word(xs)
        xs = F.split_axis(xs, caption_sections, axis=0)
        hx, cx, ys = self.lstm(hx, cx, xs)

        ys = F.concat(ys, axis=0)
        ys = F.dropout(ys, self.dropout_ratio)
        ys = self.decode_caption(ys)
        return hx, cx, ys 
Example #3
Source File: model.py    From pfio with MIT License 6 votes vote down vote up
def __init__(self, vocab_size, hidden_size, dropout_ratio, ignore_label):
        super(LSTMLanguageModel, 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.LSTM(hidden_size, hidden_size)
            self.out_word = L.Linear(
                hidden_size,
                vocab_size,
                initialW=initializers.Normal(0.01)
            )

        self.dropout_ratio = dropout_ratio 
Example #4
Source File: fsns.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, target_shape, num_labels, num_timesteps, uses_original_data=False, dropout_ratio=0.5, use_dropout=False):
        super(FSNSRecognitionNet, self).__init__()
        with self.init_scope():
            self.conv0 = L.Convolution2D(None, 32, 3, pad=1, stride=2)
            self.bn0 = L.BatchNormalization(32)
            self.conv1 = L.Convolution2D(32, 32, 3, pad=1)
            self.bn1 = L.BatchNormalization(32)
            self.rs1 = ResnetBlock(32, use_dropout=use_dropout, dropout_ratio=dropout_ratio)
            self.rs2 = ResnetBlock(64, filter_increase=True, use_dropout=use_dropout, dropout_ratio=dropout_ratio)
            self.rs3 = ResnetBlock(128, filter_increase=True, use_dropout=use_dropout, dropout_ratio=dropout_ratio)
            self.fc1 = L.Linear(None, 256)
            self.lstm = L.LSTM(None, 256)
            self.classifier = L.Linear(None, 134)

        self._train = True
        self.target_shape = target_shape
        self.num_labels = num_labels
        self.num_timesteps = num_timesteps
        self.uses_original_data = uses_original_data
        self.vis_anchor = None
        self.use_dropout = use_dropout
        self.dropout_ratio = dropout_ratio 
Example #5
Source File: fsns.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, target_shape, num_labels, num_timesteps, uses_original_data=False, dropout_ratio=0.5, use_dropout=False, use_blstm=False):
        super().__init__()
        with self.init_scope():
            self.conv0 = L.Convolution2D(None, 32, 3, pad=1)
            self.bn0 = L.BatchNormalization(32)
            self.rs1 = ResnetBlock(32)
            self.rs2 = ResnetBlock(64, filter_increase=True, use_dropout=use_dropout, dropout_ratio=dropout_ratio)
            self.rs3 = ResnetBlock(128, filter_increase=True, use_dropout=use_dropout, dropout_ratio=dropout_ratio)
            self.fc1 = L.Linear(None, 256)
            self.lstm = L.LSTM(None, 256)
            if use_blstm:
                self.blstm = L.LSTM(None, 256)
            self.classifier = L.Linear(None, 134)

        self._train = True
        self.target_shape = target_shape
        self.num_labels = num_labels
        self.num_timesteps = num_timesteps
        self.uses_original_data = uses_original_data
        self.vis_anchor = None
        self.use_dropout = use_dropout
        self.dropout_ratio = dropout_ratio
        self.use_blstm = use_blstm 
Example #6
Source File: fsns_resnet.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, target_shape, num_labels, num_timesteps, uses_original_data=False, dropout_ratio=0.5, use_dropout=False, use_blstm=False, use_attention=False):
        super().__init__()
        with self.init_scope():
            self.resnet = FSNSResNetLayers('', 152)
            self.fc1 = L.Linear(None, 512)
            self.lstm = L.LSTM(None, 512)
            if use_blstm:
                self.blstm = L.LSTM(None, 512)
            self.classifier = L.Linear(None, 134)

        self.target_shape = target_shape
        self.num_labels = num_labels
        self.num_timesteps = num_timesteps
        self.uses_original_data = uses_original_data
        self.vis_anchor = None
        self.use_dropout = use_dropout
        self.dropout_ratio = dropout_ratio
        self.use_blstm = use_blstm
        self.use_attention = use_attention 
Example #7
Source File: model.py    From chainer with MIT License 6 votes vote down vote up
def step(self, hx, cx, xs):
        """Batch of word tokens to word tokens and hidden LSTM states.

        Predict the next set of tokens given previous tokens.
        """
        # Concatenate all input captions and pass them through the model in a
        # single pass
        caption_lens = [len(x) for x in xs]
        caption_sections = np.cumsum(caption_lens[:-1])
        xs = F.concat(xs, axis=0)

        xs = self.embed_word(xs)
        xs = F.split_axis(xs, caption_sections, axis=0)
        hx, cx, ys = self.lstm(hx, cx, xs)

        ys = F.concat(ys, axis=0)
        ys = F.dropout(ys, self.dropout_ratio)
        ys = self.decode_caption(ys)
        return hx, cx, ys 
Example #8
Source File: svhn.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, dropout_ratio, num_timesteps, zoom=0.9):
        super(SVHNLocalizationNet, self).__init__()
        with self.init_scope():
            self.conv0 = L.Convolution2D(None, 32, 3, pad=1)
            self.bn0 = L.BatchNormalization(32)
            self.rs1 = ResnetBlock(32)
            self.rs2 = ResnetBlock(48, filter_increase=True)
            self.rs3 = ResnetBlock(48)
            self.lstm = L.LSTM(None, 256)
            self.transform_2 = L.Linear(256, 6)

        # initialize transform
        self.transform_2.W.data[...] = 0

        transform_bias = self.transform_2.b.data
        transform_bias[[0, 4]] = zoom
        transform_bias[[2, 5]] = 0

        self.dropout_ratio = dropout_ratio
        self._train = True
        self.num_timesteps = num_timesteps
        self.vis_anchor = None

        self.width_encoding = None
        self.height_encoding = None 
Example #9
Source File: svhn.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, target_shape, num_labels, num_timesteps, use_blstm=False):
        super(SVHNRecognitionNet, self).__init__()
        with self.init_scope():
            self.data_bn = L.BatchNormalization(3)
            self.conv0 = L.Convolution2D(None, 32, 3, pad=1, stride=2)
            self.bn0 = L.BatchNormalization(32)
            self.conv1 = L.Convolution2D(32, 32, 3, pad=1)
            self.bn1 = L.BatchNormalization(32)
            self.rs1 = ResnetBlock(32)
            self.rs2 = ResnetBlock(64, filter_increase=True)
            self.rs3 = ResnetBlock(128, filter_increase=True)
            self.fc1 = L.Linear(None, 256)
            self.lstm = L.LSTM(None, 256)
            if use_blstm:
                self.blstm = L.LSTM(None, 256)
            self.classifier = L.Linear(None, 11)

        self._train = True
        self.target_shape = target_shape
        self.num_labels = num_labels
        self.num_timesteps = num_timesteps
        self.vis_anchor = None
        self.use_blstm = use_blstm 
Example #10
Source File: svhn.py    From see with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, target_shape, num_labels, num_timesteps):
        super(SVHNCTCRecognitionNet, self).__init__()
        with self.init_scope():
            self.data_bn = L.BatchNormalization(3)
            self.conv0 = L.Convolution2D(None, 32, 3, pad=1)
            self.bn0 = L.BatchNormalization(32)
            self.rs1 = ResnetBlock(32)
            self.rs2 = ResnetBlock(64, filter_increase=True)
            self.rs3 = ResnetBlock(128, filter_increase=True)
            self.fc1 = L.Linear(None, 256)
            self.lstm = L.LSTM(None, 256)
            self.classifier = L.Linear(None, 11)

        self._train = True
        self.target_shape = target_shape
        self.num_labels = num_labels
        self.num_timesteps = num_timesteps
        self.vis_anchor = None 
Example #11
Source File: subword.py    From vecto with Mozilla Public License 2.0 6 votes vote down vote up
def __init__(self, vocab, vocab_ngram_tokens, n_units, n_units_char, dropout,
                 subword):  # dropout ratio, zero indicates no dropout
        super(RNN, self).__init__()
        with self.init_scope():
            self.embed = L.EmbedID(
                len(vocab_ngram_tokens.lst_words) + 2, n_units_char,
                initialW=I.Uniform(1. / n_units_char))  # ngram tokens embedding  plus 2 for OOV and end symbol.
            if 'lstm' in subword:
                self.mid = L.LSTM(n_units_char, n_units_char * 2)
            self.out = L.Linear(n_units_char * 2, n_units_char)  # the feed-forward output layer
            if 'bilstm' in subword:
                self.mid_b = L.LSTM(n_units_char, n_units_char * 2)
                self.out_b = L.Linear(n_units_char * 2, n_units_char)

            self.n_ngram = vocab_ngram_tokens.metadata["max_gram"] - vocab_ngram_tokens.metadata["min_gram"] + 1
            self.final_out = L.Linear(n_units * (self.n_ngram), n_units)

            self.dropout = dropout
            self.vocab = vocab
            self.vocab_ngram_tokens = vocab_ngram_tokens
            self.subword = subword 
Example #12
Source File: rnn_network.py    From turf-tipster with GNU Affero General Public License v3.0 6 votes vote down vote up
def __init__(self, n_horses, n_jockeys):
		n_units_h, n_units_v, n_units_j, n_units_r, n_units_d = nn_nodes
		super(Turf_Tipster_NN, self).__init__()
		with self.init_scope():
			# 馬モデルのステータス(モデルを保存できるようにParameterで保持しておく)
			self.vc = chainer.Parameter(np.zeros((n_horses, n_units_v), dtype=np.float32))
			self.vh = chainer.Parameter(np.zeros((n_horses, n_units_v), dtype=np.float32))
			# 馬モデルのレイヤー
			self.le = L.EmbedID(n_horses, n_horses)
			self.l1 = L.Linear(n_horses, n_units_h)
			self.l2 = L.LSTM(n_units_h, n_units_v)
			# 騎手モデルのレイヤー
			self.re = L.EmbedID(n_jockeys, n_jockeys)
			self.r1 = L.Linear(n_jockeys, n_units_j)
			# レースモデルのレイヤー
			self.m1 = L.EmbedID(11, 11)
			self.m2 = L.EmbedID(8, 8)
			self.m3 = L.EmbedID(4, 4)
			self.m4 = L.EmbedID(4, 4)
			self.j1 = L.Linear(n_units_v + n_units_j + 11 + 8 + 4 + 4, n_units_r)
			self.j2 = L.Linear(n_units_r, n_units_r)
			self.j3 = L.Linear(n_units_r, n_units_d)

	# 引数は(レースメタ情報, グリッド情報)で着順になっている 
Example #13
Source File: basetest_pgt.py    From chainerrl with MIT License 6 votes vote down vote up
def make_model(self, env):
        n_dim_obs = env.observation_space.low.size
        n_dim_action = env.action_space.low.size
        n_hidden_channels = 50
        policy = Sequence(
            L.Linear(n_dim_obs, n_hidden_channels),
            F.relu,
            L.Linear(n_hidden_channels, n_hidden_channels),
            F.relu,
            L.LSTM(n_hidden_channels, n_hidden_channels),
            policies.FCGaussianPolicy(
                n_input_channels=n_hidden_channels,
                action_size=n_dim_action,
                min_action=env.action_space.low,
                max_action=env.action_space.high)
        )

        q_func = q_function.FCLSTMSAQFunction(
            n_dim_obs=n_dim_obs,
            n_dim_action=n_dim_action,
            n_hidden_layers=2,
            n_hidden_channels=n_hidden_channels)

        return chainer.Chain(policy=policy, q_function=q_func) 
Example #14
Source File: optnets.py    From models with MIT License 5 votes vote down vote up
def __init__(self, n_units=20, n_classes=10, out_scale=0.1,
                 do_preprocess=True):
        super(LSTMOptNet, self).__init__()
        with self.init_scope():
            n_input = 2 if do_preprocess else 1
            self.l1 = L.LSTM(n_input, 20, forget_bias_init=0)
            self.l2 = L.LSTM(20, 20, forget_bias_init=0)
            self.lout = L.Linear(20, 1)
            # self.ldirect = L.Linear(n_input, 1)
        self.do_preprocess = do_preprocess
        self.out_scale = out_scale 
Example #15
Source File: context_models.py    From context2vec with Apache License 2.0 5 votes vote down vote up
def __init__(self, deep, gpu, word2index, in_units, hidden_units, out_units, loss_func, train, drop_ratio=0.0):
        n_vocab = len(word2index)        
        l2r_embedding=L.EmbedID(n_vocab, in_units)
        r2l_embedding=L.EmbedID(n_vocab, in_units)
        
        if deep:
            super(BiLstmContext, self).__init__(
                l2r_embed=l2r_embedding,
                r2l_embed=r2l_embedding,
                loss_func=loss_func,
                l2r_1 = L.LSTM(in_units, hidden_units),
                r2l_1 = L.LSTM(in_units, hidden_units),
                l3 = L.Linear(2*hidden_units, 2*hidden_units),
                l4 = L.Linear(2*hidden_units, out_units),
            )
        else:
            super(BiLstmContext, self).__init__(
                l2r_embed=l2r_embedding,
                r2l_embed=r2l_embedding,
                loss_func=loss_func,
                l2r_1 = L.LSTM(in_units, hidden_units),
                r2l_1 = L.LSTM(in_units, hidden_units),
                lp_l2r = L.Linear(hidden_units, int(out_units/2)),
                lp_r2l = L.Linear(hidden_units, int(out_units/2))
                
            )
        if gpu >=0:
            self.to_gpu()
        l2r_embedding.W.data = self.xp.random.normal(0, math.sqrt(1. / l2r_embedding.W.data.shape[0]), l2r_embedding.W.data.shape).astype(np.float32)       
        r2l_embedding.W.data = self.xp.random.normal(0, math.sqrt(1. / r2l_embedding.W.data.shape[0]), r2l_embedding.W.data.shape).astype(np.float32)
        
        self.word2index = word2index
        self.train = train
        self.deep = deep
        self.drop_ratio = drop_ratio 
Example #16
Source File: lstm.py    From deel with MIT License 5 votes vote down vote up
def __init__(self, n_input_units=1000,n_vocab=100, n_units=100, train=True):
        super(RNNLM, self).__init__(
            inputVector= L.Linear(n_input_units, n_units),
            embed=L.EmbedID(n_vocab, n_units),
            l1=L.LSTM(n_units, n_units),
            l2=L.LSTM(n_units, n_units),
            l3=L.Linear(n_units, n_vocab),
        )
        self.train = train 
Example #17
Source File: ram.py    From ram with MIT License 5 votes vote down vote up
def __init__(
        self, g_size=8, n_steps=6, n_scales=1, var=0.03, use_lstm=False
    ):
        d_glm = 128
        d_core = 256
        super(RAM, self).__init__(
            emb_l=L.Linear(2, d_glm),
            emb_x=L.Linear(g_size*g_size*n_scales, d_glm),
            fc_lg=L.Linear(d_glm, d_core),
            fc_xg=L.Linear(d_glm, d_core),
            fc_ha=L.Linear(d_core, 10),
            fc_hl=L.Linear(d_core, 2),
            fc_hb=L.Linear(d_core, 1),
        )

        if use_lstm:
            self.add_link(name='core_lstm', link=L.LSTM(d_core, d_core))
        else:
            self.add_link(name='core_hh', link=L.Linear(d_core, d_core))
            self.add_link(name='core_gh', link=L.Linear(d_core, d_core))

        self.use_lstm = use_lstm
        self.d_core = d_core
        self.g_size = g_size
        self.n_steps = n_steps
        self.n_scales = n_scales
        self.var = var 
Example #18
Source File: lstm_text_localizer.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        with self.init_scope():
            self.lstm = L.LSTM(None, 1024)

            self.param_predictor = L.Linear(1024, 6)
            # params = self.param_predictor.b.data
            # params[...] = 0
            # params[[0, 4]] = 0.8
            # self.param_predictor.W.data[...] = 0 
Example #19
Source File: text_recognizer.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, num_chars, num_classes, **kwargs):
        super().__init__()

        with self.init_scope():
            self.feature_extractor = ResNet(kwargs.pop('num_layers', 18))
            # self.lstm = L.LSTM(None, 1024)
            self.classifier = L.Linear(None, num_classes)

        self.num_chars = num_chars
        chainer.global_config.user_text_recognition_grayscale_input = False 
Example #20
Source File: dern.py    From der-network with MIT License 5 votes vote down vote up
def __init__(self, vocab, args):
        def get_initialW_X(shape):
            return np.random.normal(0, (2.0/(sum(shape)))**0.5, shape).astype(np.float32)

        super(DERN, self).__init__(
            # Word Embedding
            embed=L.EmbedID(len(vocab), args.n_units),

            # bi-LSTMs
            f_LSTM=L.LSTM(args.n_units, args.n_units),  # for article
            b_LSTM=L.LSTM(args.n_units, args.n_units),
            Q_f_LSTM=L.LSTM(args.n_units, args.n_units),  # for query
            Q_b_LSTM=L.LSTM(args.n_units, args.n_units),

            # Matrices and vectors
            W_hd=L.Linear(4*args.n_units, args.n_units, initialW=get_initialW_X((args.n_units, 4*args.n_units))),
            W_dm=L.Linear(args.n_units, args.n_units, initialW=get_initialW_X((args.n_units, args.n_units))),
            m=L.Linear(args.n_units, 1, initialW=get_initialW_X((1, args.n_units))),
            W_hq=L.Linear(4 * args.n_units, args.n_units, initialW=get_initialW_X((args.n_units, 4*args.n_units))),
            W_hu=L.Linear(4 * args.n_units, args.n_units, initialW=get_initialW_X((args.n_units, 4*args.n_units))),
            W_dv=L.Linear(args.n_units, args.n_units, initialW=get_initialW_X((args.n_units, args.n_units))),
            W_dx=L.Linear(args.n_units, args.n_units, initialW=get_initialW_X((args.n_units, args.n_units))),
            W_dxQ=L.Linear(args.n_units, args.n_units, initialW=get_initialW_X((args.n_units, args.n_units))),

            b_v2=L.Linear(1, args.n_units, initialW=get_initialW_X((args.n_units, 1)))
        )

        self.args = args
        self.n_vocab = len(vocab)
        self.n_units = args.n_units
        self.dropout_ratio = args.d_ratio

        self.PH_id = vocab["@placeholder"]
        self.eos_id = vocab["<eos>"]
        self.bos_id = vocab["<bos>"]
        self.boq_id = vocab["<boq>"]
        self.BOQ_tok_batch = self.xp.array([self.boq_id], dtype=np.int32)
        self.NULL_id = vocab["NULL_tok"]
        self.NULL_tok = self.xp.array(self.NULL_id, dtype=np.int32)

        self.initialize_additionally() 
Example #21
Source File: ic_stn.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, dropout_ratio, num_timesteps, num_refinement_steps, target_shape, zoom=0.9, do_parameter_refinement=False):
        super().__init__()
        with self.init_scope():
            self.conv0 = L.Convolution2D(None, 32, 3, pad=1)
            self.bn0 = L.BatchNormalization(32)
            self.conv0_1 = L.Convolution2D(None, 32, 3, pad=1)
            self.bn0_1 = L.BatchNormalization(32)
            self.rs1 = ResnetBlock(32)
            self.rs2 = ResnetBlock(48, filter_increase=True)
            self.rs3 = ResnetBlock(48)
            self.rs4 = ResnetBlock(32)
            self.rs5 = ResnetBlock(48, filter_increase=True)
            self.lstm = L.LSTM(None, 256)
            self.transform_2 = L.Linear(256, 6)
            self.refinement_transform = L.Linear(2352, 6)

            for transform_param_layer in [self.transform_2, self.refinement_transform]:
                # initialize transform
                transform_param_layer.W.data[...] = 0

                transform_bias = transform_param_layer.b.data
                transform_bias[[0, 4]] = zoom
                transform_bias[[2, 5]] = 0

            self.refinement_transform.b.data[[0, 4]] = 0.1

            self.dropout_ratio = dropout_ratio
            self.num_timesteps = num_timesteps
            self.num_refinement_steps = num_refinement_steps
            self.target_shape = target_shape
            self.do_parameter_refinement = do_parameter_refinement
            self.vis_anchor = None 
Example #22
Source File: fsns.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, target_shape, num_labels, num_timesteps, uses_original_data=False, dropout_ratio=0.5, use_dropout=False, use_blstm=False, use_attention=False):
        super().__init__()
        with self.init_scope():
            self.data_bn = L.BatchNormalization(3)
            self.conv0 = L.Convolution2D(None, 64, 7, stride=2, pad=3, nobias=True)
            self.bn0 = L.BatchNormalization(64)
            self.rs1_1 = ResnetBlock(64)
            self.rs1_2 = ResnetBlock(64)
            self.rs2_1 = ResnetBlock(128, filter_increase=True)
            self.rs2_2 = ResnetBlock(128)
            self.rs3_1 = ResnetBlock(256, filter_increase=True)
            self.rs3_2 = ResnetBlock(256)
            self.rs4_1 = ResnetBlock(512, filter_increase=True)
            self.rs4_2 = ResnetBlock(512)
            self.fc1 = L.Linear(None, 512)
            self.lstm = L.LSTM(None, 512)
            if use_blstm:
                self.blstm = L.LSTM(None, 512)
            if use_attention:
                self.transform_encoded_features = L.Linear(512, 512, nobias=True)
                self.transform_out_lstm_feature = L.Linear(512, 512, nobias=True)
                self.generate_attended_feat = L.Linear(512, 1)
                self.out_lstm = L.LSTM(512, 512)
            self.classifier = L.Linear(None, 134)

        self._train = True
        self.target_shape = target_shape
        self.num_labels = num_labels
        self.num_timesteps = num_timesteps
        self.uses_original_data = uses_original_data
        self.vis_anchor = None
        self.use_dropout = use_dropout
        self.dropout_ratio = dropout_ratio
        self.use_blstm = use_blstm
        self.use_attention = use_attention 
Example #23
Source File: dern.py    From der-network with MIT License 5 votes vote down vote up
def encode_tokens(self, x_datas, i2sD, train=True):
        # Embed, dropout, split into each token (batchsize=1)
        h0L = list(F.split_axis(
            F.dropout(
                self.embed(chainer.Variable(self.xp.array(x_datas, dtype=np.int32), volatile=not train)),
                ratio=self.dropout_ratio, train=train), len(x_datas), axis=0))

        # Replace embedding with dynamic entity representation
        for i in i2sD.keys():
            h0L[i] = self.W_dx(i2sD[i])

        # LSTM. forward order
        forward_outL = []
        self.f_LSTM.reset_state()
        for h0 in h0L:
            state = self.f_LSTM(h0)
            forward_outL.append(state)

        # LSTM. backward order
        backward_outL = []
        self.b_LSTM.reset_state()
        for h0 in reversed(h0L):
            state = self.b_LSTM(h0)
            backward_outL.append(state)

        return forward_outL, backward_outL 
Example #24
Source File: fsns.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, dropout_ratio, num_timesteps, zoom=0.9, use_dropout=False):
        super(FSNSSingleSTNLocalizationNet, self).__init__()
        with self.init_scope():
            self.conv0 = L.Convolution2D(None, 32, 3, pad=1)
            self.bn0 = L.BatchNormalization(32)
            self.rs1 = ResnetBlock(32, use_dropout=use_dropout, dropout_ratio=dropout_ratio)
            self.rs2 = ResnetBlock(48, filter_increase=True, use_dropout=use_dropout, dropout_ratio=dropout_ratio)
            self.rs3 = ResnetBlock(48)
            # self.rs4 = ResnetBlock(16, filter_increase=True)
            self.lstm = L.LSTM(None, 256)
            self.transform_2 = L.LSTM(256, 6)

        self.dropout_ratio = dropout_ratio
        self.use_dropout = use_dropout
        self._train = True
        self.num_timesteps = num_timesteps

        # initialize transform
        # self.transform_2.W.data[...] = 0
        #
        # transform_bias = self.transform_2.b.data
        # transform_bias[[0, 4]] = zoom
        # transform_bias[[2, 5]] = 0

        self.visual_backprop = VisualBackprop()
        self.vis_anchor = None

        self.width_encoding = None
        self.height_encoding = None 
Example #25
Source File: fsns.py    From see with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, dropout_factor, num_timesteps, zoom=0.9):
        super(FSNSMultipleSTNLocalizationNet, self).__init__()
        with self.init_scope():
            self.conv0 = L.Convolution2D(None, 32, 3, pad=1)
            self.bn0 = L.BatchNormalization(32)
            self.rs1 = ResnetBlock(32)
            self.rs2 = ResnetBlock(48, filter_increase=True)
            self.rs3 = ResnetBlock(48)
            self.lstm = L.LSTM(None, 256)
            self.translation_transform = L.Linear(256, 6)
            self.rotation_transform = L.Linear(256, 6)
            self.transform_2 = L.LSTM(256, 6)

        self.dropout_factor = dropout_factor
        self._train = True
        self.num_timesteps = num_timesteps

        for transform in [self.translation_transform, self.rotation_transform]:
            transform_bias = transform.b.data
            transform_bias[[0, 4]] = zoom
            transform_bias[[2, 5]] = 0
            transform.W.data[...] = 0

        # self.transform_2.upward.b.data[...] = 0
        # self.transform_2.upward.W.data[...] = 0
        # self.transform_2.lateral.W.data[...] = 0

        # self.transform.W.data[...] = 0

        self.visual_backprop = VisualBackprop()
        self.vis_anchor = None 
Example #26
Source File: train_a3c_ale.py    From chainerrl with MIT License 5 votes vote down vote up
def __init__(self, n_actions):
        self.head = links.NIPSDQNHead()
        self.pi = policy.FCSoftmaxPolicy(
            self.head.n_output_channels, n_actions)
        self.v = v_function.FCVFunction(self.head.n_output_channels)
        self.lstm = L.LSTM(self.head.n_output_channels,
                           self.head.n_output_channels)
        super().__init__(self.head, self.lstm, self.pi, self.v) 
Example #27
Source File: train_ptb_custom_loop.py    From chainer with MIT License 5 votes vote down vote up
def __init__(self, n_vocab, n_units):
        super(RNNForLMSlice, self).__init__()
        with self.init_scope():
            self.embed = L.EmbedID(n_vocab, n_units)
            self.l1 = L.LSTM(n_units, n_units)
            self.l2 = L.LSTM(n_units, n_units)
            self.l3 = L.Linear(n_units, n_vocab)

        for param in self.params():
            param.array[...] = np.random.uniform(-0.1, 0.1, param.shape) 
Example #28
Source File: train_a3c_gym.py    From chainerrl with MIT License 5 votes vote down vote up
def __init__(self, obs_size, action_size, hidden_size=200, lstm_size=128):
        self.pi_head = L.Linear(obs_size, hidden_size)
        self.v_head = L.Linear(obs_size, hidden_size)
        self.pi_lstm = L.LSTM(hidden_size, lstm_size)
        self.v_lstm = L.LSTM(hidden_size, lstm_size)
        self.pi = policies.FCGaussianPolicy(lstm_size, action_size)
        self.v = v_function.FCVFunction(lstm_size)
        super().__init__(self.pi_head, self.v_head,
                         self.pi_lstm, self.v_lstm, self.pi, self.v) 
Example #29
Source File: state_action_q_functions.py    From chainerrl with MIT License 5 votes vote down vote up
def __init__(self, n_dim_obs, n_dim_action, n_hidden_channels,
                 n_hidden_layers, nonlinearity=F.relu, last_wscale=1.):
        self.n_input_channels = n_dim_obs + n_dim_action
        self.n_hidden_layers = n_hidden_layers
        self.n_hidden_channels = n_hidden_channels
        self.nonlinearity = nonlinearity
        super().__init__()
        with self.init_scope():
            self.fc = MLP(self.n_input_channels, n_hidden_channels,
                          [self.n_hidden_channels] * self.n_hidden_layers,
                          nonlinearity=nonlinearity,
                          )
            self.lstm = L.LSTM(n_hidden_channels, n_hidden_channels)
            self.out = L.Linear(n_hidden_channels, 1,
                                initialW=LeCunNormal(last_wscale)) 
Example #30
Source File: state_q_functions.py    From chainerrl with MIT License 5 votes vote down vote up
def __init__(self, n_dim_obs, n_dim_action, n_hidden_channels,
                 n_hidden_layers):
        self.n_input_channels = n_dim_obs
        self.n_hidden_layers = n_hidden_layers
        self.n_hidden_channels = n_hidden_channels
        self.state_stack = []
        super().__init__()
        with self.init_scope():
            self.fc = MLP(in_size=self.n_input_channels,
                          out_size=n_hidden_channels,
                          hidden_sizes=[self.n_hidden_channels] *
                          self.n_hidden_layers)
            self.lstm = L.LSTM(n_hidden_channels, n_hidden_channels)
            self.out = L.Linear(n_hidden_channels, n_dim_action)