本文整理匯總了Python中torch.nn.GRUCell方法的典型用法代碼示例。如果您正苦於以下問題:Python nn.GRUCell方法的具體用法?Python nn.GRUCell怎麽用?Python nn.GRUCell使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torch.nn
的用法示例。
在下文中一共展示了nn.GRUCell方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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)
示例2: init_hidden
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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
示例3: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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-
示例4: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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
示例5: weights_init
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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)
示例6: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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)
示例7: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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)
示例8: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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()
示例9: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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)
示例10: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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)
示例11: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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)
示例12: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
def __init__(self, x_size, y_size, hidden_size, dropout_rate=0, cell_type=nn.GRUCell, normalize=True):
super(PointerNetwork, self).__init__()
self.normalize = normalize
self.hidden_size = hidden_size
self.dropout_rate = dropout_rate
self.linear = nn.Linear(x_size+y_size, hidden_size, bias=False)
self.weights = nn.Linear(hidden_size, 1, bias=False)
self.self_attn = NonLinearSeqAttn(y_size, hidden_size)
self.cell = cell_type(x_size, y_size)
示例13: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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
示例14: __init__
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
def __init__(self, nclass, nchannel, dropout = 0.3):
super(DTD,self).__init__()
self.nclass = nclass
self.nchannel = nchannel
self.pre_lstm = nn.LSTM(nchannel, int(nchannel / 2), bidirectional=True)
self.rnn = nn.GRUCell(nchannel * 2, nchannel)
self.generator = nn.Sequential(
nn.Dropout(p = dropout),
nn.Linear(nchannel, nclass)
)
self.char_embeddings = Parameter(torch.randn(nclass, nchannel))
示例15: get_gru_cell
# 需要導入模塊: from torch import nn [as 別名]
# 或者: from torch.nn import GRUCell [as 別名]
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