CRSLab¶
crslab.config package¶
Submodules¶
-
class
crslab.config.config.
Config
(config_file, gpu='-1', debug=False)[source]¶ Bases:
object
Configurator module that load the defined parameters.
Load parameters and set log level.
- Parameters
config_file (str) – path to the config file, which should be in
yaml
format. You can use default config provided in the Github repo, or write it by yourself.debug (bool, optional) – whether to enable debug function during running. Defaults to False.
Module contents¶
Config module which loads parameters for the whole system.
-
crslab.config.
SAVE_PATH
¶ where system to save.
- Type
str
-
crslab.config.
DATASET_PATH
¶ where dataset to save.
- Type
str
-
crslab.config.
MODEL_PATH
¶ where model related data to save.
- Type
str
-
crslab.config.
PRETRAIN_PATH
¶ where pretrained model to save.
- Type
str
-
crslab.config.
EMBEDDING_PATH
¶ where pretrained embedding to save, used for evaluate embedding related metrics.
- Type
str
crslab.data package¶
Subpackages¶
crslab.data.dataloader package¶
Submodules¶
-
class
crslab.data.dataloader.base.
BaseDataLoader
(opt, dataset)[source]¶ Bases:
abc.ABC
Abstract class of dataloader
Notes
'scale'
can be set in config to limit the size of dataset.- Parameters
opt (Config or dict) – config for dataloader or the whole system.
dataset – dataset
-
conv_batchify
(batch)[source]¶ batchify data for conversation after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train conversation part.
-
conv_interact
(data)[source]¶ Process user input data for system to converse.
- Parameters
data – user input data.
- Returns
data for system in converse.
-
conv_process_fn
()[source]¶ Process whole data for conversation before batch_fn.
- Returns
processed dataset. Defaults to return the same as self.dataset.
-
get_conv_data
(batch_size, shuffle=True)[source]¶ get_data wrapper for conversation.
You can implement your own process_fn in
conv_process_fn
, batch_fn inconv_batchify
.- Parameters
batch_size (int) –
shuffle (bool, optional) – Defaults to True.
- Yields
tuple or dict of torch.Tensor – batch data for conversation.
-
get_data
(batch_fn, batch_size, shuffle=True, process_fn=None)[source]¶ Collate batch data for system to fit
- Parameters
batch_fn (func) – function to collate data
batch_size (int) –
shuffle (bool, optional) – Defaults to True.
process_fn (func, optional) – function to process dataset before batchify. Defaults to None.
- Yields
tuple or dict of torch.Tensor – batch data for system to fit
-
get_policy_data
(batch_size, shuffle=True)[source]¶ get_data wrapper for policy.
You can implement your own process_fn in
self.policy_process_fn
, batch_fn inpolicy_batchify
.- Parameters
batch_size (int) –
shuffle (bool, optional) – Defaults to True.
- Yields
tuple or dict of torch.Tensor – batch data for policy.
-
get_rec_data
(batch_size, shuffle=True)[source]¶ get_data wrapper for recommendation.
You can implement your own process_fn in
rec_process_fn
, batch_fn inrec_batchify
.- Parameters
batch_size (int) –
shuffle (bool, optional) – Defaults to True.
- Yields
tuple or dict of torch.Tensor – batch data for recommendation.
-
policy_batchify
(batch)[source]¶ batchify data for policy after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train policy part.
-
policy_process_fn
()[source]¶ Process whole data for policy before batch_fn.
- Returns
processed dataset. Defaults to return the same as self.dataset.
-
rec_batchify
(batch)[source]¶ batchify data for recommendation after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train recommendation part.
-
rec_interact
(data)[source]¶ process user input data for system to recommend.
- Parameters
data – user input data.
- Returns
data for system to recommend.
-
class
crslab.data.dataloader.kbrd.
KBRDDataLoader
(opt, dataset, vocab)[source]¶ Bases:
crslab.data.dataloader.base.BaseDataLoader
Dataloader for model KBRD.
Notes
You can set the following parameters in config:
'context_truncate'
: the maximum length of context.'response_truncate'
: the maximum length of response.'entity_truncate'
: the maximum length of mentioned entities in context.
The following values must be specified in
vocab
:'pad'
'start'
'end'
'pad_entity'
the above values specify the id of needed special token.
- Parameters
opt (Config or dict) – config for dataloader or the whole system.
dataset – data for model.
vocab (dict) – all kinds of useful size, idx and map between token and idx.
-
conv_batchify
(batch)[source]¶ batchify data for conversation after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train conversation part.
-
conv_process_fn
(*args, **kwargs)[source]¶ Process whole data for conversation before batch_fn.
- Returns
processed dataset. Defaults to return the same as self.dataset.
-
policy_batchify
(*args, **kwargs)[source]¶ batchify data for policy after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train policy part.
-
class
crslab.data.dataloader.kgsf.
KGSFDataLoader
(opt, dataset, vocab)[source]¶ Bases:
crslab.data.dataloader.base.BaseDataLoader
Dataloader for model KGSF.
Notes
You can set the following parameters in config:
'context_truncate'
: the maximum length of context.'response_truncate'
: the maximum length of response.'entity_truncate'
: the maximum length of mentioned entities in context.'word_truncate'
: the maximum length of mentioned words in context.
The following values must be specified in
vocab
:'pad'
'start'
'end'
'pad_entity'
'pad_word'
the above values specify the id of needed special token.
'n_entity'
: the number of entities in the entity KG of dataset.
- Parameters
opt (Config or dict) – config for dataloader or the whole system.
dataset – data for model.
vocab (dict) – all kinds of useful size, idx and map between token and idx.
-
conv_batchify
(batch)[source]¶ batchify data for conversation after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train conversation part.
-
conv_process_fn
(*args, **kwargs)[source]¶ Process whole data for conversation before batch_fn.
- Returns
processed dataset. Defaults to return the same as self.dataset.
-
policy_batchify
(*args, **kwargs)[source]¶ batchify data for policy after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train policy part.
-
class
crslab.data.dataloader.redial.
ReDialDataLoader
(opt, dataset, vocab)[source]¶ Bases:
crslab.data.dataloader.base.BaseDataLoader
Dataloader for model ReDial.
Notes
You can set the following parameters in config:
'utterance_truncate'
: the maximum length of a single utterance.'conversation_truncate'
: the maximum length of the whole conversation.
The following values must be specified in
vocab
:'pad'
'start'
'end'
'unk'
the above values specify the id of needed special token.
'ind2tok'
: map from index to token.'n_entity'
: number of entities in the entity KG of dataset.'vocab_size'
: size of vocab.
- Parameters
opt (Config or dict) – config for dataloader or the whole system.
dataset – data for model.
vocab (dict) – all kinds of useful size, idx and map between token and idx.
-
conv_batchify
(batch)[source]¶ batchify data for conversation after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train conversation part.
-
conv_process_fn
()[source]¶ Process whole data for conversation before batch_fn.
- Returns
processed dataset. Defaults to return the same as self.dataset.
-
policy_batchify
(batch)[source]¶ batchify data for policy after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train policy part.
-
class
crslab.data.dataloader.tgredial.
TGReDialDataLoader
(opt, dataset, vocab)[source]¶ Bases:
crslab.data.dataloader.base.BaseDataLoader
Dataloader for model TGReDial.
Notes
You can set the following parameters in config:
'context_truncate'
: the maximum length of context.'response_truncate'
: the maximum length of response.'entity_truncate'
: the maximum length of mentioned entities in context.'word_truncate'
: the maximum length of mentioned words in context.'item_truncate'
: the maximum length of mentioned items in context.
The following values must be specified in
vocab
:'pad'
'start'
'end'
'unk'
'pad_entity'
'pad_word'
the above values specify the id of needed special token.
'ind2tok'
: map from index to token.'tok2ind'
: map from token to index.'vocab_size'
: size of vocab.'id2entity'
: map from index to entity.'n_entity'
: number of entities in the entity KG of dataset.'sent_split'
(optional): token used to split sentence. Defaults to'end'
.'word_split'
(optional): token used to split word. Defaults to'end'
.'pad_topic'
(optional): token used to pad topic.'ind2topic'
(optional): map from index to topic.
- Parameters
opt (Config or dict) – config for dataloader or the whole system.
dataset – data for model.
vocab (dict) – all kinds of useful size, idx and map between token and idx.
-
conv_batchify
(batch)[source]¶ batchify data for conversation after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train conversation part.
-
conv_interact
(data)[source]¶ Process user input data for system to converse.
- Parameters
data – user input data.
- Returns
data for system in converse.
-
policy_batchify
(batch)[source]¶ batchify data for policy after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train policy part.
-
policy_process_fn
(*args, **kwargs)[source]¶ Process whole data for policy before batch_fn.
- Returns
processed dataset. Defaults to return the same as self.dataset.
-
rec_batchify
(batch)[source]¶ batchify data for recommendation after process.
- Parameters
batch (list) – processed batch dataset.
- Returns
batch data for the system to train recommendation part.
-
crslab.data.dataloader.utils.
add_start_end_token_idx
(vec: list, start_token_idx: Optional[int] = None, end_token_idx: Optional[int] = None)[source]¶ Can choose to add start token in the beginning and end token in the end.
- Parameters
vec – source list composed of indexes.
start_token_idx – index of start token.
end_token_idx – index of end token.
- Returns
list added start or end token index.
- Return type
list
-
crslab.data.dataloader.utils.
get_onehot
(data_list, categories) → torch.Tensor[source]¶ Transform lists of label into one-hot.
- Parameters
data_list (list of list of int) – source data.
categories (int) – #label class.
- Returns
one-hot labels.
- Return type
torch.Tensor
-
crslab.data.dataloader.utils.
merge_utt
(conversation, split_token_idx=None, keep_split_in_tail=False, final_token_idx=None)[source]¶ merge utterances in one conversation.
- Parameters
conversation (list of list of int) – conversation consist of utterances consist of tokens.
split_token_idx (int) – index of split token. Defaults to None.
keep_split_in_tail (bool) – split in tail or head. Defaults to False.
final_token_idx (int) – index of final token. Defaults to None.
- Returns
tokens of all utterances in one list.
- Return type
list
-
crslab.data.dataloader.utils.
merge_utt_replace
(conversation, detect_token=None, replace_token=None, method='in')[source]¶
-
crslab.data.dataloader.utils.
padded_tensor
(items: List[Union[List[int], torch.LongTensor]], pad_idx: int = 0, pad_tail: bool = True, max_len: Optional[int] = None) → torch.LongTensor[source]¶ Create a padded matrix from an uneven list of lists.
Returns padded matrix.
Matrix is right-padded (filled to the right) by default, but can be left padded if the flag is set to True.
Matrix can also be placed on cuda automatically.
- Parameters
items (list[iter[int]]) – List of items
pad_idx (int) – the value to use for padding
pad_tail (bool) –
max_len (int) – if None, the max length is the maximum item length
- Returns
padded tensor.
- Return type
Tensor[int64]
Module contents¶
crslab.data.dataset package¶
Subpackages¶
crslab.data.dataset.durecdial package¶
References
Liu, Zeming, et al. “Towards Conversational Recommendation over Multi-Type Dialogs.” in ACL 2020.
-
class
crslab.data.dataset.durecdial.durecdial.
DuRecDialDataset
(opt, tokenize, restore=False, save=False)[source]¶ Bases:
crslab.data.dataset.base.BaseDataset
-
train_data
¶ train dataset.
-
valid_data
¶ valid dataset.
-
test_data
¶ test dataset.
-
vocab
¶ { 'tok2ind': map from token to index, 'ind2tok': map from index to token, 'entity2id': map from entity to index, 'id2entity': map from index to entity, 'word2id': map from word to index, 'vocab_size': len(self.tok2ind), 'n_entity': max(self.entity2id.values()) + 1, 'n_word': max(self.word2id.values()) + 1, }
- Type
dict
Notes
'unk'
must be specified in'special_token_idx'
inresources.py
.- Parameters
opt (Config or dict) – config for dataset or the whole system.
tokenize (str) – how to tokenize dataset.
restore (bool) – whether to restore saved dataset which has been processed. Defaults to False.
save (bool) – whether to save dataset after processing. Defaults to False.
-
crslab.data.dataset.gorecdial package¶
References
Kang, Dongyeop, et al. “Recommendation as a Communication Game: Self-Supervised Bot-Play for Goal-oriented Dialogue.” in EMNLP 2019.
-
class
crslab.data.dataset.gorecdial.gorecdial.
GoRecDialDataset
(opt, tokenize, restore=False, save=False)[source]¶ Bases:
crslab.data.dataset.base.BaseDataset
-
train_data
¶ train dataset.
-
valid_data
¶ valid dataset.
-
test_data
¶ test dataset.
-
vocab
¶ { 'tok2ind': map from token to index, 'ind2tok': map from index to token, 'entity2id': map from entity to index, 'id2entity': map from index to entity, 'word2id': map from word to index, 'vocab_size': len(self.tok2ind), 'n_entity': max(self.entity2id.values()) + 1, 'n_word': max(self.word2id.values()) + 1, }
- Type
dict
Notes
'unk'
must be specified in'special_token_idx'
inresources.py
.Specify tokenized resource and init base dataset.
- Parameters
opt (Config or dict) – config for dataset or the whole system.
tokenize (str) – how to tokenize dataset.
restore (bool) – whether to restore saved dataset which has been processed. Defaults to False.
save (bool) – whether to save dataset after processing. Defaults to False.
-
crslab.data.dataset.inspired package¶
References
Hayati, Shirley Anugrah, et al. “INSPIRED: Toward Sociable Recommendation Dialog Systems.” in EMNLP 2020.
-
class
crslab.data.dataset.inspired.inspired.
InspiredDataset
(opt, tokenize, restore=False, save=False)[source]¶ Bases:
crslab.data.dataset.base.BaseDataset
-
train_data
¶ train dataset.
-
valid_data
¶ valid dataset.
-
test_data
¶ test dataset.
-
vocab
¶ { 'tok2ind': map from token to index, 'ind2tok': map from index to token, 'entity2id': map from entity to index, 'id2entity': map from index to entity, 'word2id': map from word to index, 'vocab_size': len(self.tok2ind), 'n_entity': max(self.entity2id.values()) + 1, 'n_word': max(self.word2id.values()) + 1, }
- Type
dict
Notes
'unk'
must be specified in'special_token_idx'
inresources.py
.Specify tokenized resource and init base dataset.
- Parameters
opt (Config or dict) – config for dataset or the whole system.
tokenize (str) – how to tokenize dataset.
restore (bool) – whether to restore saved dataset which has been processed. Defaults to False.
save (bool) – whether to save dataset after processing. Defaults to False.
-
crslab.data.dataset.opendialkg package¶
References
Moon, Seungwhan, et al. “Opendialkg: Explainable conversational reasoning with attention-based walks over knowledge graphs.” in ACL 2019.
-
class
crslab.data.dataset.opendialkg.opendialkg.
OpenDialKGDataset
(opt, tokenize, restore=False, save=False)[source]¶ Bases:
crslab.data.dataset.base.BaseDataset
-
train_data
¶ train dataset.
-
valid_data
¶ valid dataset.
-
test_data
¶ test dataset.
-
vocab
¶ { 'tok2ind': map from token to index, 'ind2tok': map from index to token, 'entity2id': map from entity to index, 'id2entity': map from index to entity, 'word2id': map from word to index, 'vocab_size': len(self.tok2ind), 'n_entity': max(self.entity2id.values()) + 1, 'n_word': max(self.word2id.values()) + 1, }
- Type
dict
Notes
'unk'
must be specified in'special_token_idx'
inresources.py
.Specify tokenized resource and init base dataset.
- Parameters
opt (Config or dict) – config for dataset or the whole system.
tokenize (str) – how to tokenize dataset.
restore (bool) – whether to restore saved dataset which has been processed. Defaults to False.
save (bool) – whether to save dataset after processing. Defaults to False.
-
crslab.data.dataset.redial package¶
References
Li, Raymond, et al. “Towards deep conversational recommendations.” in NeurIPS 2018.
-
class
crslab.data.dataset.redial.redial.
ReDialDataset
(opt, tokenize, restore=False, save=False)[source]¶ Bases:
crslab.data.dataset.base.BaseDataset
-
train_data
¶ train dataset.
-
valid_data
¶ valid dataset.
-
test_data
¶ test dataset.
-
vocab
¶ { 'tok2ind': map from token to index, 'ind2tok': map from index to token, 'entity2id': map from entity to index, 'id2entity': map from index to entity, 'word2id': map from word to index, 'vocab_size': len(self.tok2ind), 'n_entity': max(self.entity2id.values()) + 1, 'n_word': max(self.word2id.values()) + 1, }
- Type
dict
Notes
'unk'
must be specified in'special_token_idx'
inresources.py
.Specify tokenized resource and init base dataset.
- Parameters
opt (Config or dict) – config for dataset or the whole system.
tokenize (str) – how to tokenize dataset.
restore (bool) – whether to restore saved dataset which has been processed. Defaults to False.
save (bool) – whether to save dataset after processing. Defaults to False.
-
crslab.data.dataset.tgredial package¶
References
Zhou, Kun, et al. “Towards Topic-Guided Conversational Recommender System.” in COLING 2020.
-
class
crslab.data.dataset.tgredial.tgredial.
TGReDialDataset
(opt, tokenize, restore=False, save=False)[source]¶ Bases:
crslab.data.dataset.base.BaseDataset
-
train_data
¶ train dataset.
-
valid_data
¶ valid dataset.
-
test_data
¶ test dataset.
-
vocab
¶ { 'tok2ind': map from token to index, 'ind2tok': map from index to token, 'topic2ind': map from topic to index, 'ind2topic': map from index to topic, 'entity2id': map from entity to index, 'id2entity': map from index to entity, 'word2id': map from word to index, 'vocab_size': len(self.tok2ind), 'n_topic': len(self.topic2ind) + 1, 'n_entity': max(self.entity2id.values()) + 1, 'n_word': max(self.word2id.values()) + 1, }
- Type
dict
Notes
'unk'
and'pad_topic'
must be specified in'special_token_idx'
inresources.py
.Specify tokenized resource and init base dataset.
- Parameters
opt (Config or dict) – config for dataset or the whole system.
tokenize (str) – how to tokenize dataset.
restore (bool) – whether to restore saved dataset which has been processed. Defaults to False.
save (bool) – whether to save dataset after processing. Defaults to False.
-
Submodules¶
Module contents¶
Module contents¶
Data module which reads, processes and batches data for the whole system
-
crslab.data.
dataset_register_table
¶ record all supported dataset
- Type
dict
-
crslab.data.
dataset_language_map
¶ record all dataset corresponding language
- Type
dict
-
crslab.data.
dataloader_register_table
¶ record all model corresponding dataloader
- Type
dict
-
crslab.data.
get_dataloader
(opt, dataset, vocab) → crslab.data.dataloader.base.BaseDataLoader[source]¶ get dataloader to batchify dataset
- Parameters
opt (Config or dict) – config for dataloader or the whole system.
dataset – processed raw data, no side data.
vocab (dict) – all kinds of useful size, idx and map between token and idx.
- Returns
dataloader
-
crslab.data.
get_dataset
(opt, tokenize, restore, save) → crslab.data.dataset.base.BaseDataset[source]¶ get and process dataset
- Parameters
opt (Config or dict) – config for dataset or the whole system.
tokenize (str) – how to tokenize the dataset.
restore (bool) – whether to restore saved dataset which has been processed.
save (bool) – whether to save dataset after processing.
- Returns
processed dataset
crslab.model package¶
Subpackages¶
crslab.model.conversation package¶
Subpackages¶
crslab.model.conversation.gpt2 package¶
References
Radford, Alec, et al. “Language Models are Unsupervised Multitask Learners.”.
-
class
crslab.model.conversation.gpt2.gpt2.
GPT2Model
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
context_truncate
¶ A integer indicating the length of dialogue context.
-
response_truncate
¶ A integer indicating the length of dialogue response.
-
pad_id
¶ A integer indicating the id of padding token.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
calculate_loss
(logit, labels)[source]¶ - Parameters
preds – torch.FloatTensor, shape=(bs, response_truncate, vocab_size)
labels – torch.LongTensor, shape=(bs, response_truncate)
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
crslab.model.conversation.transformer package¶
References
Zhou, Kun, et al. “Towards Topic-Guided Conversational Recommender System.” in COLING 2020.
-
class
crslab.model.conversation.transformer.transformer.
TransformerModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
vocab_size
¶ A integer indicating the vocabulary size.
-
pad_token_idx
¶ A integer indicating the id of padding token.
-
start_token_idx
¶ A integer indicating the id of start token.
-
end_token_idx
¶ A integer indicating the id of end token.
-
token_emb_dim
¶ A integer indicating the dimension of token embedding layer.
-
pretrain_embedding
¶ A string indicating the path of pretrained embedding.
-
n_word
¶ A integer indicating the number of words.
-
n_entity
¶ A integer indicating the number of entities.
-
pad_word_idx
¶ A integer indicating the id of word padding.
-
pad_entity_idx
¶ A integer indicating the id of entity padding.
-
num_bases
¶ A integer indicating the number of bases.
-
kg_emb_dim
¶ A integer indicating the dimension of kg embedding.
-
n_heads
¶ A integer indicating the number of heads.
-
n_layers
¶ A integer indicating the number of layer.
-
ffn_size
¶ A integer indicating the size of ffn hidden.
-
dropout
¶ A float indicating the drouput rate.
-
attention_dropout
¶ A integer indicating the drouput rate of attention layer.
-
relu_dropout
¶ A integer indicating the drouput rate of relu layer.
-
learn_positional_embeddings
¶ A boolean indicating if we learn the positional embedding.
-
embeddings_scale
¶ A boolean indicating if we use the embeddings scale.
-
reduction
¶ A boolean indicating if we use the reduction.
-
n_positions
¶ A integer indicating the number of position.
-
longest_label
¶ A integer indicating the longest length for response generation.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
Module contents¶
crslab.model.crs package¶
Subpackages¶
crslab.model.crs.kbrd package¶
References
Chen, Qibin, et al. “Towards Knowledge-Based Recommender Dialog System.” in EMNLP 2019.
-
class
crslab.model.crs.kbrd.kbrd.
KBRDModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
vocab_size
¶ A integer indicating the vocabulary size.
-
pad_token_idx
¶ A integer indicating the id of padding token.
-
start_token_idx
¶ A integer indicating the id of start token.
-
end_token_idx
¶ A integer indicating the id of end token.
-
token_emb_dim
¶ A integer indicating the dimension of token embedding layer.
-
pretrain_embedding
¶ A string indicating the path of pretrained embedding.
-
n_entity
¶ A integer indicating the number of entities.
-
n_relation
¶ A integer indicating the number of relation in KG.
-
num_bases
¶ A integer indicating the number of bases.
-
kg_emb_dim
¶ A integer indicating the dimension of kg embedding.
-
user_emb_dim
¶ A integer indicating the dimension of user embedding.
-
n_heads
¶ A integer indicating the number of heads.
-
n_layers
¶ A integer indicating the number of layer.
-
ffn_size
¶ A integer indicating the size of ffn hidden.
-
dropout
¶ A float indicating the dropout rate.
-
attention_dropout
¶ A integer indicating the dropout rate of attention layer.
-
relu_dropout
¶ A integer indicating the dropout rate of relu layer.
-
learn_positional_embeddings
¶ A boolean indicating if we learn the positional embedding.
-
embeddings_scale
¶ A boolean indicating if we use the embeddings scale.
-
reduction
¶ A boolean indicating if we use the reduction.
-
n_positions
¶ A integer indicating the number of position.
-
longest_label
¶ A integer indicating the longest length for response generation.
-
user_proj_dim
¶ A integer indicating dim to project for user embedding.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
converse
(batch, mode)[source]¶ calculate loss and prediction of conversation for batch under certain mode
- Parameters
batch (dict or tuple) – batch data
mode (str, optional) – train/valid/test.
-
forward
(batch, mode, stage)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
crslab.model.crs.kgsf package¶
References
Zhou, Kun, et al. “Improving Conversational Recommender Systems via Knowledge Graph based Semantic Fusion.” in KDD 2020.
-
class
crslab.model.crs.kgsf.kgsf.
KGSFModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
vocab_size
¶ A integer indicating the vocabulary size.
-
pad_token_idx
¶ A integer indicating the id of padding token.
-
start_token_idx
¶ A integer indicating the id of start token.
-
end_token_idx
¶ A integer indicating the id of end token.
-
token_emb_dim
¶ A integer indicating the dimension of token embedding layer.
-
pretrain_embedding
¶ A string indicating the path of pretrained embedding.
-
n_word
¶ A integer indicating the number of words.
-
n_entity
¶ A integer indicating the number of entities.
-
pad_word_idx
¶ A integer indicating the id of word padding.
-
pad_entity_idx
¶ A integer indicating the id of entity padding.
-
num_bases
¶ A integer indicating the number of bases.
-
kg_emb_dim
¶ A integer indicating the dimension of kg embedding.
-
n_heads
¶ A integer indicating the number of heads.
-
n_layers
¶ A integer indicating the number of layer.
-
ffn_size
¶ A integer indicating the size of ffn hidden.
-
dropout
¶ A float indicating the dropout rate.
-
attention_dropout
¶ A integer indicating the dropout rate of attention layer.
-
relu_dropout
¶ A integer indicating the dropout rate of relu layer.
-
learn_positional_embeddings
¶ A boolean indicating if we learn the positional embedding.
-
embeddings_scale
¶ A boolean indicating if we use the embeddings scale.
-
reduction
¶ A boolean indicating if we use the reduction.
-
n_positions
¶ A integer indicating the number of position.
-
response_truncate = A integer indicating the longest length for response generation.
-
pretrained_embedding
¶ A string indicating the path of pretrained embedding.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
converse
(batch, mode)[source]¶ calculate loss and prediction of conversation for batch under certain mode
- Parameters
batch (dict or tuple) – batch data
mode (str, optional) – train/valid/test.
-
forward
(batch, stage, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.crs.kgsf.modules.
GateLayer
(input_dim)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(input1, input2)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.crs.kgsf.modules.
TransformerDecoderKG
(n_heads, n_layers, embedding_size, ffn_size, vocabulary_size, embedding, dropout=0.0, attention_dropout=0.0, relu_dropout=0.0, embeddings_scale=True, learn_positional_embeddings=False, padding_idx=None, n_positions=1024)[source]¶ Bases:
torch.nn.modules.module.Module
Transformer Decoder layer.
- Parameters
n_heads (int) – the number of multihead attention heads.
n_layers (int) – number of transformer layers.
embedding_size (int) – the embedding sizes. Must be a multiple of n_heads.
ffn_size (int) – the size of the hidden layer in the FFN
embedding – an embedding matrix for the bottom layer of the transformer. If none, one is created for this encoder.
dropout (float) – Dropout used around embeddings and before layer layer normalizations. This is used in Vaswani 2017 and works well on large datasets.
attention_dropout (float) – Dropout performed after the multhead attention softmax. This is not used in Vaswani 2017.
relu_dropout (float) – Dropout used after the ReLU in the FFN. Not used in Vaswani 2017, but used in Tensor2Tensor.
padding_idx (int) – Reserved padding index in the embeddings matrix.
learn_positional_embeddings (bool) – If off, sinusoidal embeddings are used. If on, position embeddings are learned from scratch.
embeddings_scale (bool) – Scale embeddings relative to their dimensionality. Found useful in fairseq.
n_positions (int) – Size of the position embeddings matrix.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(input, encoder_state, kg_encoder_output, kg_encoder_mask, db_encoder_output, db_encoder_mask, incr_state=None)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
class
crslab.model.crs.kgsf.modules.
TransformerDecoderLayerKG
(n_heads, embedding_size, ffn_size, attention_dropout=0.0, relu_dropout=0.0, dropout=0.0)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(x, encoder_output, encoder_mask, kg_encoder_output, kg_encoder_mask, db_encoder_output, db_encoder_mask)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
crslab.model.crs.redial package¶
-
class
crslab.model.crs.redial.modules.
HRNN
(utterance_encoder_hidden_size, dialog_encoder_hidden_size, dialog_encoder_num_layers, pad_token_idx, embedding=None, use_dropout=False, dropout=0.3)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
class
crslab.model.crs.redial.modules.
SwitchingDecoder
(hidden_size, context_size, num_layers, vocab_size, embedding, pad_token_idx)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
References
Li, Raymond, et al. “Towards deep conversational recommendations.” in NeurIPS.
-
class
crslab.model.crs.redial.redial_conv.
ReDialConvModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
vocab_size
¶ A integer indicating the vocabulary size.
-
pad_token_idx
¶ A integer indicating the id of padding token.
-
start_token_idx
¶ A integer indicating the id of start token.
-
end_token_idx
¶ A integer indicating the id of end token.
-
unk_token_idx
¶ A integer indicating the id of unk token.
-
pretrained_embedding
¶ A string indicating the path of pretrained embedding.
-
embedding_dim
¶ A integer indicating the dimension of item embedding.
A integer indicating the size of hidden size in utterance encoder.
A integer indicating the size of hidden size in dialog encoder.
-
dialog_encoder_num_layers
¶ A integer indicating the number of layers in dialog encoder.
-
use_dropout
¶ A boolean indicating if we use the dropout.
-
dropout
¶ A float indicating the dropout rate.
A integer indicating the size of hidden size in decoder.
-
decoder_num_layers
¶ A integer indicating the number of layer in decoder.
-
decoder_embedding_dim
¶ A integer indicating the dimension of embedding in decoder.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ - Parameters
batch –
{ 'context': (batch_size, max_context_length, max_utterance_length), 'context_lengths': (batch_size), 'utterance_lengths': (batch_size, max_context_length), 'request': (batch_size, max_utterance_length), 'request_lengths': (batch_size), 'response': (batch_size, max_utterance_length) }
-
References
Li, Raymond, et al. “Towards deep conversational recommendations.” in NeurIPS.
-
class
crslab.model.crs.redial.redial_rec.
ReDialRecModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
n_entity
¶ A integer indicating the number of entities.
-
layer_sizes
¶ A integer indicating the size of layer in autorec.
-
pad_entity_idx
¶ A integer indicating the id of entity padding.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
crslab.model.crs.tgredial package¶
References
Zhou, Kun, et al. “Towards Topic-Guided Conversational Recommender System.” in COLING 2020.
-
class
crslab.model.crs.tgredial.tg_conv.
TGConvModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
context_truncate
¶ A integer indicating the length of dialogue context.
-
response_truncate
¶ A integer indicating the length of dialogue response.
-
pad_id
¶ A integer indicating the id of padding token.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
calculate_loss
(logit, labels)[source]¶ - Parameters
preds – torch.FloatTensor, shape=(bs, response_truncate, vocab_size)
labels – torch.LongTensor, shape=(bs, response_truncate)
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
References
Zhou, Kun, et al. “Towards Topic-Guided Conversational Recommender System.” in COLING 2020.
-
class
crslab.model.crs.tgredial.tg_policy.
TGPolicyModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
References
Zhou, Kun, et al. “Towards Topic-Guided Conversational Recommender System.” in COLING 2020.
-
class
crslab.model.crs.tgredial.tg_rec.
TGRecModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
A float indicating the dropout rate to dropout hidden state in SASRec.
-
initializer_range
¶ A float indicating the range of parameters initization in SASRec.
A integer indicating the size of hidden state in SASRec.
-
max_seq_length
¶ A integer indicating the max interaction history length.
-
item_size
¶ A integer indicating the number of items.
-
num_attention_heads
¶ A integer indicating the head number in SASRec.
-
attention_probs_dropout_prob
¶ A float indicating the dropout rate in attention layers.
A string indicating the activation function type in SASRec.
A integer indicating the number of hidden layers in SASRec.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
Module contents¶
crslab.model.policy package¶
Subpackages¶
crslab.model.policy.conv_bert package¶
References
Zhou, Kun, et al. “Towards Topic-Guided Conversational Recommender System.” in COLING 2020.
-
class
crslab.model.policy.conv_bert.conv_bert.
ConvBERTModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
topic_class_num
¶ the number of topic.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
crslab.model.policy.mgcg package¶
References
Liu, Zeming, et al. “Towards Conversational Recommendation over Multi-Type Dialogs.” in ACL 2020.
-
class
crslab.model.policy.mgcg.mgcg.
MGCGModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
topic_class_num
¶ A integer indicating the number of topic.
-
vocab_size
¶ A integer indicating the size of vocabulary.
-
embedding_dim
¶ A integer indicating the dimension of embedding layer.
A integer indicating the size of hidden state.
-
num_layers
¶ A integer indicating the number of layers in GRU.
A float indicating the dropout rate of hidden state.
-
n_sent
¶ A integer indicating sequence length in user profile.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
crslab.model.policy.pmi package¶
-
class
crslab.model.policy.pmi.pmi.
PMIModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
topic_class_num
¶ A integer indicating the number of topic.
-
pad_topic
¶ A integer indicating the id of topic padding.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
crslab.model.policy.profile_bert package¶
References
Zhou, Kun, et al. “Towards Topic-Guided Conversational Recommender System.” in COLING 2020.
-
class
crslab.model.policy.profile_bert.profile_bert.
ProfileBERTModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
topic_class_num
¶ A integer indicating the number of topic.
-
n_sent
¶ A integer indicating sequence length in user profile.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
crslab.model.policy.topic_bert package¶
References
Zhou, Kun, et al. “Towards Topic-Guided Conversational Recommender System.” in COLING 2020.
-
class
crslab.model.policy.topic_bert.topic_bert.
TopicBERTModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
topic_class_num
¶ A integer indicating the number of topic.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
Module contents¶
crslab.model.recommendation package¶
Subpackages¶
crslab.model.recommendation.bert package¶
References
Devlin, Jacob, et al. “BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding.” in NAACL 2019.
-
class
crslab.model.recommendation.bert.bert.
BERTModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
item_size
¶ A integer indicating the number of items.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode='train')[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
crslab.model.recommendation.gru4rec package¶
References
Hidasi, Balázs, et al. “Session-Based Recommendations with Recurrent Neural Networks.” in ICLR 2016.
-
class
crslab.model.recommendation.gru4rec.gru4rec.
GRU4RECModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
item_size
¶ A integer indicating the number of items.
A integer indicating the hidden state size in GRU.
-
num_layers
¶ A integer indicating the number of GRU layers.
A float indicating the dropout rate to dropout hidden state.
-
dropout_input
¶ A integer indicating if we dropout the input of model.
-
embedding_dim
¶ A integer indicating the dimension of item embedding.
-
batch_size
¶ A integer indicating the batch size.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
crslab.model.recommendation.popularity package¶
-
class
crslab.model.recommendation.popularity.popularity.
PopularityModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
item_size
¶ A integer indicating the number of items.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
crslab.model.recommendation.sasrec package¶
-
class
crslab.model.recommendation.sasrec.modules.
Embeddings
(item_size, hidden_size, max_seq_length, hidden_dropout_prob)[source]¶ Bases:
torch.nn.modules.module.Module
Construct the embeddings from item, position, attribute.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(input_ids)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.recommendation.sasrec.modules.
Encoder
(num_hidden_layers, hidden_size, num_attention_heads, hidden_dropout_prob, hidden_act, attention_probs_dropout_prob)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(hidden_states, attention_mask, output_all_encoded_layers=True)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.recommendation.sasrec.modules.
Intermediate
(hidden_size, hidden_act, hidden_dropout_prob)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(input_tensor)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.recommendation.sasrec.modules.
Layer
(hidden_size, num_attention_heads, hidden_dropout_prob, hidden_act, attention_probs_dropout_prob)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(hidden_states, attention_mask)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.recommendation.sasrec.modules.
LayerNorm
(hidden_size, eps=1e-12)[source]¶ Bases:
torch.nn.modules.module.Module
Construct a layernorm module in the TF style (epsilon inside the square root).
-
forward
(x)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.recommendation.sasrec.modules.
SASRec
(hidden_dropout_prob, device, initializer_range, hidden_size, max_seq_length, item_size, num_attention_heads, attention_probs_dropout_prob, hidden_act, num_hidden_layers)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(input_ids, attention_mask=None, output_all_encoded_layers=True)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.recommendation.sasrec.modules.
SelfAttention
(hidden_size, num_attention_heads, hidden_dropout_prob, attention_probs_dropout_prob)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(input_tensor, attention_mask)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
crslab.model.recommendation.sasrec.modules.
gelu
(x)[source]¶ Implementation of the gelu activation function.
For information: OpenAI GPT’s gelu is slightly different (and gives slightly different results): 0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3)))) Also see https://arxiv.org/abs/1606.08415
References
Kang, Wang-Cheng, and Julian McAuley. “Self-attentive sequential recommendation.” in ICDM 2018.
-
class
crslab.model.recommendation.sasrec.sasrec.
SASRECModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
A float indicating the dropout rate to dropout hidden state in SASRec.
-
initializer_range
¶ A float indicating the range of parameters initiation in SASRec.
A integer indicating the size of hidden state in SASRec.
-
max_seq_length
¶ A integer indicating the max interaction history length.
-
item_size
¶ A integer indicating the number of items.
-
num_attention_heads
¶ A integer indicating the head number in SASRec.
-
attention_probs_dropout_prob
¶ A float indicating the dropout rate in attention layers.
A string indicating the activation function type in SASRec.
A integer indicating the number of hidden layers in SASRec.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
crslab.model.recommendation.textcnn package¶
References
Kim, Yoon. “Convolutional Neural Networks for Sentence Classification.” in EMNLP 2014.
-
class
crslab.model.recommendation.textcnn.textcnn.
TextCNNModel
(opt, device, vocab, side_data)[source]¶ Bases:
crslab.model.base.BaseModel
-
movie_num
¶ A integer indicating the number of items.
-
num_filters
¶ A string indicating the number of filter in CNN.
-
embed
¶ A integer indicating the size of embedding layer.
-
filter_sizes
¶ A string indicating the size of filter in CNN.
-
dropout
¶ A float indicating the dropout rate.
- Parameters
opt (dict) – A dictionary record the hyper parameters.
device (torch.device) – A variable indicating which device to place the data and model.
vocab (dict) – A dictionary record the vocabulary information.
side_data (dict) – A dictionary record the side data.
-
forward
(batch, mode)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
Module contents¶
crslab.model.utils package¶
Subpackages¶
crslab.model.utils.modules package¶
-
class
crslab.model.utils.modules.attention.
SelfAttentionBatch
(dim, da, alpha=0.2, dropout=0.5)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(h)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.utils.modules.attention.
SelfAttentionSeq
(dim, da, alpha=0.2, dropout=0.5)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
class
crslab.model.utils.modules.transformer.
MultiHeadAttention
(n_heads, dim, dropout=0.0)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(query, key=None, value=None, mask=None)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.utils.modules.transformer.
TransformerDecoder
(n_heads, n_layers, embedding_size, ffn_size, vocabulary_size, embedding=None, dropout=0.0, attention_dropout=0.0, relu_dropout=0.0, embeddings_scale=True, learn_positional_embeddings=False, padding_idx=None, n_positions=1024)[source]¶ Bases:
torch.nn.modules.module.Module
Transformer Decoder layer.
- Parameters
n_heads (int) – the number of multihead attention heads.
n_layers (int) – number of transformer layers.
embedding_size (int) – the embedding sizes. Must be a multiple of n_heads.
ffn_size (int) – the size of the hidden layer in the FFN
embedding – an embedding matrix for the bottom layer of the transformer. If none, one is created for this encoder.
dropout (float) – Dropout used around embeddings and before layer layer normalizations. This is used in Vaswani 2017 and works well on large datasets.
attention_dropout (float) – Dropout performed after the multhead attention softmax. This is not used in Vaswani 2017.
padding_idx (int) – Reserved padding index in the embeddings matrix.
learn_positional_embeddings (bool) – If off, sinusoidal embeddings are used. If on, position embeddings are learned from scratch.
embeddings_scale (bool) – Scale embeddings relative to their dimensionality. Found useful in fairseq.
n_positions (int) – Size of the position embeddings matrix.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(input, encoder_state, incr_state=None)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
class
crslab.model.utils.modules.transformer.
TransformerDecoderLayer
(n_heads, embedding_size, ffn_size, attention_dropout=0.0, relu_dropout=0.0, dropout=0.0)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(x, encoder_output, encoder_mask)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.utils.modules.transformer.
TransformerEncoder
(n_heads, n_layers, embedding_size, ffn_size, vocabulary_size, embedding=None, dropout=0.0, attention_dropout=0.0, relu_dropout=0.0, padding_idx=0, learn_positional_embeddings=False, embeddings_scale=False, reduction=True, n_positions=1024)[source]¶ Bases:
torch.nn.modules.module.Module
Transformer encoder module.
- Parameters
n_heads (int) – the number of multihead attention heads.
n_layers (int) – number of transformer layers.
embedding_size (int) – the embedding sizes. Must be a multiple of n_heads.
ffn_size (int) – the size of the hidden layer in the FFN
embedding – an embedding matrix for the bottom layer of the transformer. If none, one is created for this encoder.
dropout (float) – Dropout used around embeddings and before layer layer normalizations. This is used in Vaswani 2017 and works well on large datasets.
attention_dropout (float) – Dropout performed after the multhead attention softmax. This is not used in Vaswani 2017.
relu_dropout (float) – Dropout used after the ReLU in the FFN. Not used in Vaswani 2017, but used in Tensor2Tensor.
padding_idx (int) – Reserved padding index in the embeddings matrix.
learn_positional_embeddings (bool) – If off, sinusoidal embeddings are used. If on, position embeddings are learned from scratch.
embeddings_scale (bool) – Scale embeddings relative to their dimensionality. Found useful in fairseq.
reduction (bool) – If true, returns the mean vector for the entire encoding sequence.
n_positions (int) – Size of the position embeddings matrix.
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
class
crslab.model.utils.modules.transformer.
TransformerEncoderLayer
(n_heads, embedding_size, ffn_size, attention_dropout=0.0, relu_dropout=0.0, dropout=0.0)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(tensor, mask)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
-
class
crslab.model.utils.modules.transformer.
TransformerFFN
(dim, dim_hidden, relu_dropout=0.0)[source]¶ Bases:
torch.nn.modules.module.Module
Initializes internal Module state, shared by both nn.Module and ScriptModule.
-
forward
(x)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
Submodules¶
Module contents¶
Submodules¶
-
class
crslab.model.base.
BaseModel
(opt, device, dpath=None, resource=None)[source]¶ Bases:
abc.ABC
,torch.nn.modules.module.Module
Base class for all models
-
converse
(batch, mode)[source]¶ calculate loss and prediction of conversation for batch under certain mode
- Parameters
batch (dict or tuple) – batch data
mode (str, optional) – train/valid/test.
-