This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gc | |
def objective(trial): | |
### Dataset section | |
# see https://gist.github.com/krsnewwave/273b9cafa4813771791f076cee32c2e4#file-nvtabular_movielens_main_loop_functions-py-L2 | |
train_loader, valid_loader = create_loaders(train_dataset, valid_dataset) | |
### Model section | |
# see https://gist.github.com/krsnewwave/273b9cafa4813771791f076cee32c2e4#file-nvtabular_movielens_main_loop_functions-py-L29 | |
epochs = 1 | |
patience = 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# (1) Create loaders | |
def create_loaders(train_dataset, valid_dataset): | |
# dataset and loaders | |
train_iter = TorchAsyncItr( | |
train_dataset, | |
batch_size=BATCH_SIZE, | |
cats=CATEGORICAL_COLUMNS + CATEGORICAL_MH_COLUMNS, | |
conts=NUMERIC_COLUMNS, | |
labels=["rating"], | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WideAndDeepMultihot(pl.LightningModule): | |
# others go here... | |
# | |
# | |
def training_step(self, batch, batch_idx): | |
# unpack | |
x_cat, x_cont, y = self.transform.transform_with_label(batch) | |
# forward | |
y_pred = self((x_cat, x_cont)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytorch_lightning as pl | |
from nvtabular.framework_utils.torch.layers import ConcatenatedEmbeddings, MultiHotEmbeddings | |
import torch | |
class WideAndDeepMultihot(pl.LightningModule): | |
def __init__( | |
self, | |
model_conf, | |
cat_names, cont_names, label_names, | |
num_continuous, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MVAERecommender(TopNRecommender): | |
# TopNRecommender contains methods to predict top k | |
def __init__(self, model_conf : Dict, novelty_per_item, num_users, num_items, remove_observed = False, ): | |
# ... configuration is skipped | |
# # # # Model Structure # # # # | |
# this is to handle encoding dimensions as lists | |
self.encoder = nn.ModuleList() | |
# this enumeration produces dims in pairs, start with 1 | |
for i, (d_in, d_out) in enumerate(zip(self.enc_dims[:-1], self.enc_dims[1:]), start=1): | |
# double d out at last for the mean and variance parameters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install ray tune, and comet ml | |
from ray.tune.integration.comet import CometLoggerCallback | |
from functools import partial | |
from ray.tune.integration.pytorch_lightning import TuneReportCallback | |
def train_function(model_conf, novelty_per_item, epochs, patience, | |
train_loader, val_loader, checkpoint_dir=None): | |
model = CDAE(model_conf, novelty_per_item, num_users, num_items) | |
# fill up your metrics here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CDAE(pl.LightningModule): | |
def __init__(self, model_conf : Dict, novelty_per_item, num_users, num_items, remove_observed = False, ): | |
super().__init__() | |
self.hidden_dim = model_conf["hidden_dim"] | |
# ... other self. initializations | |
self.user_embedding = nn.Embedding(self.num_users, self.hidden_dim) | |
self.encoder = nn.Linear(self.num_items, self.hidden_dim) | |
self.decoder = nn.Linear(self.hidden_dim, self.num_items) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RecoSparseTrainDataset(Dataset): | |
def __init__(self, sparse_mat): | |
self.sparse_mat = sparse_mat | |
def __len__(self): | |
return self.sparse_mat.shape[0] | |
def __getitem__(self, idx): | |
batch_matrix = self.sparse_mat[idx].toarray().squeeze() | |
return batch_matrix, idx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LightningResNet(pl.LightningModule): | |
def __init__(self, net_pretrained, device='cpu', criterion = F.cross_entropy, | |
num_classes = 4, optimizer = None, scheduler = None): | |
super().__init__() | |
self.net = net_pretrained | |
# set top to number of classes | |
num_ftrs = self.net.fc.in_features | |
self.net.fc = nn.Linear(num_ftrs, num_classes) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# following https://towardsdatascience.com/multilabel-classification-with-pytorch-in-5-minutes-a4fa8993cbc7 | |
class LightningResNetMultiLabel(pl.LightningModule): | |
def __init__(self, net, n_period, n_artists, criterion = F.cross_entropy, optimizer = None, scheduler = None, dropout_p = 0., lr=0.001, freeze_net=False): | |
super().__init__() | |
self.net = net | |
self.feature_extractor = nn.Sequential(*(list(self.net.children())[:-1])) | |
if freeze_net: | |
for param in self.net.parameters(): |
NewerOlder