Python torch.nn.init.orthogonal() Examples
The following are 30
code examples of torch.nn.init.orthogonal().
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: bnlstm.py From RAdam with Apache License 2.0 | 7 votes |
def reset_parameters(self): """ Initialize parameters following the way proposed in the paper. """ # The input-to-hidden weight matrix is initialized orthogonally. init.orthogonal(self.weight_ih.data) # The hidden-to-hidden weight matrix is initialized as an identity # matrix. weight_hh_data = torch.eye(self.hidden_size) weight_hh_data = weight_hh_data.repeat(1, 4) self.weight_hh.data.set_(weight_hh_data) # The bias is just set to zero vectors. init.constant(self.bias.data, val=0) # Initialization of BN parameters. self.bn_ih.reset_parameters() self.bn_hh.reset_parameters() self.bn_c.reset_parameters() self.bn_ih.bias.data.fill_(0) self.bn_hh.bias.data.fill_(0) self.bn_ih.weight.data.fill_(0.1) self.bn_hh.weight.data.fill_(0.1) self.bn_c.weight.data.fill_(0.1)
Example #3
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 #4
Source File: cnn_train.py From cgp-cnn-PyTorch with MIT License | 6 votes |
def init_weights(net, init_type='normal'): print('initialization method [%s]' % init_type) if init_type == 'normal': net.apply(weights_init_normal) elif init_type == 'xavier': net.apply(weights_init_xavier) elif init_type == 'kaiming': net.apply(weights_init_kaiming) elif init_type == 'orthogonal': net.apply(weights_init_orthogonal) else: raise NotImplementedError('initialization method [%s] is not implemented' % init_type) # __init__: load dataset # __call__: training the CNN defined by CGP list
Example #5
Source File: AoAReader.py From AoAReader with MIT License | 6 votes |
def __init__(self, vocab_dict, dropout_rate, embed_dim, hidden_dim, bidirectional=True): super(AoAReader, self).__init__() self.vocab_dict = vocab_dict self.hidden_dim = hidden_dim self.embed_dim = embed_dim self.dropout_rate = dropout_rate self.embedding = nn.Embedding(vocab_dict.size(), self.embed_dim, padding_idx=Constants.PAD) self.embedding.weight.data.uniform_(-0.05, 0.05) input_size = self.embed_dim self.gru = nn.GRU(input_size, hidden_size=self.hidden_dim, dropout=dropout_rate, bidirectional=bidirectional, batch_first=True) # try independent gru #self.query_gru = nn.GRU(input_size, hidden_size=self.hidden_dim, dropout=dropout_rate, # bidirectional=bidirectional, batch_first=True) for weight in self.gru.parameters(): if len(weight.size()) > 1: weigth_init.orthogonal(weight.data)
Example #6
Source File: aoa.py From chinese_reading_comprehension with Apache License 2.0 | 6 votes |
def __init__(self, vocab_dict, dropout_rate, embed_dim, hidden_dim, bidirectional=True): super(AOA, self).__init__() self.vocab_dict = vocab_dict self.hidden_dim = hidden_dim self.embed_dim = embed_dim self.dropout_rate = dropout_rate self.embedding = nn.Embedding(vocab_dict.size(), self.embed_dim, padding_idx=PAD) self.embedding.weight.data.uniform_(-0.05, 0.05) input_size = self.embed_dim self.gru = nn.GRU(input_size, hidden_size=self.hidden_dim, dropout=dropout_rate, bidirectional=bidirectional, batch_first=True) for weight in self.gru.parameters(): if len(weight.size()) > 1: weigth_init.orthogonal(weight.data)
Example #7
Source File: bnlstm.py From FewShotLearning with MIT License | 6 votes |
def reset_parameters(self): """ Initialize parameters following the way proposed in the paper. """ # The input-to-hidden weight matrix is initialized orthogonally. init.orthogonal(self.weight_ih.data) # The hidden-to-hidden weight matrix is initialized as an identity # matrix. weight_hh_data = torch.eye(self.hidden_size) weight_hh_data = weight_hh_data.repeat(1, 4) self.weight_hh.data.set_(weight_hh_data) # The bias is just set to zero vectors. init.constant(self.bias.data, val=0) # Initialization of BN parameters. self.bn_ih.reset_parameters() self.bn_hh.reset_parameters() self.bn_c.reset_parameters() self.bn_ih.bias.data.fill_(0) self.bn_hh.bias.data.fill_(0) self.bn_ih.weight.data.fill_(0.1) self.bn_hh.weight.data.fill_(0.1) self.bn_c.weight.data.fill_(0.1)
Example #8
Source File: lstm.py From tranX with Apache License 2.0 | 6 votes |
def reset_parameters(self): init.orthogonal(self.W_i) init.orthogonal(self.U_i) init.orthogonal(self.U_i_p) init.orthogonal(self.W_f) init.orthogonal(self.U_f) init.orthogonal(self.U_f_p) init.orthogonal(self.W_c) init.orthogonal(self.U_c) init.orthogonal(self.U_c_p) init.orthogonal(self.W_o) init.orthogonal(self.U_o) init.orthogonal(self.U_o_p) self.b_i.data.fill_(0.) self.b_c.data.fill_(0.) self.b_o.data.fill_(0.) # forget bias set to 1. self.b_f.data.fill_(1.) self.b_f_p.data.fill_(1.)
Example #9
Source File: bnlstm.py From benchmark with BSD 3-Clause "New" or "Revised" License | 6 votes |
def reset_parameters(self): """ Initialize parameters following the way proposed in the paper. """ # The input-to-hidden weight matrix is initialized orthogonally. init.orthogonal(self.weight_ih.data) # The hidden-to-hidden weight matrix is initialized as an identity # matrix. weight_hh_data = torch.eye(self.hidden_size) weight_hh_data = weight_hh_data.repeat(1, 4) self.weight_hh.data.set_(weight_hh_data) # The bias is just set to zero vectors. init.constant(self.bias.data, val=0) # Initialization of BN parameters. self.bn_ih.reset_parameters() self.bn_hh.reset_parameters() self.bn_c.reset_parameters() self.bn_ih.bias.data.fill_(0) self.bn_hh.bias.data.fill_(0) self.bn_ih.weight.data.fill_(0.1) self.bn_hh.weight.data.fill_(0.1) self.bn_c.weight.data.fill_(0.1)
Example #10
Source File: latticelstm.py From glyce with Apache License 2.0 | 6 votes |
def reset_parameters(self): """ Initialize parameters following the way proposed in the paper. """ init.orthogonal(self.weight_ih.data) init.orthogonal(self.alpha_weight_ih.data) weight_hh_data = torch.eye(self.hidden_size) weight_hh_data = weight_hh_data.repeat(1, 3) self.weight_hh.data.set_(weight_hh_data) alpha_weight_hh_data = torch.eye(self.hidden_size) alpha_weight_hh_data = alpha_weight_hh_data.repeat(1, 1) self.alpha_weight_hh.data.set_(alpha_weight_hh_data) # The bias is just set to zero vectors. if self.use_bias: init.constant(self.bias.data, val=0) init.constant(self.alpha_bias.data, val=0)
Example #11
Source File: cnn_train.py From Evolutionary-Autoencoders with MIT License | 6 votes |
def init_weights(net, init_type='normal'): print('initialization method [%s]' % init_type) if init_type == 'normal': net.apply(weights_init_normal) elif init_type == 'xavier': net.apply(weights_init_xavier) elif init_type == 'kaiming': net.apply(weights_init_kaiming) elif init_type == 'orthogonal': net.apply(weights_init_orthogonal) else: raise NotImplementedError('initialization method [%s] is not implemented' % init_type) # __init__: load dataset # __call__: training the CNN defined by CGP list
Example #12
Source File: bnlstm.py From GAN_Review with MIT License | 6 votes |
def reset_parameters(self): """ Initialize parameters following the way proposed in the paper. """ # The input-to-hidden weight matrix is initialized orthogonally. init.orthogonal(self.weight_ih.data) # The hidden-to-hidden weight matrix is initialized as an identity # matrix. weight_hh_data = torch.eye(self.hidden_size) weight_hh_data = weight_hh_data.repeat(1, 4) self.weight_hh.data.set_(weight_hh_data) # The bias is just set to zero vectors. init.constant(self.bias.data, val=0) # Initialization of BN parameters. self.bn_ih.reset_parameters() self.bn_hh.reset_parameters() self.bn_c.reset_parameters() self.bn_ih.bias.data.fill_(0) self.bn_hh.bias.data.fill_(0) self.bn_ih.weight.data.fill_(0.1) self.bn_hh.weight.data.fill_(0.1) self.bn_c.weight.data.fill_(0.1)
Example #13
Source File: cnn_train.py From Evolutionary-Autoencoders with MIT License | 6 votes |
def init_weights(net, init_type='normal'): print('initialization method [%s]' % init_type) if init_type == 'normal': net.apply(weights_init_normal) elif init_type == 'xavier': net.apply(weights_init_xavier) elif init_type == 'kaiming': net.apply(weights_init_kaiming) elif init_type == 'orthogonal': net.apply(weights_init_orthogonal) else: raise NotImplementedError('initialization method [%s] is not implemented' % init_type) # __init__: load dataset # __call__: training the CNN defined by CGP list
Example #14
Source File: models.py From TreeEnc with MIT License | 6 votes |
def reset_parameters(self): if self.use_leaf_rnn: init.kaiming_normal(self.leaf_rnn_cell.weight_ih.data) init.orthogonal(self.leaf_rnn_cell.weight_hh.data) init.constant(self.leaf_rnn_cell.bias_ih.data, val=0) init.constant(self.leaf_rnn_cell.bias_hh.data, val=0) # Set forget bias to 1 self.leaf_rnn_cell.bias_ih.data.chunk(4)[1].fill_(1) if self.bidirectional: init.kaiming_normal(self.leaf_rnn_cell_bw.weight_ih.data) init.orthogonal(self.leaf_rnn_cell_bw.weight_hh.data) init.constant(self.leaf_rnn_cell_bw.bias_ih.data, val=0) init.constant(self.leaf_rnn_cell_bw.bias_hh.data, val=0) # Set forget bias to 1 self.leaf_rnn_cell_bw.bias_ih.data.chunk(4)[1].fill_(1) else: init.kaiming_normal(self.word_linear.weight.data) init.constant(self.word_linear.bias.data, val=0) self.treelstm_layer.reset_parameters()
Example #15
Source File: cnn_train.py From Evolutionary-Autoencoders with MIT License | 5 votes |
def weights_init_orthogonal(m): classname = m.__class__.__name__ print(classname) if classname.find('Conv') != -1: init.orthogonal(m.weight.data, gain=1) elif classname.find('Linear') != -1: init.orthogonal(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 #16
Source File: models.py From pytorch-rnng with MIT License | 5 votes |
def reset_parameters(self) -> None: for name, param in self.lstm.named_parameters(): if name.startswith('weight'): init.orthogonal(param) else: assert name.startswith('bias') init.constant(param, 0.) init.constant(self.h0, 0.) init.constant(self.c0, 0.)
Example #17
Source File: convgru.py From rl_swiss with MIT License | 5 votes |
def __init__(self, input_size, hidden_size, kernel_size): super().__init__() padding = kernel_size // 2 self.input_size = input_size self.hidden_size = hidden_size self.reset_gate = nn.Conv2d(input_size + hidden_size, hidden_size, kernel_size, padding=padding) self.update_gate = nn.Conv2d(input_size + hidden_size, hidden_size, kernel_size, padding=padding) self.out_gate = nn.Conv2d(input_size + hidden_size, hidden_size, kernel_size, padding=padding) init.orthogonal(self.reset_gate.weight) init.orthogonal(self.update_gate.weight) init.orthogonal(self.out_gate.weight) init.constant(self.reset_gate.bias, 0.) init.constant(self.update_gate.bias, 0.) init.constant(self.out_gate.bias, 0.)
Example #18
Source File: models.py From pytorch-rnng with MIT License | 5 votes |
def reset_parameters(self) -> None: # Embeddings for name in 'word pos nt action'.split(): embedding = getattr(self, f'{name}_embedding') embedding.reset_parameters() # Encoders for name in 'stack buffer history'.split(): encoder = getattr(self, f'{name}_encoder') encoder.reset_parameters() # Compositions for name in 'fwd bwd'.split(): lstm = getattr(self, f'{name}_composer') for pname, pval in lstm.named_parameters(): if pname.startswith('weight'): init.orthogonal(pval) else: assert pname.startswith('bias') init.constant(pval, 0.) # Transformations gain = init.calculate_gain('relu') for name in 'word nt action'.split(): layer = getattr(self, f'{name}2encoder') init.xavier_uniform(layer[0].weight, gain=gain) init.constant(layer[0].bias, 1.) init.xavier_uniform(self.fwdbwd2composed[0].weight, gain=gain) init.constant(self.fwdbwd2composed[0].bias, 1.) init.xavier_uniform(self.encoders2summary[1].weight, gain=gain) init.constant(self.encoders2summary[1].bias, 1.) init.xavier_uniform(self.summary2actionlogprobs.weight) init.constant(self.summary2actionlogprobs.bias, 0.) # Guards for name in 'stack buffer history'.split(): guard = getattr(self, f'{name}_guard') init.constant(guard, 0.)
Example #19
Source File: cnn_train.py From cgp-cnn-PyTorch with MIT License | 5 votes |
def weights_init_orthogonal(m): classname = m.__class__.__name__ print(classname) if classname.find('Conv') != -1: init.orthogonal(m.weight.data, gain=1) elif classname.find('Linear') != -1: init.orthogonal(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 #20
Source File: bnlstm.py From FewShotLearning with MIT License | 5 votes |
def reset_parameters(self): """ Initialize parameters following the way proposed in the paper. """ init.orthogonal(self.weight_ih.data) weight_hh_data = torch.eye(self.hidden_size) weight_hh_data = weight_hh_data.repeat(1, 4) self.weight_hh.data.set_(weight_hh_data) # The bias is just set to zero vectors. if self.use_bias: init.constant(self.bias.data, val=0)
Example #21
Source File: srresnet.py From onnx-fb-universe with MIT License | 5 votes |
def _initialize_orthogonal(conv): prelu_gain = math.sqrt(2) init.orthogonal(conv.weight, gain=prelu_gain) if conv.bias is not None: conv.bias.data.zero_()
Example #22
Source File: super_resolution.py From onnx-fb-universe with MIT License | 5 votes |
def _initialize_weights(self): init.orthogonal(self.conv1.weight, init.calculate_gain('relu')) init.orthogonal(self.conv2.weight, init.calculate_gain('relu')) init.orthogonal(self.conv3.weight, init.calculate_gain('relu')) init.orthogonal(self.conv4.weight)
Example #23
Source File: pc_model.py From malmo-challenge with MIT License | 5 votes |
def __init__(self, hidden_size): super(ActorCritic, self).__init__() self.state_size = STATE_SIZE[0] * STATE_SIZE[1] * STATE_SIZE[2] self.elu = nn.ELU(inplace=True) self.softmax = nn.Softmax() self.sigmoid = nn.Sigmoid() # Pass state into model body self.conv1 = nn.Conv2d(STATE_SIZE[0], 32, 4, stride=2) self.conv2 = nn.Conv2d(32, 32, 3) self.fc1 = nn.Linear(1152, hidden_size) # Pass previous action, reward and timestep directly into LSTM self.lstm = nn.LSTMCell(hidden_size + ACTION_SIZE + 2, hidden_size) self.fc_actor1 = nn.Linear(hidden_size, ACTION_SIZE) self.fc_critic1 = nn.Linear(hidden_size, ACTION_SIZE) self.fc_actor2 = nn.Linear(hidden_size, ACTION_SIZE) self.fc_critic2 = nn.Linear(hidden_size, ACTION_SIZE) self.fc_class = nn.Linear(hidden_size, 1) # Orthogonal weight initialisation for name, p in self.named_parameters(): if 'weight' in name: init.orthogonal(p) elif 'bias' in name: init.constant(p, 0) # Set LSTM forget gate bias to 1 for name, p in self.lstm.named_parameters(): if 'bias' in name: n = p.size(0) forget_start_idx, forget_end_idx = n // 4, n // 2 init.constant(p[forget_start_idx:forget_end_idx], 1)
Example #24
Source File: utils.py From LightNetPlusPlus with MIT License | 5 votes |
def weights_init_orthogonal(m): classname = m.__class__.__name__ # print(classname) if classname.find('Conv') != -1: init.orthogonal(m.weight.data, gain=1) elif classname.find('Linear') != -1: init.orthogonal(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 #25
Source File: utils.py From LightNetPlusPlus with MIT License | 5 votes |
def init_weights(net, init_type='normal'): # print('initialization method [%s]' % init_type) if init_type == 'normal': net.apply(weights_init_normal) elif init_type == 'xavier': net.apply(weights_init_xavier) elif init_type == 'kaiming': net.apply(weights_init_kaiming) elif init_type == 'orthogonal': net.apply(weights_init_orthogonal) else: raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
Example #26
Source File: bnlstm.py From benchmark with BSD 3-Clause "New" or "Revised" License | 5 votes |
def reset_parameters(self): """ Initialize parameters following the way proposed in the paper. """ init.orthogonal(self.weight_ih.data) weight_hh_data = torch.eye(self.hidden_size) weight_hh_data = weight_hh_data.repeat(1, 4) self.weight_hh.data.set_(weight_hh_data) # The bias is just set to zero vectors. if self.use_bias: init.constant(self.bias.data, val=0)
Example #27
Source File: latticelstm.py From glyce with Apache License 2.0 | 5 votes |
def reset_parameters(self): """ Initialize parameters following the way proposed in the paper. """ init.orthogonal(self.weight_ih.data) weight_hh_data = torch.eye(self.hidden_size) weight_hh_data = weight_hh_data.repeat(1, 3) self.weight_hh.data.set_(weight_hh_data) # The bias is just set to zero vectors. if self.use_bias: init.constant(self.bias.data, val=0)
Example #28
Source File: cnn_train.py From Evolutionary-Autoencoders with MIT License | 5 votes |
def weights_init_orthogonal(m): classname = m.__class__.__name__ print(classname) if classname.find('Conv') != -1: init.orthogonal(m.weight.data, gain=1) elif classname.find('Linear') != -1: init.orthogonal(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 #29
Source File: models.py From TreeEnc with MIT License | 5 votes |
def reset_parameters(self): init.kaiming_normal(self.rnn.weight_ih_l0.data) init.orthogonal(self.rnn.weight_hh_l0.data) init.constant(self.rnn.bias_ih_l0.data, val=0) init.constant(self.rnn.bias_hh_l0.data, val=0) # Set forget bias to 1 self.rnn.bias_ih_l0.data.chunk(4)[1].fill_(1)
Example #30
Source File: models.py From TreeEnc with MIT License | 5 votes |
def __init__(self, bidirectional, hidden_size, attention_size=128, dropout=0): super(AttnCombiner, self).__init__() self.num_directions = 2 if bidirectional else 1 self.ws1 = nn.Linear(hidden_size * self.num_directions, attention_size, bias=False) self.ws2 = nn.Linear(attention_size, 1, bias=False) init.orthogonal(self.ws1.weight.data) init.orthogonal(self.ws2.weight.data) self.tanh = nn.Tanh() self.drop = nn.Dropout(dropout)