Sequence Labeling

model_partial_ner.basic module

class model_partial_ner.basic.BasicRNN(layer_num, unit, emb_dim, hid_dim, droprate, batch_norm)[source]

The multi-layer recurrent networks for the vanilla stacked RNNs.

Parameters:
  • layer_num (int, required.) – The number of layers.
  • unit (torch.nn.Module, required.) – The type of rnn unit.
  • input_dim (int, required.) – The input dimension fo the unit.
  • hid_dim (int, required.) – The hidden dimension fo the unit.
  • droprate (float, required.) – The dropout ratrio.
  • batch_norm (bool, required.) – Incorporate batch norm or not.
forward(x)[source]

Calculate the output.

Parameters:x (torch.LongTensor, required.) – the input tensor, of shape (seq_len, batch_size, input_dim).
Returns:output – The output of RNNs.
Return type:torch.FloatTensor.
init_hidden()[source]

Initialize hidden states.

rand_ini()[source]

Random Initialization.

to_params()[source]

To parameters.

class model_partial_ner.basic.BasicUnit(unit, input_dim, hid_dim, droprate, batch_norm)[source]

The basic recurrent unit for the vanilla stacked RNNs.

Parameters:
  • unit (torch.nn.Module, required.) – The type of rnn unit.
  • input_dim (int, required.) – The input dimension fo the unit.
  • hid_dim (int, required.) – The hidden dimension fo the unit.
  • droprate (float, required.) – The dropout ratrio.
  • batch_norm (bool, required.) – Incorporate batch norm or not.
forward(x)[source]

Calculate the output.

Parameters:x (torch.LongTensor, required.) – the input tensor, of shape (seq_len, batch_size, input_dim).
Returns:output – The output of RNNs.
Return type:torch.FloatTensor.
init_hidden()[source]

Initialize hidden states.

rand_ini()[source]

Random Initialization.

model_partial_ner.dataset module

model_partial_ner.highway module

class model_partial_ner.highway.highway(size, num_layers=1, droprate=0.5)[source]

Highway layers

Parameters:
  • size (int, required.) – Input and output dimension.
  • num_layers (int, required.) – Number of layers.
  • droprate (float, required.) – Dropout ratio
forward(x)[source]

update statics for f1 score

Parameters:(ins_num, hidden_dim) (x) –
Returns:output – output tensor (ins_num, hidden_dim)
Return type:torch.FloatTensor.
rand_ini()[source]

random initialization

model_partial_ner.ner module

model_partial_ner.object module

model_partial_ner.object.hinge_loss(score, label)[source]

Hinge loss for distant supervision.

class model_partial_ner.object.softCE(if_average=True)[source]

The objective function for the distant supervised typing.

Parameters:if_average (bool, optional, (default = True).) – Whether to average over batches or not.
forward(scores, target)[source]

Calculate the cross entropy loss for distant supervision.

Parameters:
  • scores (torch.FloatTensor, required.) – The input of the softmax.
  • target (torch.ByteTensor , required.) – The target as the mask for the softmax input.
static soft_max(vec, mask)[source]

Calculate the softmax for the input with regard to a mask.

Parameters:
  • vec (torch.FloatTensor, required.) – The input of the softmax.
  • mask (torch.ByteTensor , required.) – The mask for the softmax input.

model_partial_ner.utils module

model_partial_ner.utils.adjust_learning_rate(optimizer, lr)[source]

Shrink learning rate for pytorch

model_partial_ner.utils.evaluate_chunking(iterator, ner_model, none_idx)[source]

Evaluate the chunking performance.

Parameters:
  • iterator (iterator, required.) – Dataset loader.
  • ner_model (torch.nn.Module , required.) – Sequence labeling model for evaluation.
  • none_idx (int, required.) – The index for the not-target-type entities.
model_partial_ner.utils.evaluate_ner(iterator, ner_model, none_idx, id2label)[source]

Evaluate the NER performance.

Parameters:
  • iterator (iterator, required.) – Dataset loader.
  • ner_model (torch.nn.Module , required.) – Sequence labeling model for evaluation.
  • none_idx (int, required.) – The index for the not-target-type entities.
model_partial_ner.utils.evaluate_typing(iterator, ner_model, none_idx)[source]

Evaluate the typing performance.

Parameters:
  • iterator (iterator, required.) – Dataset loader.
  • ner_model (torch.nn.Module , required.) – Sequence labeling model for evaluation.
  • none_idx (int, required.) – The index for the not-target-type entities.
model_partial_ner.utils.init_embedding(input_embedding)[source]

Initialize embedding

model_partial_ner.utils.init_linear(input_linear)[source]

Initialize linear transformation

model_partial_ner.utils.init_lstm(input_lstm)[source]

Initialize lstm

model_partial_ner.utils.to_scalar(var)[source]

Turn the first element of a tensor to scalar