Python torch.nn.Parameter() Examples
The following are 30
code examples of torch.nn.Parameter().
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: modules.py From BAMnet with Apache License 2.0 | 7 votes |
def __init__(self, hidden_size, h_state_embed_size=None, in_memory_embed_size=None, atten_type='simple'): super(Attention, self).__init__() self.atten_type = atten_type if not h_state_embed_size: h_state_embed_size = hidden_size if not in_memory_embed_size: in_memory_embed_size = hidden_size if atten_type in ('mul', 'add'): self.W = torch.Tensor(h_state_embed_size, hidden_size) self.W = nn.Parameter(nn.init.xavier_uniform_(self.W)) if atten_type == 'add': self.W2 = torch.Tensor(in_memory_embed_size, hidden_size) self.W2 = nn.Parameter(nn.init.xavier_uniform_(self.W2)) self.W3 = torch.Tensor(hidden_size, 1) self.W3 = nn.Parameter(nn.init.xavier_uniform_(self.W3)) elif atten_type == 'simple': pass else: raise RuntimeError('Unknown atten_type: {}'.format(self.atten_type))
Example #2
Source File: norm.py From torch-toolbox with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, num_features, eps=1e-5, momentum=0.9, affine=True): super(_SwitchNorm, self).__init__() self.num_features = num_features self.eps = eps self.momentum = momentum self.affine = affine if self.affine: self.weight = nn.Parameter(torch.Tensor(num_features)) self.bias = nn.Parameter(torch.Tensor(num_features)) else: self.register_parameter('weight', None) self.register_parameter('bias', None) self.mean_weight = nn.Parameter(torch.ones(3)) self.var_weight = nn.Parameter(torch.ones(3)) self.register_buffer('running_mean', torch.zeros(num_features)) self.register_buffer('running_var', torch.ones(num_features))
Example #3
Source File: ReadoutFunction.py From nmp_qc with MIT License | 6 votes |
def init_duvenaud(self, params): learn_args = [] learn_modules = [] args = {} args['out'] = params['out'] # Define a parameter matrix W for each layer. for l in range(params['layers']): learn_args.append(nn.Parameter(torch.randn(params['in'][l], params['out']))) # learn_modules.append(nn.Linear(params['out'], params['target'])) learn_modules.append(NNet(n_in=params['out'], n_out=params['target'])) return nn.ParameterList(learn_args), nn.ModuleList(learn_modules), args # GG-NN, Li et al.
Example #4
Source File: norm.py From torch-toolbox with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, prefix, num_features, eps=1e-5, momentum=0.9, groups=32, affine=True): super(_EvoNorm, self).__init__() assert prefix in ('s0', 'b0') self.prefix = prefix self.groups = groups self.num_features = num_features self.eps = eps self.momentum = momentum self.affine = affine if self.affine: self.weight = nn.Parameter(torch.Tensor(1, num_features, 1, 1)) self.bias = nn.Parameter(torch.Tensor(1, num_features, 1, 1)) self.v = nn.Parameter(torch.Tensor(1, num_features, 1, 1)) else: self.register_parameter('weight', None) self.register_parameter('bias', None) self.register_parameter('v', None) self.register_buffer('running_var', torch.ones(1, num_features, 1, 1)) self.reset_parameters()
Example #5
Source File: enc_PTRUNK.py From ConvLab with MIT License | 6 votes |
def __init__(self, hidden_size, output_size, n_layers=1, dropout=0.1): super(PtrDecoderRNN, self).__init__() self.hidden_size = hidden_size self.output_size = output_size ### Vocab size self.n_layers = n_layers self.dropout = dropout self.embedding = nn.Embedding(output_size, hidden_size) self.embedding_dropout = nn.Dropout(dropout) self.lstm = nn.LSTM(2*hidden_size, hidden_size, n_layers, dropout=dropout) self.W1 = nn.Linear(2*hidden_size, hidden_size) self.v = nn.Parameter(torch.rand(hidden_size)) stdv = 1. / math.sqrt(self.v.size(0)) self.v.data.normal_(mean=0, std=stdv) self.concat = nn.Linear(hidden_size * 2, hidden_size) self.U = nn.Linear(hidden_size, output_size) self.W = nn.Linear(hidden_size, 1) if USE_CUDA: self.embedding = self.embedding.cuda() self.embedding_dropout = self.embedding_dropout.cuda() self.lstm = self.lstm.cuda() self.W1 = self.W1.cuda() self.v = self.v.cuda() self.U = self.U.cuda() self.W = self.W.cuda()
Example #6
Source File: usermodule.py From ConvLab with MIT License | 6 votes |
def __init__(self, vocab_size, embed_size, hidden_size, input_dropout_p=0, dropout_p=0, n_layers=1, rnn_cell='GRU', variable_lengths=False, embedding=None, update_embedding=True): super(Encoder, self).__init__() self.vocab_size = vocab_size self.hidden_size = hidden_size self.n_layers = n_layers self.input_dropout = nn.Dropout(p=input_dropout_p) if rnn_cell == 'LSTM': self.rnn_cell = nn.LSTM elif rnn_cell == 'GRU': self.rnn_cell = nn.GRU else: raise ValueError("Unsupported RNN Cell: {0}".format(rnn_cell)) self.variable_lengths = variable_lengths self.embedding = nn.Embedding(vocab_size, embed_size) if embedding is not None: self.embedding.weight = nn.Parameter(embedding) self.embedding.weight.requires_grad = update_embedding self.rnn = self.rnn_cell(embed_size, hidden_size, n_layers, batch_first=True, dropout=dropout_p)
Example #7
Source File: detnet_backbone.py From cascade-rcnn_Pytorch with MIT License | 6 votes |
def load_pretrained_imagenet_weights(model, state_dict): own_state = model.state_dict() for name, param in state_dict.items(): if ('layer4' in name) or ('layer5' in name) or ('fc' in name): continue if (name in own_state): if isinstance(param, nn.Parameter): # backwards compatibility for serialized parameters param = param.data try: own_state[name].copy_(param) except Exception: raise RuntimeError('While copying the parameter named {}, ' 'whose dimensions in the model are {} and ' 'whose dimensions in the checkpoint are {}.' .format(name, own_state[name].size(), param.size())) else: raise KeyError('unexpected key "{}" in state_dict' .format(name))
Example #8
Source File: serialization.py From Dispersion-based-Clustering with MIT License | 6 votes |
def copy_state_dict(state_dict, model, strip=None): tgt_state = model.state_dict() copied_names = set() for name, param in state_dict.items(): if strip is not None and name.startswith(strip): name = name[len(strip):] if name not in tgt_state: continue if isinstance(param, Parameter): param = param.data if param.size() != tgt_state[name].size(): print('mismatch:', name, param.size(), tgt_state[name].size()) continue tgt_state[name].copy_(param) copied_names.add(name) missing = set(tgt_state.keys()) - copied_names if len(missing) > 0: print("missing keys in state_dict:", missing) return model
Example #9
Source File: dee_model.py From Doc2EDAG with MIT License | 6 votes |
def __init__(self, event_type, field_types, hidden_size): super(EventTable, self).__init__() self.event_type = event_type self.field_types = field_types self.num_fields = len(field_types) self.hidden_size = hidden_size self.event_cls = nn.Linear(hidden_size, 2) # 0: NA, 1: trigger this event self.field_cls_list = nn.ModuleList( # 0: NA, 1: trigger this field [nn.Linear(hidden_size, 2) for _ in range(self.num_fields)] ) # used to aggregate sentence and span embedding self.event_query = nn.Parameter(torch.Tensor(1, self.hidden_size)) # used for fields that do not contain any valid span # self.none_span_emb = nn.Parameter(torch.Tensor(1, self.hidden_size)) # used for aggregating history filled span info self.field_queries = nn.ParameterList( [nn.Parameter(torch.Tensor(1, self.hidden_size)) for _ in range(self.num_fields)] ) self.reset_parameters()
Example #10
Source File: set2set.py From LanczosNetwork with MIT License | 6 votes |
def __init__(self, element_dim, num_step_encoder): """ Implementation of Set2Set """ super(Set2Set, self).__init__() self.element_dim = element_dim self.num_step_encoder = num_step_encoder self.LSTM_encoder = Set2SetLSTM(element_dim) self.LSTM_decoder = Set2SetLSTM(element_dim) self.W_1 = nn.Parameter(torch.ones(self.element_dim, self.element_dim)) self.W_2 = nn.Parameter(torch.ones(self.element_dim, 1)) self.W_3 = nn.Parameter(torch.ones(self.element_dim, self.element_dim)) self.W_4 = nn.Parameter(torch.ones(self.element_dim, 1)) self.W_5 = nn.Parameter(torch.ones(self.element_dim, self.element_dim)) self.W_6 = nn.Parameter(torch.ones(self.element_dim, self.element_dim)) self.W_7 = nn.Parameter(torch.ones(self.element_dim, 1)) self.register_parameter('W_1', self.W_1) self.register_parameter('W_2', self.W_2) self.register_parameter('W_3', self.W_3) self.register_parameter('W_4', self.W_4) self.register_parameter('W_5', self.W_5) self.register_parameter('W_6', self.W_6) self.register_parameter('W_7', self.W_7) self._init_param()
Example #11
Source File: model2.py From controllable-text-attribute-transfer with Apache License 2.0 | 6 votes |
def __init__(self, input_size, hidden_size, correlation_func=1, do_similarity=False): super(AttentionScore, self).__init__() self.correlation_func = correlation_func self.hidden_size = hidden_size if correlation_func == 2 or correlation_func == 3: self.linear = nn.Linear(input_size, hidden_size, bias=False) if do_similarity: self.diagonal = Parameter(torch.ones(1, 1, 1) / (hidden_size ** 0.5), requires_grad=False) else: self.diagonal = Parameter(torch.ones(1, 1, hidden_size), requires_grad=True) if correlation_func == 4: self.linear = nn.Linear(input_size, input_size, bias=False) if correlation_func == 5: self.linear = nn.Linear(input_size, hidden_size, bias=False)
Example #12
Source File: model.py From ICDAR-2019-SROIE with MIT License | 6 votes |
def __init__(self, n_classes): super(SSD300, self).__init__() self.n_classes = n_classes self.base = VGGBase() self.aux_convs = AuxiliaryConvolutions() self.pred_convs = PredictionConvolutions(n_classes) # Since lower level features (conv4_3_feats) have considerably larger scales, we take the L2 norm and rescale # Rescale factor is initially set at 20, but is learned for each channel during back-prop self.rescale_factors = nn.Parameter(torch.FloatTensor(1, 512, 1, 1)) # there are 512 channels in conv4_3_feats nn.init.constant_(self.rescale_factors, 20) # Prior boxes self.priors_cxcy = self.create_prior_boxes()
Example #13
Source File: model2.py From controllable-text-attribute-transfer with Apache License 2.0 | 6 votes |
def __init__(self, input_size, hidden_size, correlation_func=1, do_similarity=False): super(AttentionScore, self).__init__() self.correlation_func = correlation_func self.hidden_size = hidden_size if correlation_func == 2 or correlation_func == 3: self.linear = nn.Linear(input_size, hidden_size, bias=False) if do_similarity: self.diagonal = Parameter(torch.ones(1, 1, 1) / (hidden_size ** 0.5), requires_grad=False) else: self.diagonal = Parameter(torch.ones(1, 1, hidden_size), requires_grad=True) if correlation_func == 4: self.linear = nn.Linear(input_size, input_size, bias=False) if correlation_func == 5: self.linear = nn.Linear(input_size, hidden_size, bias=False)
Example #14
Source File: prunable_nn.py From prunnable-layers-pytorch with GNU General Public License v3.0 | 6 votes |
def drop_inputs(self, input_shape, index, dim=0): """ Previous layer is expected to be a convnet which just underwent pruning Drop cells connected to the pruned layer of the convnet :param input_shape: shape of inputs before flattening, should exclude batch_size :param index: index to drop :param dim: dimension where index is dropped, w.r.t input_shape :return: """ is_cuda = self.weight.is_cuda reshaped = self.weight.view(-1, *input_shape) dim_length = input_shape[dim] indices = Variable(torch.LongTensor([i for i in range(dim_length) if i != index])) indices = indices.cuda() if is_cuda else indices self.weight = nn.Parameter( reshaped.index_select(dim+1, indices) .data .view(self.out_features, -1) ) self.in_features = self.weight.size()[1]
Example #15
Source File: attention.py From TaskBot with GNU General Public License v3.0 | 6 votes |
def __init__(self, hidden_size, method="concat"): """ Args: hidden_size: <int>, hidden size previous hidden state size of decoder method: <str>, {"concat"} Attention method Notes: we use the GRU outputs instead of using encoder t-step hidden sates for attention, because the pytorch-GRU hidden_n only contains the last time step information. """ super(Attn, self).__init__() self.method = method self.hidden_size = hidden_size self.attn = nn.Linear(self.hidden_size * 2, hidden_size) self.v = nn.Parameter(torch.rand(hidden_size)) stdv = 1. / math.sqrt(self.v.size(0)) self.v.data.normal_(mean=0, std=stdv)
Example #16
Source File: model2.py From controllable-text-attribute-transfer with Apache License 2.0 | 6 votes |
def __init__(self, input_size, hidden_size, correlation_func=1, do_similarity=False): super(AttentionScore, self).__init__() self.correlation_func = correlation_func self.hidden_size = hidden_size if correlation_func == 2 or correlation_func == 3: self.linear = nn.Linear(input_size, hidden_size, bias=False) if do_similarity: self.diagonal = Parameter(torch.ones(1, 1, 1) / (hidden_size ** 0.5), requires_grad=False) else: self.diagonal = Parameter(torch.ones(1, 1, hidden_size), requires_grad=True) if correlation_func == 4: self.linear = nn.Linear(input_size, input_size, bias=False) if correlation_func == 5: self.linear = nn.Linear(input_size, hidden_size, bias=False)
Example #17
Source File: CRF.py From pytorch_NER_BiLSTM_CNN_CRF with Apache License 2.0 | 6 votes |
def __init__(self, **kwargs): """ kwargs: target_size: int, target size device: str, device """ super(CRF, self).__init__() for k in kwargs: self.__setattr__(k, kwargs[k]) device = self.device # init transitions self.START_TAG, self.STOP_TAG = -2, -1 init_transitions = torch.zeros(self.target_size + 2, self.target_size + 2, device=device) init_transitions[:, self.START_TAG] = -10000.0 init_transitions[self.STOP_TAG, :] = -10000.0 self.transitions = nn.Parameter(init_transitions)
Example #18
Source File: meta.py From ScenarioMeta with MIT License | 6 votes |
def __init__(self, hidden_size, layer_norm=False, input_gate=True, forget_gate=True): nn.Module.__init__(self) self.hidden_size = hidden_size # gradient(2), param(2), loss self.lstm = nn.LSTMCell(input_size=5, hidden_size=hidden_size) if layer_norm: self.layer_norm = nn.LayerNorm(hidden_size) else: self.layer_norm = None self.input_gate = input_gate self.forget_gate = forget_gate if self.input_gate: self.lr_layer = nn.Linear(hidden_size, 1) self.lrs = [] else: self.output_layer = nn.Linear(hidden_size, 1) self.dets = [] if forget_gate: self.fg_layer = nn.Linear(hidden_size, 1) self.fgs = [] self.h_0 = nn.Parameter(torch.randn((hidden_size,), requires_grad=True)) self.c_0 = nn.Parameter(torch.randn((hidden_size,), requires_grad=True))
Example #19
Source File: detnet_backbone.py From cascade-rcnn_Pytorch with MIT License | 6 votes |
def load_pretrained_imagenet_weights(model, state_dict): own_state = model.state_dict() for name, param in state_dict.items(): if ('layer4' in name) or ('layer5' in name) or ('fc' in name): continue if (name in own_state): if isinstance(param, nn.Parameter): # backwards compatibility for serialized parameters param = param.data try: own_state[name].copy_(param) except Exception: raise RuntimeError('While copying the parameter named {}, ' 'whose dimensions in the model are {} and ' 'whose dimensions in the checkpoint are {}.' .format(name, own_state[name].size(), param.size())) else: raise KeyError('unexpected key "{}" in state_dict' .format(name))
Example #20
Source File: bert_basic_layer.py From mrc-for-flat-nested-ner with Apache License 2.0 | 5 votes |
def __init__(self, hidden_size, eps=1e-12): """Construct a layernorm module in the TF style (epsilon inside the square root). """ super(BertLayerNorm, self).__init__() self.weight = nn.Parameter(torch.ones(hidden_size)) self.bias = nn.Parameter(torch.zeros(hidden_size)) self.variance_epsilon = eps
Example #21
Source File: transformer.py From naru with Apache License 2.0 | 5 votes |
def __init__(self, d_in, d_out, w_init_std=0.02): super(Conv1d, self).__init__() self.w = nn.Parameter(torch.zeros(d_in, d_out)) self.b = nn.Parameter(torch.zeros(d_out)) nn.init.normal_(self.w, std=w_init_std) nn.init.zeros_(self.b) self.d_in = d_in self.d_out = d_out
Example #22
Source File: bert_basic_layer.py From mrc-for-flat-nested-ner with Apache License 2.0 | 5 votes |
def __init__(self, config, bert_model_embedding_weights): super(BertLMPredictionHead, self).__init__() self.transform = BertPredictionHeadTransform(config) # The output weights are the same as the input embeddings, but there is # an output-only bias for each token. self.decoder = nn.Linear(bert_model_embedding_weights.size(1), bert_model_embedding_weights.size(0), bias=False) self.decoder.weight = bert_model_embedding_weights self.bias = nn.Parameter(torch.zeros(bert_model_embedding_weights.size(0)))
Example #23
Source File: train_l2t_ww.py From L2T-ww with MIT License | 5 votes |
def __init__(self, source_model, pairs, weight_type='relu', init=None): super(LossWeightNetwork, self).__init__() n = _get_num_features(source_model) if weight_type == 'const': self.weights = nn.Parameter(torch.zeros(len(pairs))) else: for i, _ in pairs: l = nn.Linear(n[i], 1) if init is not None: nn.init.constant_(l.bias, init) self.append(l) self.pairs = pairs self.weight_type = weight_type
Example #24
Source File: att_fasttext.py From TaskBot with GNU General Public License v3.0 | 5 votes |
def __init__(self, param: dict): super().__init__(param) embed_dim = param['embed_dim'] vocab_size = param['vocab_size'] class_num = param['class_num'] self.embed = nn.Embedding(vocab_size, embed_dim) self.fc = nn.Linear(embed_dim, class_num) self.score_w = nn.Parameter(torch.randn(param['embed_dim'])) self.score_b = nn.Parameter(torch.randn(1))
Example #25
Source File: models_resnet.py From DTN with MIT License | 5 votes |
def __init__(self, num_classes=80, norm=True, scale=True): super(Net,self).__init__() self.extractor = ResNetLike() self.classifier = Classifier(num_classes) self.s = nn.Parameter(torch.FloatTensor([10])) self.norm = norm self.scale = scale
Example #26
Source File: generator.py From DTN with MIT License | 5 votes |
def __init__(self, num_classes=80, norm=True, scale=True): super(GeneratorNet, self).__init__() self.add_info = AddInfo() self.generator = Generator() self.fc = nn.Linear(1024, num_classes, bias=False) self.s = nn.Parameter(torch.FloatTensor([10]))
Example #27
Source File: bn.py From pytorch-segmentation-toolbox with MIT License | 5 votes |
def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True, activation="leaky_relu", slope=0.01): """Creates an InPlace Activated Batch Normalization module Parameters ---------- num_features : int Number of feature channels in the input and output. eps : float Small constant to prevent numerical issues. momentum : float Momentum factor applied to compute running statistics as. affine : bool If `True` apply learned scale and shift transformation after normalization. activation : str Name of the activation functions, one of: `leaky_relu`, `elu` or `none`. slope : float Negative slope for the `leaky_relu` activation. """ super(InPlaceABN, self).__init__() self.num_features = num_features self.affine = affine self.eps = eps self.momentum = momentum self.activation = activation self.slope = slope if self.affine: self.weight = nn.Parameter(torch.Tensor(num_features)) self.bias = nn.Parameter(torch.Tensor(num_features)) else: self.register_parameter('weight', None) self.register_parameter('bias', None) self.register_buffer('running_mean', torch.zeros(num_features)) self.register_buffer('running_var', torch.ones(num_features)) self.reset_parameters()
Example #28
Source File: set2set.py From LanczosNetwork with MIT License | 5 votes |
def __init__(self, element_dim, num_step_encoder): """ Implementation of Set2Vec """ super(Set2Vec, self).__init__() self.element_dim = element_dim self.num_step_encoder = num_step_encoder self.LSTM = Set2SetLSTM(element_dim) self.W_1 = nn.Parameter(torch.ones(self.element_dim, self.element_dim)) self.W_2 = nn.Parameter(torch.ones(self.element_dim, 1)) self.register_parameter('W_1', self.W_1) self.register_parameter('W_2', self.W_2) self._init_param()
Example #29
Source File: text_cnn.py From TaskBot with GNU General Public License v3.0 | 5 votes |
def init_embed(self, embed_matrix): self.embed.weight = nn.Parameter(torch.Tensor(embed_matrix))
Example #30
Source File: prunable_nn.py From prunnable-layers-pytorch with GNU General Public License v3.0 | 5 votes |
def drop_input_channel(self, index): """ Use when a convnet earlier in the chain is pruned. Reduces input channel count :param index: :return: """ is_cuda = self.weight.is_cuda indices = Variable(torch.LongTensor([i for i in range(self.in_channels) if i != index])) indices = indices.cuda() if is_cuda else indices self.weight = nn.Parameter(self.weight.index_select(1, indices).data) self.in_channels -= 1