Python torch.nn.init.xavier_uniform_() Examples
The following are 30
code examples of torch.nn.init.xavier_uniform_().
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.init
, or try the search function
.
Example #1
Source File: Embed.py From pytorch_NER_BiLSTM_CNN_CRF with Apache License 2.0 | 6 votes |
def _nn_embed(self, embed_dict, words_dict): """ :param embed_dict: :param words_dict: """ print("loading pre_train embedding by nn.Embedding for out of vocabulary.") embed = nn.Embedding(int(self.words_count), int(self.dim)) init.xavier_uniform_(embed.weight.data) embeddings = np.array(embed.weight.data) for word in words_dict: if word in embed_dict: embeddings[words_dict[word]] = np.array([float(i) for i in embed_dict[word]], dtype='float32') self.exact_count += 1 elif word.lower() in embed_dict: embeddings[words_dict[word]] = np.array([float(i) for i in embed_dict[word.lower()]], dtype='float32') self.fuzzy_count += 1 else: self.oov_count += 1 embeddings[self.padID] = 0 final_embed = torch.from_numpy(embeddings).float() return final_embed
Example #2
Source File: gat_layers.py From DeepInf with MIT License | 6 votes |
def __init__(self, n_head, f_in, f_out, attn_dropout, bias=True): super(BatchMultiHeadGraphAttention, self).__init__() self.n_head = n_head self.w = Parameter(torch.Tensor(n_head, f_in, f_out)) self.a_src = Parameter(torch.Tensor(n_head, f_out, 1)) self.a_dst = Parameter(torch.Tensor(n_head, f_out, 1)) self.leaky_relu = nn.LeakyReLU(negative_slope=0.2) self.softmax = nn.Softmax(dim=-1) self.dropout = nn.Dropout(attn_dropout) if bias: self.bias = Parameter(torch.Tensor(f_out)) init.constant_(self.bias, 0) else: self.register_parameter('bias', None) init.xavier_uniform_(self.w) init.xavier_uniform_(self.a_src) init.xavier_uniform_(self.a_dst)
Example #3
Source File: model.py From dspn with MIT License | 6 votes |
def __init__(self, set_encoder, set_decoder, input_encoder=None): """ In the auto-encoder setting, don't pass an input_encoder because the target set and mask is assumed to be the input. In the general prediction setting, must pass all three. """ super().__init__() self.set_encoder = set_encoder self.input_encoder = input_encoder self.set_decoder = set_decoder for m in self.modules(): if ( isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d) or isinstance(m, nn.Conv1d) ): init.xavier_uniform_(m.weight) if m.bias is not None: m.bias.data.zero_()
Example #4
Source File: gat_layers.py From DeepInf with MIT License | 6 votes |
def __init__(self, n_head, f_in, f_out, attn_dropout, bias=True): super(MultiHeadGraphAttention, self).__init__() self.n_head = n_head self.w = Parameter(torch.Tensor(n_head, f_in, f_out)) self.a_src = Parameter(torch.Tensor(n_head, f_out, 1)) self.a_dst = Parameter(torch.Tensor(n_head, f_out, 1)) self.leaky_relu = nn.LeakyReLU(negative_slope=0.2) self.softmax = nn.Softmax(dim=-1) self.dropout = nn.Dropout(attn_dropout) if bias: self.bias = Parameter(torch.Tensor(f_out)) init.constant_(self.bias, 0) else: self.register_parameter('bias', None) init.xavier_uniform_(self.w) init.xavier_uniform_(self.a_src) init.xavier_uniform_(self.a_dst)
Example #5
Source File: Embed.py From PyTorch_Biaffine_Dependency_Parsing with Apache License 2.0 | 6 votes |
def _nn_embed(self, embed_dict, words_dict): """ :param embed_dict: :param words_dict: """ print("loading pre_train embedding by nn.Embedding for out of vocabulary.") embed = nn.Embedding(int(self.words_count), int(self.dim)) init.xavier_uniform_(embed.weight.data) embeddings = np.array(embed.weight.data) for word in words_dict: if word in embed_dict: embeddings[words_dict[word]] = np.array([float(i) for i in embed_dict[word]], dtype='float32') self.exact_count += 1 elif word.lower() in embed_dict: embeddings[words_dict[word]] = np.array([float(i) for i in embed_dict[word.lower()]], dtype='float32') self.fuzzy_count += 1 else: self.oov_count += 1 embeddings[self.padID] = 0 final_embed = torch.from_numpy(embeddings).float() return final_embed
Example #6
Source File: gat_layers.py From GPF with MIT License | 6 votes |
def __init__(self, n_head, f_in, f_out, attn_dropout, bias=True): super(BatchMultiHeadGraphAttention, self).__init__() self.n_head = n_head self.w = Parameter(torch.Tensor(n_head, f_in, f_out)) self.a_src = Parameter(torch.Tensor(n_head, f_out, 1)) self.a_dst = Parameter(torch.Tensor(n_head, f_out, 1)) self.leaky_relu = nn.LeakyReLU(negative_slope=0.2) self.softmax = nn.Softmax(dim=-1) self.dropout = nn.Dropout(attn_dropout) if bias: self.bias = Parameter(torch.Tensor(f_out)) init.constant_(self.bias, 0) else: self.register_parameter('bias', None) init.xavier_uniform_(self.w) init.xavier_uniform_(self.a_src) init.xavier_uniform_(self.a_dst)
Example #7
Source File: Embed.py From pytorch_Joint-Word-Segmentation-and-POS-Tagging with Apache License 2.0 | 6 votes |
def _nn_embed(self, embed_dict, words_dict): """ :param embed_dict: :param words_dict: """ print("loading pre_train embedding by nn.Embedding for out of vocabulary.") embed = nn.Embedding(int(self.words_count), int(self.dim)) init.xavier_uniform_(embed.weight.data) embeddings = np.array(embed.weight.data) for word in words_dict: if word in embed_dict: embeddings[words_dict[word]] = np.array([float(i) for i in embed_dict[word]], dtype='float32') self.exact_count += 1 elif word.lower() in embed_dict: embeddings[words_dict[word]] = np.array([float(i) for i in embed_dict[word.lower()]], dtype='float32') self.fuzzy_count += 1 else: self.oov_count += 1 embeddings[self.padID] = 0 final_embed = torch.from_numpy(embeddings).float() return final_embed
Example #8
Source File: models.py From dgl with Apache License 2.0 | 6 votes |
def make_model(src_vocab, tgt_vocab, N=6, dim_model=512, dim_ff=2048, h=8, dropout=0.1, universal=False): if universal: return make_universal_model(src_vocab, tgt_vocab, dim_model, dim_ff, h, dropout) c = copy.deepcopy attn = MultiHeadAttention(h, dim_model) ff = PositionwiseFeedForward(dim_model, dim_ff) pos_enc = PositionalEncoding(dim_model, dropout) encoder = Encoder(EncoderLayer(dim_model, c(attn), c(ff), dropout), N) decoder = Decoder(DecoderLayer(dim_model, c(attn), c(attn), c(ff), dropout), N) src_embed = Embeddings(src_vocab, dim_model) tgt_embed = Embeddings(tgt_vocab, dim_model) generator = Generator(dim_model, tgt_vocab) model = Transformer( encoder, decoder, src_embed, tgt_embed, pos_enc, generator, h, dim_model // h) # xavier init for p in model.parameters(): if p.dim() > 1: INIT.xavier_uniform_(p) return model
Example #9
Source File: act.py From dgl with Apache License 2.0 | 6 votes |
def make_universal_model(src_vocab, tgt_vocab, dim_model=512, dim_ff=2048, h=8, dropout=0.1): c = copy.deepcopy attn = MultiHeadAttention(h, dim_model) ff = PositionwiseFeedForward(dim_model, dim_ff) pos_enc = PositionalEncoding(dim_model, dropout) time_enc = PositionalEncoding(dim_model, dropout) encoder = UEncoder(EncoderLayer((dim_model), c(attn), c(ff), dropout)) decoder = UDecoder(DecoderLayer((dim_model), c(attn), c(attn), c(ff), dropout)) src_embed = Embeddings(src_vocab, dim_model) tgt_embed = Embeddings(tgt_vocab, dim_model) generator = Generator(dim_model, tgt_vocab) model = UTransformer( encoder, decoder, src_embed, tgt_embed, pos_enc, time_enc, generator, h, dim_model // h) # xavier init for p in model.parameters(): if p.dim() > 1: INIT.xavier_uniform_(p) return model
Example #10
Source File: gat_layers.py From GPF with MIT License | 6 votes |
def __init__(self, n_head, f_in, f_out, attn_dropout, bias=True): super(MultiHeadGraphAttention, self).__init__() self.n_head = n_head self.w = Parameter(torch.Tensor(n_head, f_in, f_out)) self.a_src = Parameter(torch.Tensor(n_head, f_out, 1)) self.a_dst = Parameter(torch.Tensor(n_head, f_out, 1)) self.leaky_relu = nn.LeakyReLU(negative_slope=0.2) self.softmax = nn.Softmax(dim=-1) self.dropout = nn.Dropout(attn_dropout) if bias: self.bias = Parameter(torch.Tensor(f_out)) init.constant_(self.bias, 0) else: self.register_parameter('bias', None) init.xavier_uniform_(self.w) init.xavier_uniform_(self.a_src) init.xavier_uniform_(self.a_dst)
Example #11
Source File: submodules.py From DeeperInverseCompositionalAlgorithm with MIT License | 6 votes |
def initialize_weights(modules, method='xavier'): for m in modules: if isinstance(m, nn.Conv2d): if m.bias is not None: m.bias.data.zero_() if method == 'xavier': init.xavier_uniform_(m.weight) elif method == 'kaiming': init.kaiming_uniform_(m.weight) if isinstance(m, nn.ConvTranspose2d): if m.bias is not None: m.bias.data.zero_() if method == 'xavier': init.xavier_uniform_(m.weight) elif method == 'kaiming': init.kaiming_uniform_(m.weight)
Example #12
Source File: BigGAN.py From BigGAN-PyTorch with MIT License | 6 votes |
def init_weights(self): self.param_count = 0 for module in self.modules(): if (isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear) or isinstance(module, nn.Embedding)): if self.init == 'ortho': init.orthogonal_(module.weight) elif self.init == 'N02': init.normal_(module.weight, 0, 0.02) elif self.init in ['glorot', 'xavier']: init.xavier_uniform_(module.weight) else: print('Init style not recognized...') self.param_count += sum([p.data.nelement() for p in module.parameters()]) print('Param count for D''s initialized parameters: %d' % self.param_count)
Example #13
Source File: BigGAN.py From BigGAN-PyTorch with MIT License | 6 votes |
def init_weights(self): self.param_count = 0 for module in self.modules(): if (isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear) or isinstance(module, nn.Embedding)): if self.init == 'ortho': init.orthogonal_(module.weight) elif self.init == 'N02': init.normal_(module.weight, 0, 0.02) elif self.init in ['glorot', 'xavier']: init.xavier_uniform_(module.weight) else: print('Init style not recognized...') self.param_count += sum([p.data.nelement() for p in module.parameters()]) print('Param count for G''s initialized parameters: %d' % self.param_count) # Note on this forward function: we pass in a y vector which has # already been passed through G.shared to enable easy class-wise # interpolation later. If we passed in the one-hot and then ran it through # G.shared in this forward function, it would be harder to handle.
Example #14
Source File: BigGANdeep.py From BigGAN-PyTorch with MIT License | 6 votes |
def init_weights(self): self.param_count = 0 for module in self.modules(): if (isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear) or isinstance(module, nn.Embedding)): if self.init == 'ortho': init.orthogonal_(module.weight) elif self.init == 'N02': init.normal_(module.weight, 0, 0.02) elif self.init in ['glorot', 'xavier']: init.xavier_uniform_(module.weight) else: print('Init style not recognized...') self.param_count += sum([p.data.nelement() for p in module.parameters()]) print('Param count for D''s initialized parameters: %d' % self.param_count)
Example #15
Source File: BigGANdeep.py From BigGAN-PyTorch with MIT License | 6 votes |
def init_weights(self): self.param_count = 0 for module in self.modules(): if (isinstance(module, nn.Conv2d) or isinstance(module, nn.Linear) or isinstance(module, nn.Embedding)): if self.init == 'ortho': init.orthogonal_(module.weight) elif self.init == 'N02': init.normal_(module.weight, 0, 0.02) elif self.init in ['glorot', 'xavier']: init.xavier_uniform_(module.weight) else: print('Init style not recognized...') self.param_count += sum([p.data.nelement() for p in module.parameters()]) print('Param count for G''s initialized parameters: %d' % self.param_count) # Note on this forward function: we pass in a y vector which has # already been passed through G.shared to enable easy class-wise # interpolation later. If we passed in the one-hot and then ran it through # G.shared in this forward function, it would be harder to handle. # NOTE: The z vs y dichotomy here is for compatibility with not-y
Example #16
Source File: counting_model.py From VQA2.0-Recent-Approachs-2018.pytorch with MIT License | 6 votes |
def __init__(self, embedding_tokens, embedding_features, lstm_features, drop=0.0): super(TextProcessor, self).__init__() self.embedding = nn.Embedding(embedding_tokens, embedding_features, padding_idx=0) self.drop = nn.Dropout(drop) self.tanh = nn.Tanh() self.lstm = nn.GRU(input_size=embedding_features, hidden_size=lstm_features, num_layers=1) self.features = lstm_features self._init_lstm(self.lstm.weight_ih_l0) self._init_lstm(self.lstm.weight_hh_l0) self.lstm.bias_ih_l0.data.zero_() self.lstm.bias_hh_l0.data.zero_() init.xavier_uniform_(self.embedding.weight)
Example #17
Source File: model_helper.py From SSD_Pytorch with MIT License | 5 votes |
def xavier(param): init.xavier_uniform_(param) # def weights_init(m): # if isinstance(m, nn.Conv2d): # xavier(m.weight.data) # m.bias.data.zero_()
Example #18
Source File: train.py From SSD_resnet_pytorch with MIT License | 5 votes |
def xavier(param): init.xavier_uniform_(param)
Example #19
Source File: model.py From GCN-GAN-pytorch with MIT License | 5 votes |
def _reset_parameters(self): init.xavier_uniform_(self.weights)
Example #20
Source File: box_utils.py From SSD_Pytorch with MIT License | 5 votes |
def xavier(param): init.xavier_uniform_(param) # def weights_init(m): # if isinstance(m, nn.Conv2d): # xavier(m.weight.data) # m.bias.data.zero_()
Example #21
Source File: train_refinedet.py From RefineDet.PyTorch with MIT License | 5 votes |
def xavier(param): init.xavier_uniform_(param)
Example #22
Source File: tanh_concat_attention.py From MAMS-for-ABSA with Apache License 2.0 | 5 votes |
def __init__(self, query_size, key_size, dropout=0): super(TanhConcatAttention, self).__init__(dropout) self.query_weights = nn.Parameter(torch.Tensor(query_size, 1)) self.key_weights = nn.Parameter(torch.Tensor(key_size, 1)) init.xavier_uniform_(self.query_weights) init.xavier_uniform_(self.key_weights)
Example #23
Source File: bilinear_attention.py From MAMS-for-ABSA with Apache License 2.0 | 5 votes |
def __init__(self, query_size, key_size, dropout=0): super(BilinearAttention, self).__init__(dropout) self.weights = nn.Parameter(torch.FloatTensor(query_size, key_size)) init.xavier_uniform_(self.weights)
Example #24
Source File: concat_attention.py From MAMS-for-ABSA with Apache License 2.0 | 5 votes |
def __init__(self, query_size, key_size, dropout=0): super(ConcatAttention, self).__init__(dropout) self.query_weights = nn.Parameter(torch.Tensor(query_size, 1)) self.key_weights = nn.Parameter(torch.Tensor(key_size, 1)) init.xavier_uniform_(self.query_weights) init.xavier_uniform_(self.key_weights)
Example #25
Source File: mlp_attention.py From MAMS-for-ABSA with Apache License 2.0 | 5 votes |
def __init__(self, query_size, key_size, out_size=100, dropout=0): super(MlpAttention, self).__init__(dropout) self.query_projection = nn.Linear(query_size, out_size) self.key_projection = nn.Linear(key_size, out_size) self.v = nn.Parameter(torch.FloatTensor(out_size, 1)) init.xavier_uniform_(self.v)
Example #26
Source File: counting_model.py From VQA2.0-Recent-Approachs-2018.pytorch with MIT License | 5 votes |
def _init_lstm(self, weight): for w in weight.chunk(3, 0): init.xavier_uniform_(w)
Example #27
Source File: counting_model.py From VQA2.0-Recent-Approachs-2018.pytorch with MIT License | 5 votes |
def __init__(self, embedding_tokens): super(Net, self).__init__() question_features = 1024 vision_features = config.output_features glimpses = 2 objects = 10 self.text = TextProcessor( embedding_tokens=len(embedding_tokens)+1, embedding_features=300, lstm_features=question_features, drop=0.5, ) self.attention = Attention( v_features=vision_features, q_features=question_features, mid_features=512, glimpses=glimpses, drop=0.5, ) self.classifier = Classifier( in_features=(glimpses * vision_features, question_features), mid_features=1024, out_features=config.max_answers, count_features=objects + 1, drop=0.5, ) self.counter = counting.Counter(objects) for m in self.modules(): if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d): init.xavier_uniform_(m.weight) if m.bias is not None: m.bias.data.zero_()
Example #28
Source File: main.py From VASNet with MIT License | 5 votes |
def weights_init(m): classname = m.__class__.__name__ if classname == 'Linear': init.xavier_uniform_(m.weight, gain=np.sqrt(2.0)) if m.bias is not None: init.constant_(m.bias, 0.1)
Example #29
Source File: no_query_attention.py From MAMS-for-ABSA with Apache License 2.0 | 5 votes |
def __init__(self, query_size, attention): super(NoQueryAttention, self).__init__() self.query_size = query_size self.query = nn.Parameter(torch.Tensor(1, query_size)) init.xavier_uniform_(self.query) self.attention = attention
Example #30
Source File: vgg16_bn.py From CRAFT-pytorch with MIT License | 5 votes |
def init_weights(modules): for m in modules: if isinstance(m, nn.Conv2d): init.xavier_uniform_(m.weight.data) if m.bias is not None: m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() elif isinstance(m, nn.Linear): m.weight.data.normal_(0, 0.01) m.bias.data.zero_()