Python torch.nn.init.xavier_normal() Examples
The following are 30
code examples of torch.nn.init.xavier_normal().
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: model.py From SingleGAN with MIT License | 7 votes |
def weights_init(init_type='xavier'): def init_fun(m): classname = m.__class__.__name__ if (classname.find('Conv') == 0 or classname.find('Linear') == 0) and hasattr(m, 'weight'): if init_type == 'normal': init.normal(m.weight.data, 0.0, 0.02) elif init_type == 'xavier': init.xavier_normal(m.weight.data, gain=math.sqrt(2)) elif init_type == 'kaiming': init.kaiming_normal(m.weight.data, a=0, mode='fan_in') elif init_type == 'orthogonal': init.orthogonal(m.weight.data, gain=math.sqrt(2)) elif init_type == 'default': pass else: assert 0, "Unsupported initialization: {}".format(init_type) if hasattr(m, 'bias') and m.bias is not None: init.constant(m.bias.data, 0.0) return init_fun
Example #2
Source File: network.py From DMIT with MIT License | 7 votes |
def weights_init(init_type='xavier'): def init_fun(m): classname = m.__class__.__name__ if (classname.find('Conv') == 0 or classname.find('Linear') == 0) and hasattr(m, 'weight'): if init_type == 'normal': init.normal(m.weight.data, 0.0, 0.02) elif init_type == 'xavier': init.xavier_normal(m.weight.data, gain=math.sqrt(2)) elif init_type == 'kaiming': init.kaiming_normal(m.weight.data, a=0, mode='fan_in') elif init_type == 'orthogonal': init.orthogonal(m.weight.data, gain=math.sqrt(2)) elif init_type == 'default': pass else: assert 0, "Unsupported initialization: {}".format(init_type) if hasattr(m, 'bias') and m.bias is not None: init.constant(m.bias.data, 0.0) elif (classname.find('Norm') == 0): if hasattr(m, 'weight') and m.weight is not None: init.constant(m.weight.data, 1.0) if hasattr(m, 'bias') and m.bias is not None: init.constant(m.bias.data, 0.0) return init_fun
Example #3
Source File: dgm.py From semi-supervised-pytorch with MIT License | 6 votes |
def __init__(self, dims): """ M2 code replication from the paper 'Semi-Supervised Learning with Deep Generative Models' (Kingma 2014) in PyTorch. The "Generative semi-supervised model" is a probabilistic model that incorporates label information in both inference and generation. Initialise a new generative model :param dims: dimensions of x, y, z and hidden layers. """ [x_dim, self.y_dim, z_dim, h_dim] = dims super(DeepGenerativeModel, self).__init__([x_dim, z_dim, h_dim]) self.encoder = Encoder([x_dim + self.y_dim, h_dim, z_dim]) self.decoder = Decoder([z_dim + self.y_dim, list(reversed(h_dim)), x_dim]) self.classifier = Classifier([x_dim, h_dim[0], self.y_dim]) for m in self.modules(): if isinstance(m, nn.Linear): init.xavier_normal(m.weight.data) if m.bias is not None: m.bias.data.zero_()
Example #4
Source File: vae.py From semi-supervised-pytorch with MIT License | 6 votes |
def __init__(self, dims): """ Variational Autoencoder [Kingma 2013] model consisting of an encoder/decoder pair for which a variational distribution is fitted to the encoder. Also known as the M1 model in [Kingma 2014]. :param dims: x, z and hidden dimensions of the networks """ super(VariationalAutoencoder, self).__init__() [x_dim, z_dim, h_dim] = dims self.z_dim = z_dim self.flow = None self.encoder = Encoder([x_dim, h_dim, z_dim]) self.decoder = Decoder([z_dim, list(reversed(h_dim)), x_dim]) self.kl_divergence = 0 for m in self.modules(): if isinstance(m, nn.Linear): init.xavier_normal(m.weight.data) if m.bias is not None: m.bias.data.zero_()
Example #5
Source File: vae.py From semi-supervised-pytorch with MIT License | 6 votes |
def __init__(self, dims, n_samples=100): super(GumbelAutoencoder, self).__init__() [x_dim, z_dim, h_dim] = dims self.z_dim = z_dim self.n_samples = n_samples self.encoder = Perceptron([x_dim, *h_dim]) self.sampler = GumbelSoftmax(h_dim[-1], z_dim, n_samples) self.decoder = Perceptron([z_dim, *reversed(h_dim), x_dim], output_activation=F.sigmoid) self.kl_divergence = 0 for m in self.modules(): if isinstance(m, nn.Linear): init.xavier_normal(m.weight.data) if m.bias is not None: m.bias.data.zero_()
Example #6
Source File: vae.py From semi-supervised-pytorch with MIT License | 6 votes |
def __init__(self, dims): """ Ladder Variational Autoencoder as described by [Sønderby 2016]. Adds several stochastic layers to improve the log-likelihood estimate. :param dims: x, z and hidden dimensions of the networks """ [x_dim, z_dim, h_dim] = dims super(LadderVariationalAutoencoder, self).__init__([x_dim, z_dim[0], h_dim]) neurons = [x_dim, *h_dim] encoder_layers = [LadderEncoder([neurons[i - 1], neurons[i], z_dim[i - 1]]) for i in range(1, len(neurons))] decoder_layers = [LadderDecoder([z_dim[i - 1], h_dim[i - 1], z_dim[i]]) for i in range(1, len(h_dim))][::-1] self.encoder = nn.ModuleList(encoder_layers) self.decoder = nn.ModuleList(decoder_layers) self.reconstruction = Decoder([z_dim[0], h_dim, x_dim]) for m in self.modules(): if isinstance(m, nn.Linear): init.xavier_normal(m.weight.data) if m.bias is not None: m.bias.data.zero_()
Example #7
Source File: attention.py From GraphIE with GNU General Public License v3.0 | 6 votes |
def __init__(self, n_head, d_input, d_model, d_input_v=None, dropout=0.1): super(MultiHeadAttention, self).__init__() self.n_head = n_head d_k, d_v = d_model//n_head, d_model//n_head self.d_k = d_k self.d_v = d_v if d_input_v is None: d_input_v = d_input self.w_qs = nn.Parameter(torch.FloatTensor(n_head, d_input, d_k)) self.w_ks = nn.Parameter(torch.FloatTensor(n_head, d_input, d_k)) self.w_vs = nn.Parameter(torch.FloatTensor(n_head, d_input_v, d_v)) self.attention = DotProductAttention(d_model) # self.attention = SingleLayerAttention(d_model, d_k) # self.layer_norm = LayerNormalization(d_model) self.proj = Linear(n_head*d_v, d_model) self.dropout = nn.Dropout(dropout) init.xavier_normal(self.w_qs) init.xavier_normal(self.w_ks) init.xavier_normal(self.w_vs)
Example #8
Source File: BasicBlock.py From DAIN with MIT License | 6 votes |
def __init__(self, inplanes, planes, dilation = 1, stride=1, downsample=None): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes,dilation, stride) # self.bn1 = nn.BatchNorm2d(planes) self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes) # self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.stride = stride for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) # weight_init.xavier_normal() elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_()
Example #9
Source File: Transformer.py From TextClassificationBenchmark with MIT License | 6 votes |
def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1): super(MultiHeadAttention, self).__init__() self.n_head = n_head self.d_k = d_k self.d_v = d_v self.w_qs = nn.Parameter(torch.FloatTensor(n_head, d_model, d_k)) self.w_ks = nn.Parameter(torch.FloatTensor(n_head, d_model, d_k)) self.w_vs = nn.Parameter(torch.FloatTensor(n_head, d_model, d_v)) self.attention = ScaledDotProductAttention(d_model) self.layer_norm = LayerNormalization(d_model) self.proj = Linear(n_head*d_v, d_model) self.dropout = nn.Dropout(dropout) init.xavier_normal(self.w_qs) init.xavier_normal(self.w_ks) init.xavier_normal(self.w_vs)
Example #10
Source File: vae.py From hybrid_rvae with MIT License | 6 votes |
def __init__(self, vocab_size, embed_size, latent_size, decoder_size, decoder_num_layers): super(VAE, self).__init__() self.latent_size = latent_size self.vocab_size = vocab_size self.embed_size = embed_size self.embed = nn.Embedding(self.vocab_size, self.embed_size) self.embed.weight = xavier_normal(self.embed.weight) self.encoder = Encoder(self.embed_size, self.latent_size) self.context_to_mu = nn.Linear(self.latent_size, self.latent_size) self.context_to_logvar = nn.Linear(self.latent_size, self.latent_size) self.decoder = Decoder(self.vocab_size, self.latent_size, decoder_size, decoder_num_layers, self.embed_size)
Example #11
Source File: models.py From open-solution-data-science-bowl-2018 with MIT License | 5 votes |
def init_weights_xavier(model): if isinstance(model, nn.Conv2d): init.xavier_normal(model.weight) init.constant(model.bias, 0)
Example #12
Source File: model.py From style-based-gan-pytorch with MIT License | 5 votes |
def init_linear(linear): init.xavier_normal(linear.weight) linear.bias.data.zero_()
Example #13
Source File: representation.py From pair2vec with Apache License 2.0 | 5 votes |
def init(self): [xavier_normal(p) for p in self.parameters() if len(p.size()) > 1] if self.vocab.vectors is not None: pretrained = normalize(self.vocab.vectors, dim=-1) if self.normalize_pretrained else self.vocab.vectors self.embedding.weight.data.copy_(pretrained) print('Copied pretrained vectors into relation span representation') else: #xavier_normal(self.embedding.weight.data) self.embedding.reset_parameters()
Example #14
Source File: model.py From unet-pytorch with MIT License | 5 votes |
def weight_init(m): if isinstance(m, nn.Conv2d): init.xavier_normal(m.weight) init.constant(m.bias, 0)
Example #15
Source File: dragan.py From dragan-pytorch with MIT License | 5 votes |
def xavier_init(model): for param in model.parameters(): if len(param.size()) == 2: xavier_normal(param)
Example #16
Source File: matcher.py From One-shot-Relational-Learning with Apache License 2.0 | 5 votes |
def __init__(self, embed_dim, num_ents, num_rels, use_pretrain=True, ent_embed=None, rel_matrices=None, dropout=0.1, attn_layers=1, n_head=4, batch_size=64, process_steps=4, finetune=False, aggregate='max'): super(RescalMatcher, self).__init__() self.embed_dim = embed_dim self.ent_emb = nn.Embedding(num_ents + 1, embed_dim, padding_idx=num_ents) self.rel_matrices = nn.Embedding(num_rels + 1, embed_dim * embed_dim, padding_idx=num_rels) self.aggregate = aggregate self.gcn_w = nn.Linear(self.embed_dim, self.embed_dim) self.gcn_b = nn.Parameter(torch.FloatTensor(self.embed_dim)) # self.gcn_self_r = nn.Parameter(torch.FloatTensor(1, self.embed_dim)) self.dropout = nn.Dropout(0.5) init.xavier_normal(self.gcn_w.weight) init.constant(self.gcn_b, 0) if use_pretrain: print('LOADING KB EMBEDDINGS') # emb_np = np.loadtxt(embed_path) self.ent_emb.weight.data.copy_(torch.from_numpy(ent_embed)) self.rel_matrices.weight.data.copy_(torch.from_numpy(rel_matrices)) if not finetune: print('FIX KB EMBEDDING') self.ent_emb.weight.requires_grad = False self.rel_matrices.weight.requires_grad = False d_model = embed_dim * 2 self.support_encoder = SupportEncoder(d_model, 2*d_model, dropout) self.query_encoder = QueryEncoder(d_model, process_steps) # self.slf_neighbor = ContextAwareEncoder(attn_layers, d_model, d_inner_hid, n_head, d_k, d_v, dropout) # extra parameters for Siamese Neural Networks # self.siamese = nn.Linear(d_model, 1) # init.xavier_normal(self.siamese.weight)
Example #17
Source File: Highway.py From torch-light with MIT License | 5 votes |
def _init_weight(self): stdv = 1. / math.sqrt(self.hsz) self.gate.weight.data.uniform_(-stdv, stdv) self.gate.bias.data.fill_(-1) if active.__name__ == "relu": init.xavier_normal(self.h.weight) else: self.h.weight.data.uniform_(-stdv, stdv)
Example #18
Source File: model.py From torch-light with MIT License | 5 votes |
def _init_weight(self): if self.share_linear: self.linear.weight = self.dec.dec_ebd.weight else: init.xavier_normal(self.linear.weight)
Example #19
Source File: vanilla_gan.py From dragan-pytorch with MIT License | 5 votes |
def xavier_init(model): for param in model.parameters(): if len(param.size()) == 2: xavier_normal(param)
Example #20
Source File: highway.py From torch-light with MIT License | 5 votes |
def _init_weight(self): stdv = 1. / math.sqrt(self.hsz) self.gate.weight.data.uniform_(-stdv, stdv) self.gate.bias.data.fill_(-1) if active.__name__ == "relu": init.xavier_normal(self.h.weight) else: self.h.weight.data.uniform_(-stdv, stdv)
Example #21
Source File: models.py From open-solution-mapping-challenge with MIT License | 5 votes |
def init_weights_xavier(model): if isinstance(model, nn.Conv2d): init.xavier_normal(model.weight) init.constant(model.bias, 0)
Example #22
Source File: dgm.py From semi-supervised-pytorch with MIT License | 5 votes |
def __init__(self, dims): """ Ladder version of the Deep Generative Model. Uses a hierarchical representation that is trained end-to-end to give very nice disentangled representations. :param dims: dimensions of x, y, z layers and h layers note that len(z) == len(h). """ [x_dim, y_dim, z_dim, h_dim] = dims super(LadderDeepGenerativeModel, self).__init__([x_dim, y_dim, z_dim[0], h_dim]) neurons = [x_dim, *h_dim] encoder_layers = [LadderEncoder([neurons[i - 1], neurons[i], z_dim[i - 1]]) for i in range(1, len(neurons))] e = encoder_layers[-1] encoder_layers[-1] = LadderEncoder([e.in_features + y_dim, e.out_features, e.z_dim]) decoder_layers = [LadderDecoder([z_dim[i - 1], h_dim[i - 1], z_dim[i]]) for i in range(1, len(h_dim))][::-1] self.classifier = Classifier([x_dim, h_dim[0], y_dim]) self.encoder = nn.ModuleList(encoder_layers) self.decoder = nn.ModuleList(decoder_layers) self.reconstruction = Decoder([z_dim[0]+y_dim, h_dim, x_dim]) for m in self.modules(): if isinstance(m, nn.Linear): init.xavier_normal(m.weight.data) if m.bias is not None: m.bias.data.zero_()
Example #23
Source File: utils.py From LightNetPlusPlus with MIT License | 5 votes |
def weights_init_xavier(m): classname = m.__class__.__name__ # print(classname) if classname.find('Conv') != -1: init.xavier_normal(m.weight.data, gain=1) elif classname.find('Linear') != -1: init.xavier_normal(m.weight.data, gain=1) elif classname.find('BatchNorm') != -1: init.normal(m.weight.data, 1.0, 0.02) init.constant(m.bias.data, 0.0)
Example #24
Source File: Transformer.py From TextClassificationBenchmark with MIT License | 5 votes |
def __init__(self, d_in, d_out, bias=True): super(Linear, self).__init__() self.linear = nn.Linear(d_in, d_out, bias=bias) init.xavier_normal(self.linear.weight)
Example #25
Source File: cnn_train.py From Evolutionary-Autoencoders with MIT License | 5 votes |
def weights_init_xavier(m): classname = m.__class__.__name__ if classname.find('Conv') != -1: init.xavier_normal(m.weight.data, gain=1) elif classname.find('Linear') != -1: init.xavier_normal(m.weight.data, gain=1) elif classname.find('BatchNorm2d') != -1: init.uniform(m.weight.data, 1.0, 0.02) init.constant(m.bias.data, 0.0)
Example #26
Source File: cnn_train.py From Evolutionary-Autoencoders with MIT License | 5 votes |
def weights_init_xavier(m): classname = m.__class__.__name__ if classname.find('Conv') != -1: init.xavier_normal(m.weight.data, gain=1) elif classname.find('Linear') != -1: init.xavier_normal(m.weight.data, gain=1) elif classname.find('BatchNorm2d') != -1: init.uniform(m.weight.data, 1.0, 0.02) init.constant(m.bias.data, 0.0)
Example #27
Source File: network_architectures.py From self-ensemble-visual-domain-adapt with MIT License | 5 votes |
def __init__(self, n_classes): super(Grey_32_64_128_gp_nonorm, self).__init__() self.conv1_1 = nn.Conv2d(1, 32, (3, 3), padding=1) self.conv1_2 = nn.Conv2d(32, 32, (3, 3), padding=1) self.pool1 = nn.MaxPool2d((2, 2)) self.conv2_1 = nn.Conv2d(32, 64, (3, 3), padding=1) self.conv2_2 = nn.Conv2d(64, 64, (3, 3), padding=1) self.conv2_3 = nn.Conv2d(64, 64, (3, 3), padding=1) self.pool2 = nn.MaxPool2d((2, 2)) self.conv3_1 = nn.Conv2d(64, 128, (3, 3), padding=1) self.conv3_2 = nn.Conv2d(128, 128, (3, 3), padding=1) self.conv3_3 = nn.Conv2d(128, 128, (3, 3), padding=1) self.pool3 = nn.MaxPool2d((2, 2)) self.drop1 = nn.Dropout() self.fc4 = nn.Linear(128, 128) self.fc5 = nn.Linear(128, n_classes) nninit.xavier_normal(self.conv1_1.weight) nninit.xavier_normal(self.conv1_2.weight) nninit.xavier_normal(self.conv2_1.weight) nninit.xavier_normal(self.conv2_2.weight) nninit.xavier_normal(self.conv2_3.weight) nninit.xavier_normal(self.conv3_1.weight) nninit.xavier_normal(self.conv3_2.weight) nninit.xavier_normal(self.conv3_3.weight) nninit.xavier_normal(self.fc4.weight) nninit.xavier_normal(self.fc5.weight)
Example #28
Source File: utils.py From pytorch-ewc with MIT License | 5 votes |
def xavier_initialize(model): modules = [ m for n, m in model.named_modules() if 'conv' in n or 'linear' in n ] parameters = [ p for m in modules for p in m.parameters() if p.dim() >= 2 ] for p in parameters: init.xavier_normal(p)
Example #29
Source File: self_attention_snl.py From GAN_Review with MIT License | 5 votes |
def __init__(self, d_in, d_out, bias=True): super(Linear, self).__init__() self.linear = nn.Linear(d_in, d_out, bias=bias) init.xavier_normal(self.linear.weight)
Example #30
Source File: self_attention.py From GAN_Review with MIT License | 5 votes |
def __init__(self, d_in, d_out, bias=True): super(Linear, self).__init__() self.linear = nn.Linear(d_in, d_out, bias=bias) init.xavier_normal(self.linear.weight)