Python torch.nn.GRUCell() Examples
The following are 30
code examples of torch.nn.GRUCell().
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
torch.nn
, or try the search function
.
Example #1
Source File: 5_dgmg.py From dgl with Apache License 2.0 | 7 votes |
def __init__(self, num_prop_rounds, node_hidden_size): super(GraphProp, self).__init__() self.num_prop_rounds = num_prop_rounds # Setting from the paper self.node_activation_hidden_size = 2 * node_hidden_size message_funcs = [] node_update_funcs = [] self.reduce_funcs = [] for t in range(num_prop_rounds): # input being [hv, hu, xuv] message_funcs.append(nn.Linear(2 * node_hidden_size + 1, self.node_activation_hidden_size)) self.reduce_funcs.append(partial(self.dgmg_reduce, round=t)) node_update_funcs.append( nn.GRUCell(self.node_activation_hidden_size, node_hidden_size)) self.message_funcs = nn.ModuleList(message_funcs) self.node_update_funcs = nn.ModuleList(node_update_funcs)
Example #2
Source File: model_batch.py From dgl with Apache License 2.0 | 7 votes |
def __init__(self, num_prop_rounds, node_hidden_size): super(GraphProp, self).__init__() self.num_prop_rounds = num_prop_rounds # Setting from the paper self.node_activation_hidden_size = 2 * node_hidden_size message_funcs = [] node_update_funcs = [] self.reduce_funcs = [] for t in range(num_prop_rounds): # input being [hv, hu, xuv] message_funcs.append(nn.Linear(2 * node_hidden_size + 1, self.node_activation_hidden_size)) self.reduce_funcs.append(partial(self.dgmg_reduce, round=t)) node_update_funcs.append( nn.GRUCell(self.node_activation_hidden_size, node_hidden_size)) self.message_funcs = nn.ModuleList(message_funcs) self.node_update_funcs = nn.ModuleList(node_update_funcs)
Example #3
Source File: module.py From Tacotron-pytorch with Apache License 2.0 | 6 votes |
def __init__(self, num_units): """ :param num_units: dimension of hidden units """ super(AttentionDecoder, self).__init__() self.num_units = num_units self.v = nn.Linear(num_units, 1, bias=False) self.W1 = nn.Linear(num_units, num_units, bias=False) self.W2 = nn.Linear(num_units, num_units, bias=False) self.attn_grucell = nn.GRUCell(num_units // 2, num_units) self.gru1 = nn.GRUCell(num_units, num_units) self.gru2 = nn.GRUCell(num_units, num_units) self.attn_projection = nn.Linear(num_units * 2, num_units) self.out = nn.Linear(num_units, hp.num_mels * hp.outputs_per_step)
Example #4
Source File: model.py From conv-emotion with MIT License | 6 votes |
def __init__(self, D_m, D_g, D_p, D_e, listener_state=False, context_attention='simple', D_a=100, dropout=0.5): super(DialogueRNNCell, self).__init__() self.D_m = D_m self.D_g = D_g self.D_p = D_p self.D_e = D_e self.listener_state = listener_state self.g_cell = nn.GRUCell(D_m+D_p,D_g) self.p_cell = nn.GRUCell(D_m+D_g,D_p) self.e_cell = nn.GRUCell(D_p,D_e) if listener_state: self.l_cell = nn.GRUCell(D_m+D_p,D_p) self.dropout = nn.Dropout(dropout) if context_attention=='simple': self.attention = SimpleAttention(D_g) else: self.attention = MatchingAttention(D_g, D_m, D_a, context_attention)
Example #5
Source File: model.py From conv-emotion with MIT License | 6 votes |
def __init__(self, D_m, D_g, D_p, D_e, listener_state=False, context_attention='simple', D_a=100, dropout=0.5): super(DialogueRNNCell, self).__init__() self.D_m = D_m self.D_g = D_g self.D_p = D_p self.D_e = D_e self.listener_state = listener_state self.g_cell = nn.GRUCell(D_m+D_p,D_g) self.p_cell = nn.GRUCell(D_m+D_g,D_p) self.e_cell = nn.GRUCell(D_p,D_e) if listener_state: self.l_cell = nn.GRUCell(D_m+D_p,D_p) self.dropout = nn.Dropout(dropout) if context_attention=='simple': self.attention = SimpleAttention(D_g) else: self.attention = MatchingAttention(D_g, D_m, D_a, context_attention)
Example #6
Source File: models.py From Multiple-Object-Forecasting with MIT License | 6 votes |
def __init__(self, device, num_hidden, dropout_p, num_layers): super(DecoderRNN, self).__init__() self.num_hidden = num_hidden self.num_layers = num_layers self.decoder1 = nn.GRUCell(self.num_hidden, self.num_hidden) self.out = nn.Linear(self.num_hidden, 4) self.dropout_context = nn.Dropout(p=dropout_p) self.dropout_dtp_features = nn.Dropout(p=dropout_p) self.relu_context = nn.ReLU() self.relu_dtp_features = nn.ReLU() self.device = device self.context_encoder = nn.Linear(self.num_hidden, int(self.num_hidden / 2)) self.dtp_encoder = nn.Linear(2048, int(self.num_hidden / 2)) if num_layers > 1: self.decoder2 = nn.GRUCell(self.num_hidden, self.num_hidden) if num_layers > 2: self.decoder3 = nn.GRUCell(self.num_hidden, self.num_hidden)
Example #7
Source File: san_decoder.py From converse_reading_cmr with MIT License | 6 votes |
def __init__(self, x_size, h_size, opt={}, prefix='answer', dropout=None): super(SANDecoder, self).__init__() self.prefix = prefix self.attn_b = FlatSimilarityWrapper(x_size, h_size, prefix, opt, dropout) self.attn_e = FlatSimilarityWrapper(x_size, h_size, prefix, opt, dropout) self.rnn_type = opt.get('{}_rnn_type'.format(prefix), 'gru') self.rnn =RNN_MAP.get(self.rnn_type, nn.GRUCell)(x_size, h_size) self.num_turn = opt.get('{}_num_turn'.format(prefix), 5) self.opt = opt self.mem_random_drop = opt.get('{}_mem_drop_p'.format(prefix), 0) self.answer_opt = opt.get('{}_opt'.format(prefix), 0) # 0: std mem; 1: random select step; 2 random selection; voting in pred; 3:sort merge self.mem_type = opt.get('{}_mem_type'.format(prefix), 0) self.gamma = opt.get('{}_mem_gamma'.format(prefix), 0.5) self.alpha = Parameter(torch.zeros(1, 1, 1)) self.proj = nn.Linear(h_size, x_size) if h_size != x_size else None if dropout is None: self.dropout = DropoutWrapper(opt.get('{}_dropout_p'.format(self.prefix), 0)) else: self.dropout = dropout self.h2h = nn.Linear(h_size, h_size) self.a2h = nn.Linear(x_size, h_size, bias=False) self.luong_output_layer = nn.Linear(h_size + x_size, h_size)
Example #8
Source File: gatedgraphconv.py From dgl with Apache License 2.0 | 6 votes |
def __init__(self, in_feats, out_feats, n_steps, n_etypes, bias=True): super(GatedGraphConv, self).__init__() self._in_feats = in_feats self._out_feats = out_feats self._n_steps = n_steps self._n_etypes = n_etypes self.linears = nn.ModuleList( [nn.Linear(out_feats, out_feats) for _ in range(n_etypes)] ) self.gru = nn.GRUCell(out_feats, out_feats, bias=bias) self.reset_parameters()
Example #9
Source File: model.py From dgl with Apache License 2.0 | 6 votes |
def __init__(self, num_prop_rounds, node_hidden_size): super(GraphProp, self).__init__() self.num_prop_rounds = num_prop_rounds # Setting from the paper self.node_activation_hidden_size = 2 * node_hidden_size message_funcs = [] self.reduce_funcs = [] node_update_funcs = [] for t in range(num_prop_rounds): # input being [hv, hu, xuv] message_funcs.append(nn.Linear(2 * node_hidden_size + 1, self.node_activation_hidden_size)) self.reduce_funcs.append(partial(self.dgmg_reduce, round=t)) node_update_funcs.append( nn.GRUCell(self.node_activation_hidden_size, node_hidden_size)) self.message_funcs = nn.ModuleList(message_funcs) self.node_update_funcs = nn.ModuleList(node_update_funcs)
Example #10
Source File: utils.py From dgl with Apache License 2.0 | 6 votes |
def weights_init(m): ''' Code from https://gist.github.com/jeasinema/ed9236ce743c8efaf30fa2ff732749f5 Usage: model = Model() model.apply(weight_init) ''' if isinstance(m, nn.Linear): init.xavier_normal_(m.weight.data) init.normal_(m.bias.data) elif isinstance(m, nn.GRUCell): for param in m.parameters(): if len(param.shape) >= 2: init.orthogonal_(param.data) else: init.normal_(param.data)
Example #11
Source File: module.py From Tacotron-pytorch with MIT License | 6 votes |
def __init__(self, in_size, r): super(MelDecoder, self).__init__() self.in_size = in_size self.r = r self.prenet = Prenet(in_size * r, hidden_sizes=[256, 128]) # Input: (prenet output, previous context) self.attn_rnn = AttnWrapper( nn.GRUCell(256 + 128, 256), BahdanauAttn(256)) self.memory_layer = nn.Linear(256, 256, bias=False) # RNN decoder in the original paper self.pre_rnn_dec_proj = nn.Linear(512, 256) self.rnns_dec = nn.ModuleList( [nn.GRUCell(256, 256) for _ in range(2)]) self.mel_proj = nn.Linear(256, in_size * r) self.max_decode_steps = 200
Example #12
Source File: models.py From PlaNet with MIT License | 6 votes |
def __init__(self, belief_size, state_size, action_size, hidden_size, embedding_size, activation_function='relu', min_std_dev=0.1): super().__init__() self.act_fn = getattr(F, activation_function) self.min_std_dev = min_std_dev self.fc_embed_state_action = nn.Linear(state_size + action_size, belief_size) self.rnn = nn.GRUCell(belief_size, belief_size) self.fc_embed_belief_prior = nn.Linear(belief_size, hidden_size) self.fc_state_prior = nn.Linear(hidden_size, 2 * state_size) self.fc_embed_belief_posterior = nn.Linear(belief_size + embedding_size, hidden_size) self.fc_state_posterior = nn.Linear(hidden_size, 2 * state_size) # Operates over (previous) state, (previous) actions, (previous) belief, (previous) nonterminals (mask), and (current) observations # Diagram of expected inputs and outputs for T = 5 (-x- signifying beginning of output belief/state that gets sliced off): # t : 0 1 2 3 4 5 # o : -X--X--X--X--X- # a : -X--X--X--X--X- # n : -X--X--X--X--X- # pb: -X- # ps: -X- # b : -x--X--X--X--X--X- # s : -x--X--X--X--X--X-
Example #13
Source File: utilz.py From tamil-lm2 with GNU General Public License v2.0 | 6 votes |
def init_hidden(config, batch_size, cell): layers = 1 if isinstance(cell, (nn.LSTM, nn.GRU)): layers = cell.num_layers if cell.bidirectional: layers = layers * 2 if isinstance(cell, (nn.LSTM, nn.LSTMCell)): hidden = Variable(torch.zeros(layers, batch_size, cell.hidden_size)) context = Variable(torch.zeros(layers, batch_size, cell.hidden_size)) if config.CONFIG.cuda: hidden = hidden.cuda() context = context.cuda() return hidden, context if isinstance(cell, (nn.GRU, nn.GRUCell)): hidden = Variable(torch.zeros(layers, batch_size, cell.hidden_size)) if config.CONFIG.cuda: hidden = hidden.cuda() return hidden
Example #14
Source File: MTGCN.py From MultiTurnDialogZoo with MIT License | 5 votes |
def __init__(self, inpt_size, output_size, user_embed_size, posemb_size, dropout=0.5, threshold=2): # inpt_size: utter_hidden_size + user_embed_size super(OGCNContext, self).__init__() # utter + user_embed + pos_embed size = inpt_size + user_embed_size + posemb_size self.threshold = threshold # GatedGCN # self.kernel_rnn = nn.GRUCell(size, size) # self.conv1 = My_GatedGCN(size, inpt_size, self.kernel_rnn) # self.conv2 = My_GatedGCN(size, inpt_size, self.kernel_rnn) # self.conv3 = My_GatedGCN(size, inpt_size, self.kernel_rnn) self.conv1 = GCNConv(size, inpt_size) self.conv2 = GCNConv(size, inpt_size) self.conv3 = GCNConv(size, inpt_size) # self.bn1 = nn.BatchNorm1d(num_features=inpt_size) # self.bn2 = nn.BatchNorm1d(num_features=inpt_size) # self.bn3 = nn.BatchNorm1d(num_features=inpt_size) # rnn for background self.rnn = nn.GRU(inpt_size + user_embed_size, inpt_size, bidirectional=True) self.linear1 = nn.Linear(inpt_size * 2, inpt_size) self.linear2 = nn.Linear(inpt_size * 2, output_size) self.drop = nn.Dropout(p=dropout) # 100 is far bigger than the max turn lengths (cornell and dailydialog datasets) self.posemb = nn.Embedding(100, posemb_size) self.init_weight()
Example #15
Source File: rnn.py From Global-Encoding with MIT License | 5 votes |
def __init__(self, num_layers, input_size, hidden_size, dropout): super(StackedGRU, self).__init__() self.dropout = nn.Dropout(dropout) self.num_layers = num_layers self.layers = nn.ModuleList() for i in range(num_layers): self.layers.append(nn.GRUCell(input_size, hidden_size)) input_size = hidden_size
Example #16
Source File: default.py From espnet with Apache License 2.0 | 5 votes |
def __init__( self, n_vocab, n_layers, n_units, n_embed=None, typ="lstm", dropout_rate=0.5 ): """Initialize class. :param int n_vocab: The size of the vocabulary :param int n_layers: The number of layers to create :param int n_units: The number of units per layer :param str typ: The RNN type """ super(RNNLM, self).__init__() if n_embed is None: n_embed = n_units self.embed = nn.Embedding(n_vocab, n_embed) if typ == "lstm": self.rnn = nn.ModuleList( [nn.LSTMCell(n_embed, n_units)] + [nn.LSTMCell(n_units, n_units) for _ in range(n_layers - 1)] ) else: self.rnn = nn.ModuleList( [nn.GRUCell(n_embed, n_units)] + [nn.GRUCell(n_units, n_units) for _ in range(n_layers - 1)] ) self.dropout = nn.ModuleList( [nn.Dropout(dropout_rate) for _ in range(n_layers + 1)] ) self.lo = nn.Linear(n_units, n_vocab) self.n_layers = n_layers self.n_units = n_units self.typ = typ # initialize parameters from uniform distribution for param in self.parameters(): param.data.uniform_(-0.1, 0.1)
Example #17
Source File: torchfold_test.py From torchfold with Apache License 2.0 | 5 votes |
def __init__(self, num_units, input_size): super(RNNEncoder, self).__init__() self.num_units = num_units self.input_size = input_size self.encoder = nn.GRUCell(self.input_size, self.num_units)
Example #18
Source File: deterministic_decoder.py From deep-generative-lm with MIT License | 5 votes |
def __init__(self, device, seq_len, word_p, parameter_p, drop_type, unk_index, css, sparse, N, rnn_type, tie_in_out, v_dim, x_dim, h_dim, s_dim, l_dim): super(DeterministicDecoder, self).__init__(device, seq_len, word_p, parameter_p, drop_type, unk_index, css, N, rnn_type, v_dim, x_dim, h_dim, s_dim, l_dim) self.tie_in_out = tie_in_out # The model embeds words and passes them through the RNN to get a probability of next words. self.emb = nn.Embedding(v_dim, x_dim, sparse=bool(sparse)) # We currently support GRU and LSTM type RNNs if rnn_type == "GRU": if self.drop_type in ["varied", "shared"]: # Varied and shared dropout modes only drop input and output layer. Shared shares between timesteps. self.grnn = nn.GRU(x_dim, h_dim, l_dim, batch_first=True) else: self.grnn = nn.ModuleList([nn.GRUCell(x_dim, h_dim, 1)]) self.grnn.extend([nn.GRUCell(h_dim, h_dim, 1) for _ in range(l_dim - 1)]) elif rnn_type == "LSTM": if self.drop_type in ["varied", "shared"]: self.grnn = nn.LSTM(x_dim, h_dim, l_dim, batch_first=True) else: self.grnn = nn.ModuleList([nn.LSTMCell(x_dim, h_dim, 1)]) self.grnn.extend([nn.LSTMCell(h_dim, h_dim, 1) for _ in range(l_dim - 1)]) self.linear = nn.Linear(h_dim, v_dim)
Example #19
Source File: model_utils.py From sample-factory with MIT License | 5 votes |
def __init__(self, cfg, input_size): super().__init__(cfg) self.cfg = cfg self.is_gru = False if cfg.rnn_type == 'gru': self.core = nn.GRUCell(input_size, cfg.hidden_size) self.is_gru = True elif cfg.rnn_type == 'lstm': self.core = nn.LSTMCell(input_size, cfg.hidden_size) else: raise RuntimeError(f'Unknown RNN type {cfg.rnn_type}') self.core_output_size = cfg.hidden_size
Example #20
Source File: layers.py From mead-baseline with Apache License 2.0 | 5 votes |
def __init__(self, num_layers: int, input_size: int, rnn_size: int, dropout: float): super().__init__() self.dropout = nn.Dropout(dropout) self.num_layers = num_layers self.layers = nn.ModuleList() for i in range(num_layers): self.layers.append(nn.GRUCell(input_size=input_size, hidden_size=rnn_size)) input_size = rnn_size
Example #21
Source File: Layers.py From SDNet with MIT License | 5 votes |
def __init__(self, x_size, h_size): super(GetFinalScores, self).__init__() self.noanswer_linear = nn.Linear(h_size, x_size) self.noanswer_w = nn.Linear(x_size, 1, bias=True) self.no_linear = nn.Linear(h_size, x_size) self.no_w = nn.Linear(x_size, 1, bias=True) self.yes_linear = nn.Linear(h_size, x_size) self.yes_w = nn.Linear(x_size, 1, bias=True) self.attn = BilinearSeqAttn(x_size, h_size) self.attn2 = BilinearSeqAttn(x_size, h_size) self.rnn = nn.GRUCell(x_size, h_size)
Example #22
Source File: cgru.py From NJUNMT-pytorch with MIT License | 5 votes |
def __init__(self, input_size, hidden_size, context_size): super(CGRUCell, self).__init__() self.hidden_size = hidden_size self.context_size = context_size self.gru1 = nn.GRUCell(input_size=input_size, hidden_size=hidden_size) self.attn = BahdanauAttention(query_size=hidden_size, key_size=self.context_size) self.gru2 = nn.GRUCell(input_size=self.context_size, hidden_size=hidden_size) self._reset_parameters()
Example #23
Source File: pdp_decimate.py From PDP-Solver with MIT License | 5 votes |
def __init__(self, device, message_dimension, meta_data_dimension, hidden_dimension, mem_hidden_dimension, mem_agg_hidden_dimension, agg_hidden_dimension, edge_dimension, dropout): super(NeuralDecimator, self).__init__() self._device = device self._module_list = nn.ModuleList() self._drop_out = dropout if isinstance(message_dimension, tuple): variable_message_dim, function_message_dim = message_dimension else: variable_message_dim = message_dimension function_message_dim = message_dimension self._variable_rnn_cell = nn.GRUCell( variable_message_dim + edge_dimension + meta_data_dimension, hidden_dimension, bias=True) self._function_rnn_cell = nn.GRUCell( function_message_dim + edge_dimension + meta_data_dimension, hidden_dimension, bias=True) self._module_list.append(self._variable_rnn_cell) self._module_list.append(self._function_rnn_cell) self._hidden_dimension = hidden_dimension self._mem_hidden_dimension = mem_hidden_dimension self._agg_hidden_dimension = agg_hidden_dimension self._mem_agg_hidden_dimension = mem_agg_hidden_dimension
Example #24
Source File: tacotron.py From ZeroSpeech-TTS-without-T with MIT License | 5 votes |
def __init__(self, in_dim, r, attention): super(Decoder, self).__init__() self.in_dim = in_dim self.r = r self.prenet = Prenet(in_dim * r, sizes=[256, 128]) # (prenet_out + attention context) -> output self.attention = attention if self.attention == 'Bahdanau': self.attention_rnn = AttentionRNNh(nn.GRUCell(256 + 128, 256), BahdanauAttention(256), attention='Bahdanau') elif self.attention == 'LocationSensitive': self.attention_rnn = AttentionRNN(nn.GRUCell(256 + 128, 256), LocationSensitiveAttention(256), attention='LocationSensitive') else: raise NotImplementedError self.project_to_decoder_in = nn.Linear(512, 256) self.decoder_rnns = nn.ModuleList([nn.GRUCell(256, 256) for _ in range(2)]) self.proj_to_mel = nn.Linear(256, in_dim * self.r) self.proj_to_gate = nn.Linear(256, 1 * self.r) self.sigmoid = nn.Sigmoid() self.max_decoder_steps = 800
Example #25
Source File: tacotron.py From ZeroSpeech-TTS-without-T with MIT License | 5 votes |
def __init__(self, in_dim, r, attention): super(Decoder, self).__init__() self.in_dim = in_dim self.r = r self.prenet = Prenet(in_dim * r, sizes=[256, 128]) # (prenet_out + attention context) -> output self.attention = attention if self.attention == 'Bahdanau': self.attention_rnn = AttentionRNN(nn.GRUCell(256 + 128, 256), BahdanauAttention(256), attention='Bahdanau') elif self.attention == 'LocationSensitive': self.attention_rnn = AttentionRNN(nn.GRUCell(256 + 128, 256), LocationSensitiveAttention(256), attention='LocationSensitive') else: raise NotImplementedError self.project_to_decoder_in = nn.Linear(512, 256) self.decoder_rnns = nn.ModuleList([nn.GRUCell(256, 256) for _ in range(2)]) self.proj_to_mel = nn.Linear(256, in_dim * self.r) self.proj_to_gate = nn.Linear(256, 1 * self.r) self.sigmoid = nn.Sigmoid() self.max_decoder_steps = 800
Example #26
Source File: models.py From ilf with Apache License 2.0 | 5 votes |
def __init__(self, input_size, hidden_size): super(ArgsNet, self).__init__() self.hidden_size = hidden_size self.input_size = input_size self.gru = nn.GRUCell(self.input_size, self.hidden_size) self.fc1 = nn.Linear(self.hidden_size, 50) self.fc2 = nn.Linear(50, self.input_size)
Example #27
Source File: rnncells.py From neural_chat with MIT License | 5 votes |
def __init__(self, num_layers, input_size, rnn_size, dropout): super(StackedGRUCell, self).__init__() self.dropout = nn.Dropout(dropout) self.num_layers = num_layers self.layers = nn.ModuleList() for i in range(num_layers): self.layers.append(nn.GRUCell(input_size, rnn_size)) input_size = rnn_size
Example #28
Source File: asrn_res.py From MORAN_v2 with MIT License | 5 votes |
def __init__(self, input_size, hidden_size, num_embeddings=128, CUDA=True): super(AttentionCell, self).__init__() self.i2h = nn.Linear(input_size, hidden_size,bias=False) self.h2h = nn.Linear(hidden_size, hidden_size) self.score = nn.Linear(hidden_size, 1, bias=False) self.rnn = nn.GRUCell(input_size+num_embeddings, hidden_size) self.hidden_size = hidden_size self.input_size = input_size self.num_embeddings = num_embeddings self.fracPickup = fracPickup(CUDA=CUDA)
Example #29
Source File: StackedRNN.py From DC-NeuralConversation with MIT License | 5 votes |
def __init__(self, num_layers, input_size, rnn_size, dropout): super(StackedGRU, self).__init__() self.dropout = nn.Dropout(dropout) self.num_layers = num_layers self.layers = nn.ModuleList() for i in range(num_layers): self.layers.append(nn.GRUCell(input_size, rnn_size)) input_size = rnn_size
Example #30
Source File: model.py From WaveRNN-Pytorch with MIT License | 5 votes |
def get_gru_cell(self, gru) : gru_cell = nn.GRUCell(gru.input_size, gru.hidden_size) gru_cell.weight_hh.data = gru.weight_hh_l0.data gru_cell.weight_ih.data = gru.weight_ih_l0.data gru_cell.bias_hh.data = gru.bias_hh_l0.data gru_cell.bias_ih.data = gru.bias_ih_l0.data return gru_cell