def __init__(self, name, config): """ Initializes the ``SentenceEmbeddings`` layer. :param name: Name of the model (taken from the configuration file). :param config: Parameters read from configuration file. :type config: ``ptp.configuration.ConfigInterface`` """ # Call base class constructors. Model.__init__(self, name, SentenceEmbeddings, config) WordMappings.__init__(self) # Set key mappings. self.key_inputs = self.stream_keys["inputs"] self.key_outputs = self.stream_keys["outputs"] # Force padding to a fixed length self.fixed_padding = self.config['fixed_padding'] # Retrieve embeddings size from configuration and export it to globals. self.embeddings_size = self.config['embeddings_size'] self.globals["embeddings_size"] = self.embeddings_size # Create the embeddings layer. self.logger.info("Initializing embeddings layer with vocabulary size = {} and embeddings size = {}".format(len(self.word_to_ix), self.embeddings_size)) self.embeddings = torch.nn.Embedding(len(self.word_to_ix), self.embeddings_size, padding_idx=0) # Index of self.word_to_ix['<PAD>'] # Load the embeddings first. if self.config["pretrained_embeddings_file"] != '': emb_vectors = emb.load_pretrained_embeddings(self.logger, self.data_folder, self.config["pretrained_embeddings_file"], self.word_to_ix, self.embeddings_size) self.embeddings.weight = torch.nn.Parameter(emb_vectors) # Get index of <PAD> from vocabulary. self.pad_index = self.word_to_ix['<PAD>']
def __init__(self, name, config): """ Initializes the component. :param name: Component name (read from configuration file). :type name: str :param config: Dictionary of parameters (read from the configuration ``.yaml`` file). :type config: :py:class:`ptp.configuration.ConfigInterface` """ # Call constructor(s) of parent class(es) - in the right order! Component.__init__(self, name, SentenceIndexer, config) WordMappings.__init__(self) # Set key mappings. self.key_inputs = self.stream_keys["inputs"] self.key_outputs = self.stream_keys["outputs"] # Read mode from the configuration. self.mode_reverse = self.config['reverse'] # Force padding to a fixed length self.fixed_padding = self.config['fixed_padding'] # Wether to add <EOS> at the end of sequence self.enable_eos_token = self.config['eos_token'] if self.mode_reverse: # We will need reverse (index:word) mapping. self.ix_to_word = dict((v, k) for k, v in self.word_to_ix.items()) # Get inputs distributions/indices flag. self.use_input_distributions = self.config["use_input_distributions"]
def __init__(self, name, config): """ Initializes the component. :param name: Component name (read from configuration file). :type name: str :param config: Dictionary of parameters (read from the configuration ``.yaml`` file). :type config: :py:class:`ptp.configuration.ConfigInterface` """ # Call constructor(s) of parent class(es). WordMappings.__init__(self, name, SentenceOneHotEncoder, config) # Set key mappings. self.key_inputs = self.stream_keys["inputs"] self.key_outputs = self.stream_keys["outputs"]
def __init__(self, name, config): """ Initializes the component. :param name: Component name (read from configuration file). :type name: str :param config: Dictionary of parameters (read from the configuration ``.yaml`` file). :type config: :py:class:`ptp.configuration.ConfigInterface` """ # Call constructor(s) of parent class(es). WordMappings.__init__(self, name, WordDecoder, config) # Construct reverse mapping for faster processing. self.ix_to_word = dict((v, k) for k, v in self.word_to_ix.items()) # Set key mappings. self.key_inputs = self.stream_keys["inputs"] self.key_outputs = self.stream_keys["outputs"]
def __init__(self, name, config): """ Initializes the component. :param name: Component name (read from configuration file). :type name: str :param config: Dictionary of parameters (read from the configuration ``.yaml`` file). :type config: :py:class:`ptp.configuration.ConfigInterface` """ # Call constructor(s) of parent class(es) - in the right order! Component.__init__(self, name, LabelIndexer, config) WordMappings.__init__(self) # Set key mappings. self.key_inputs = self.stream_keys["inputs"] self.key_outputs = self.stream_keys["outputs"] # Get value from configuration. self.out_of_vocabulary_value = self.config["out_of_vocabulary_value"]