### TabPreprocessor with Quantization Setup (Dictionary) Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/preprocessing.html Example of initializing TabPreprocessor with continuous columns and a dictionary for specific quantization bin setup, then fitting and transforming a DataFrame. ```python import pandas as pd import numpy as np from pytorch_widedeep.preprocessing import TabPreprocessor cont_df = pd.DataFrame({"col1": np.random.rand(10), "col2": np.random.rand(10) + 1}) cont_cols = ["col1", "col2"] quantization_setup = {'col1': [0., 0.4, 1.], 'col2': [1., 1.4, 2.]} tab_preprocessor2 = TabPreprocessor(continuous_cols=cont_cols, quantization_setup=quantization_setup) ft_cont_df2 = tab_preprocessor2.fit_transform(cont_df) ``` -------------------------------- ### TabPreprocessor with Quantization Setup (Integer) Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/preprocessing.html Example of initializing TabPreprocessor with continuous columns and an integer for quantization setup, then fitting and transforming a DataFrame. ```python import pandas as pd import numpy as np from pytorch_widedeep.preprocessing import TabPreprocessor cont_df = pd.DataFrame({"col1": np.random.rand(10), "col2": np.random.rand(10) + 1}) cont_cols = ["col1", "col2"] tab_preprocessor = TabPreprocessor(continuous_cols=cont_cols, quantization_setup=3) ft_cont_df = tab_preprocessor.fit_transform(cont_df) ``` -------------------------------- ### PyTorch-Widedeep Trainer Setup Example Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/trainer.html This example demonstrates how to initialize and configure the Trainer class with a WideDeep model, specifying optimizers, schedulers, initializers, callbacks, and transforms. ```python import torch from torchvision.transforms import ToTensor from pytorch_widedeep.callbacks import EarlyStopping, LRHistory from pytorch_widedeep.initializers import KaimingNormal, KaimingUniform, Normal, Uniform from pytorch_widedeep.models import TabResnet, Vision, BasicRNN, Wide, WideDeep from pytorch_widedeep import Trainer # wide deep imports embed_input = [(u, i, j) for u, i, j in zip(["a", "b", "c"][:4], [4] * 3, [8] * 3)] column_idx = {k: v for v, k in enumerate(["a", "b", "c"])} wide = Wide(10, 1) # build the model deeptabular = TabResnet(blocks_dims=[8, 4], column_idx=column_idx, cat_embed_input=embed_input) deeptext = BasicRNN(vocab_size=10, embed_dim=4, padding_idx=0) deepimage = Vision() model = WideDeep(wide=wide, deeptabular=deeptabular, deeptext=deeptext, deepimage=deepimage) # set optimizers and schedulers wide_opt = torch.optim.Adam(model.wide.parameters()) deep_opt = torch.optim.AdamW(model.deeptabular.parameters()) text_opt = torch.optim.Adam(model.deeptext.parameters()) img_opt = torch.optim.AdamW(model.deepimage.parameters()) wide_sch = torch.optim.lr_scheduler.StepLR(wide_opt, step_size=5) deep_sch = torch.optim.lr_scheduler.StepLR(deep_opt, step_size=3) text_sch = torch.optim.lr_scheduler.StepLR(text_opt, step_size=5) img_sch = torch.optim.lr_scheduler.StepLR(img_opt, step_size=3) optimizers = {"wide": wide_opt, "deeptabular": deep_opt, "deeptext": text_opt, "deepimage": img_opt} schedulers = {"wide": wide_sch, "deeptabular": deep_sch, "deeptext": text_sch, "deepimage": img_sch} # set initializers and callbacks initializers = {"wide": Uniform, "deeptabular": Normal, "deeptext": KaimingNormal, "deepimage": KaimingUniform} transforms = [ToTensor] callbacks = [LRHistory(n_epochs=4), EarlyStopping] # set the trainer trainer = Trainer(model, objective="regression", initializers=initializers, optimizers=optimizers, lr_schedulers=schedulers, callbacks=callbacks, transforms=transforms) ``` -------------------------------- ### Developer Installation Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/installation.html Clone the repository and install the package in development mode for local contributions and testing. ```bash # Clone the repository git clone https://github.com/jrzaurin/pytorch-widedeep.git cd pytorch-widedeep # Install in dev mode pip install -e . ``` -------------------------------- ### Install and Import Libraries Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/12_ZILNLoss_origkeras_vs_pytorch_widedeep.ipynb Installs the `lifetime_value` library and imports necessary modules for data manipulation, plotting, and TensorFlow. Ensure `lifetime_value` is installed before running. ```python import os import numpy as np import pandas as pd from scipy import stats import matplotlib.pyplot as plt import seaborn as sns import tensorflow as tf import tensorflow_probability as tfp from typing import Sequence # install and import ltv !pip install -q git+https://github.com/google/lifetime_value import lifetime_value as ltv ``` -------------------------------- ### Install pytorch-widedeep from GitHub Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/README.md Install the library directly from its GitHub repository. ```bash pip install git+https://github.com/jrzaurin/pytorch-widedeep.git ``` -------------------------------- ### Fit TabPreprocessor with Integer Quantization Setup Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/preprocessing.html Illustrates fitting TabPreprocessor to continuous columns using an integer to specify the number of bins for quantization. ```python cont_df = pd.DataFrame({"col1": np.random.rand(10), "col2": np.random.rand(10) + 1}) cont_cols = ["col1", "col2"] tab_preprocessor = TabPreprocessor(continuous_cols=cont_cols, quantization_setup=3) ft_cont_df = tab_preprocessor.fit_transform(cont_df) ``` -------------------------------- ### Fit TabPreprocessor with Dictionary Quantization Setup Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/preprocessing.html Demonstrates fitting TabPreprocessor to continuous columns using a dictionary to define custom bin edges for quantization. ```python quantization_setup = {'col1': [0., 0.4, 1.], 'col2': [1., 1.4, 2.]} tab_preprocessor2 = TabPreprocessor(continuous_cols=cont_cols, quantization_setup=quantization_setup) ft_cont_df2 = tab_preprocessor2.fit_transform(cont_df) ``` -------------------------------- ### ModelCheckpoint Callback Example Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/callbacks.html This example demonstrates how to instantiate and use the ModelCheckpoint callback with the Trainer. It shows setting the filepath for saving weights and passing the callback instance to the Trainer. ```python from pytorch_widedeep.callbacks import ModelCheckpoint from pytorch_widedeep.models import TabMlp, Wide, WideDeep from pytorch_widedeep.training import Trainer embed_input = [(u, i, j) for u, i, j in zip(["a", "b", "c"][:4], [4] * 3, [8] * 3)] column_idx = {k: v for v, k in enumerate(["a", "b", "c"])} wide = Wide(10, 1) deep = TabMlp(mlp_hidden_dims=[8, 4], column_idx=column_idx, cat_embed_input=embed_input) model = WideDeep(wide, deep) trainer = Trainer(model, objective="regression", callbacks=[ModelCheckpoint(filepath='checkpoints/weights_out')]) ``` -------------------------------- ### Tab2Vec Initialization and Usage Example Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/tab2vec.html This example demonstrates how to initialize Tab2Vec with a TabPreprocessor and a trained model, and then use it to transform a dataframe. It covers setting up toy data, preprocessing it, defining a TabMlp model, and finally vectorizing a separate dataframe. ```python import string from random import choices import numpy as np import pandas as pd from pytorch_widedeep import Tab2Vec from pytorch_widedeep.models import TabMlp, WideDeep from pytorch_widedeep.preprocessing import TabPreprocessor colnames = list(string.ascii_lowercase)[:4] cat_col1_vals = ["a", "b", "c"] cat_col2_vals = ["d", "e", "f"] # Create the toy input dataframe and a toy dataframe to be vectorised cat_inp = [np.array(choices(c, k=5)) for c in [cat_col1_vals, cat_col2_vals]] cont_inp = [np.round(np.random.rand(5), 2) for _ in range(2)] df_inp = pd.DataFrame(np.vstack(cat_inp + cont_inp).transpose(), columns=colnames) cat_t2v = [np.array(choices(c, k=5)) for c in [cat_col1_vals, cat_col2_vals]] cont_t2v = [np.round(np.random.rand(5), 2) for _ in range(2)] df_t2v = pd.DataFrame(np.vstack(cat_t2v + cont_t2v).transpose(), columns=colnames) # fit the TabPreprocessor embed_cols = [("a", 2), ("b", 4)] cont_cols = ["c", "d"] tab_preprocessor = TabPreprocessor(cat_embed_cols=embed_cols, continuous_cols=cont_cols) X_tab = tab_preprocessor.fit_transform(df_inp) # define the model (and let's assume we train it) tabmlp = TabMlp( column_idx=tab_preprocessor.column_idx, cat_embed_input=tab_preprocessor.cat_embed_input, continuous_cols=tab_preprocessor.continuous_cols, mlp_hidden_dims=[8, 4]) model = WideDeep(deeptabular=tabmlp) # ...train the model... # vectorise the dataframe t2v = Tab2Vec(tab_preprocessor, model, device="cpu") X_vec = t2v.transform(df_t2v) ``` -------------------------------- ### Initialize and Run ContrastiveDenoisingTrainer Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/15_Self_Supervised_Pretraning_pt2.ipynb Initializes the ContrastiveDenoisingTrainer with the pretrained model and preprocessor. Then, it starts the pretraining process for a specified number of epochs and batch size. ```python # for a full list of the params for the the ContrastiveDenoisingTrainer (which are many) please see the docs. # Note that using these params involves some knowledge of the routine and the architecture of the model used contrastive_denoising_trainer = ContrastiveDenoisingTrainer( model=ft_transformer, preprocessor=tab_preprocessor, ) contrastive_denoising_trainer.pretrain(X_tab, n_epochs=5, batch_size=256) ``` -------------------------------- ### Whole model training start Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/06_finetune_and_warmup.ipynb Signals the commencement of training the entire model after individual component fine-tuning or warmup. ```text Fine-tuning (or warmup) of individual components completed. Training the whole model for 2 epochs ``` -------------------------------- ### TabResnet Model Initialization and Usage Example Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/model_components.html Demonstrates how to initialize and use the TabResnet model with categorical and continuous features. Ensure all necessary imports are present. ```python >>> import torch >>> from pytorch_widedeep.models import TabResnet >>> X_deep = torch.cat((torch.empty(5, 4).random_(4), torch.rand(5, 1)), axis=1) >>> colnames = ['a', 'b', 'c', 'd', 'e'] >>> cat_embed_input = [(u,i,j) for u,i,j in zip(colnames[:4], [4]*4, [8]*4)] >>> column_idx = {k:v for v,k in enumerate(colnames)} >>> model = TabResnet(blocks_dims=[16,4], column_idx=column_idx, cat_embed_input=cat_embed_input, ... continuous_cols = ['e']) >>> out = model(X_deep) ``` -------------------------------- ### Initialize TabMlp Model Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/model_components.html Example of initializing a TabMlp model with categorical and continuous column configurations. Ensure column indices and embedding inputs are correctly defined. ```python >>> import torch >>> from pytorch_widedeep.models import TabMlp >>> X_tab = torch.cat((torch.empty(5, 4).random_(4), torch.rand(5, 1)), axis=1) >>> colnames = ["a", "b", "c", "d", "e"] >>> cat_embed_input = [(u, i, j) for u, i, j in zip(colnames[:4], [4] * 4, [8] * 4)] >>> column_idx = {k: v for v, k in enumerate(colnames)} >>> model = TabMlp(mlp_hidden_dims=[8, 4], column_idx=column_idx, cat_embed_input=cat_embed_input, ... continuous_cols=["e"]) ``` -------------------------------- ### ChunkWidePreprocessor Example Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/preprocessing.html Demonstrates how to use ChunkWidePreprocessor to prepare data for the wide component of a Wide & Deep model. It handles label encoding for wide and crossed columns. ```python >>> import pandas as pd >>> from pytorch_widedeep.preprocessing import ChunkWidePreprocessor >>> chunk = pd.DataFrame({'color': ['r', 'b', 'g'], 'size': ['s', 'n', 'l']}) >>> wide_cols = ['color'] >>> crossed_cols = [('color', 'size')] >>> chunk_wide_preprocessor = ChunkWidePreprocessor(wide_cols=wide_cols, crossed_cols=crossed_cols, ... n_chunks=1) >>> X_wide = chunk_wide_preprocessor.fit_transform(chunk) ``` -------------------------------- ### Initialize Trainer for Initial Training Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/sources/examples/06_finetune_and_warmup.ipynb Sets up the Trainer with the model, binary objective, and Adam optimizer. Includes accuracy as a metric. ```python trainer = Trainer( model, objective="binary", optimizers=torch.optim.Adam(model.parameters(), lr=0.01), metrics=[Accuracy], ) ``` -------------------------------- ### Initialize SelfAttentionMLP Model Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/model_components.html Example of initializing the SelfAttentionMLP model with specified column indices, categorical embedding inputs, and continuous columns. This setup is crucial for defining how the model processes different types of input features. ```python >>> import torch >>> from pytorch_widedeep.models import SelfAttentionMLP >>> X_tab = torch.cat((torch.empty(5, 4).random_(4), torch.rand(5, 1)), axis=1) >>> colnames = ['a', 'b', 'c', 'd', 'e'] >>> cat_embed_input = [(u,i,j) for u,i,j in zip(colnames[:4], [4]*4, [8]*4)] >>> column_idx = {k:v for v,k in enumerate(colnames)} >>> model = SelfAttentionMLP(column_idx=column_idx, cat_embed_input=cat_embed_input, continuous_cols = ['e']) >>> out = model(X_tab) ``` -------------------------------- ### Initialize TabPreprocessor with Quantization Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/01_preprocessors_and_utils.ipynb Initialize TabPreprocessor with a quantization_setup to bucketize continuous columns into specified numbers of bins. ```python quantization_setup = { "age": 4, "hours-per-week": 5, } # you can also pass a list of floats with the boundaries if you wanted quant_tab_preprocessor = TabPreprocessor( cat_embed_cols=cat_embed_cols, continuous_cols=continuous_cols, quantization_setup=quantization_setup, ) qX_tab = quant_tab_preprocessor.fit_transform(df) ``` -------------------------------- ### Developer Install of pytorch-widedeep Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/README.md Install the library in development mode after cloning the repository. ```bash # Clone the repository git clone https://github.com/jrzaurin/pytorch-widedeep cd pytorch-widedeep # Install in dev mode pip install -e . ``` -------------------------------- ### Deep tabular training start Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/06_finetune_and_warmup.ipynb Indicates the start of training for a deeptabular model. ```text Training deeptabular for 2 epochs ``` -------------------------------- ### Install pytorch-widedeep using pip Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/README.md Install the library using pip for general use. ```bash pip install pytorch-widedeep ``` -------------------------------- ### Initialize Tabular, Text, and Image Preprocessors and Models Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/README.md Sets up preprocessors for tabular, text, and image data, and initializes corresponding model components like TabMlp, BasicRNN, and Vision. Ensure dataframes and image paths are correctly configured. ```python import torch from pytorch_widedeep.preprocessing import TabPreprocessor, TextPreprocessor, ImagePreprocessor from pytorch_widedeep.models import TabMlp, BasicRNN, WideDeep, ModelFuser, Vision from pytorch_widedeep.models._base_wd_model_component import BaseWDModelComponent from pytorch_widedeep import Trainer # Tabular tab_preprocessor = TabPreprocessor( embed_cols=["city", "name"], continuous_cols=["age", "height"] ) X_tab = tab_preprocessor.fit_transform(df) tab_mlp = TabMlp( column_idx=tab_preprocessor.column_idx, cat_embed_input=tab_preprocessor.cat_embed_input, continuous_cols=tab_preprocessor.continuous_cols, mlp_hidden_dims=[16, 8], ) # Text text_preprocessor_1 = TextPreprocessor( text_col="sentence", maxlen=20, max_vocab=100, n_cpus=1 ) X_text_1 = text_preprocessor_1.fit_transform(df) text_preprocessor_2 = TextPreprocessor( text_col="other_sentence", maxlen=20, max_vocab=100, n_cpus=1 ) X_text_2 = text_preprocessor_2.fit_transform(df) rnn_1 = BasicRNN( vocab_size=len(text_preprocessor_1.vocab.itos), embed_dim=16, hidden_dim=8, n_layers=1, ) rnn_2 = BasicRNN( vocab_size=len(text_preprocessor_2.vocab.itos), embed_dim=16, hidden_dim=8, n_layers=1, ) models_fuser = ModelFuser( models=[rnn_1, rnn_2], fusion_method="mult", ) # Image image_preprocessor = ImagePreprocessor(img_col="image_name", img_path="images") X_img = image_preprocessor.fit_transform(df) vision = Vision(pretrained_model_setup="resnet18", head_hidden_dims=[16, 8]) ``` -------------------------------- ### Initialize Trainer for Warm-up Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/sources/examples/06_finetune_and_warmup.ipynb Sets up the Trainer for the warm-up phase. The model is passed, and the objective and metrics are configured. ```python trainer_2 = Trainer(model, objective="binary", metrics=[Accuracy]) ``` -------------------------------- ### TabPerceiver Model Initialization Example Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/model_components.html Demonstrates how to initialize and use the TabPerceiver model with specified column indices, categorical embedding inputs, continuous columns, and architectural parameters like the number of latents and latent dimensions. ```python >>> import torch >>> from pytorch_widedeep.models import TabPerceiver >>> X_tab = torch.cat((torch.empty(5, 4).random_(4), torch.rand(5, 1)), axis=1) >>> colnames = ['a', 'b', 'c', 'd', 'e'] >>> cat_embed_input = [(u,i) for u,i in zip(colnames[:4], [4]*4)] >>> continuous_cols = ['e'] >>> column_idx = {k:v for v,k in enumerate(colnames)} >>> model = TabPerceiver(column_idx=column_idx, cat_embed_input=cat_embed_input, ... continuous_cols=continuous_cols, n_latents=2, latent_dim=16, ... n_perceiver_blocks=2) >>> out = model(X_tab) ``` -------------------------------- ### Deep Tabular Layer 3 Training Start Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/06_finetune_and_warmup.ipynb Indicates the start of training for the third layer of the deep tabular component. ```text Training deeptabular, layer 3 of 3 ``` -------------------------------- ### Instantiate Text and Image Data Loaders from Folder Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/19_load_from_folder_functionality.ipynb Instantiate `TextFromFolder` and `ImageFromFolder` for text and image datasets respectively. These loaders require a preprocessor to be passed during instantiation. ```python # for the text and image datasets we do not need to specify eval or test loaders text_folder = TextFromFolder(preprocessor=text_preprocessor) img_folder = ImageFromFolder(preprocessor=img_preprocessor) ``` -------------------------------- ### Deep Tabular Layer 2 Training Start Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/06_finetune_and_warmup.ipynb Indicates the start of training for the second layer of the deep tabular component. ```text Training deeptabular, layer 2 of 3 ``` -------------------------------- ### Setting up model components Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/model_components.html Configures and returns a model component, potentially wrapping it in nn.Sequential with a linear layer or TabNetPredLayer if needed. Handles lists of components by creating an nn.ModuleList. ```python def _set_model_component( self, component: Union[BaseWDModelComponent, List[BaseWDModelComponent]], is_deeptabular: bool = False, ) -> Union[nn.ModuleList, WDModel]: if isinstance(component, list): component_: Optional[Union[nn.ModuleList, WDModel]] = nn.ModuleList() for cp in component: if self.with_deephead or cp.output_dim == 1: component_.append(cp) else: component_.append( nn.Sequential(cp, nn.Linear(cp.output_dim, self.pred_dim)) ) elif self.with_deephead or component.output_dim == 1: component_ = component elif is_deeptabular and self.is_tabnet: component_ = nn.Sequential( component, TabNetPredLayer(component.output_dim, self.pred_dim) ) else: component_ = nn.Sequential(component, nn.Linear(component.output_dim, self.pred_dim)) return component_ ``` -------------------------------- ### Deep Tabular Layer Training Start Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/06_finetune_and_warmup.ipynb Indicates the start of training for a specific layer within the deep tabular component. ```text Training deeptabular, layer 1 of 3 ``` -------------------------------- ### Initialize Standard Preprocessors Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/19_load_from_folder_functionality.ipynb Initialize Tabular, Text, and Image preprocessors for standard data loading. Use this when your dataset fits into memory. ```python tab_preprocessor = TabPreprocessor( embed_cols=cat_embed_cols, continuous_cols=cont_cols, default_embed_dim=8, verbose=0, ) text_preprocessor = TextPreprocessor( text_col=text_col, n_cpus=1, ) img_preprocessor = ImagePreprocessor( img_col=img_col, img_path=img_path, ) ``` -------------------------------- ### ImagePreprocessor fit_transform and transform Example Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/preprocessing.html Demonstrates how to initialize and use the ImagePreprocessor. The `fit_transform` method resizes images and computes normalization metrics, while `transform` applies the resizing without recomputing metrics. Normalization metrics are only computed when `fit_transform` is called. ```python >>> import pandas as pd >>> >>> from pytorch_widedeep.preprocessing import ImagePreprocessor >>> >>> path_to_image1 = 'tests/test_data_utils/images/galaxy1.png' >>> path_to_image2 = 'tests/test_data_utils/images/galaxy2.png' >>> >>> df_train = pd.DataFrame({'images_column': [path_to_image1]}) >>> df_test = pd.DataFrame({'images_column': [path_to_image2]}) >>> img_preprocessor = ImagePreprocessor(img_col='images_column', img_path='.', verbose=0) >>> resized_images = img_preprocessor.fit_transform(df_train) >>> new_resized_images = img_preprocessor.transform(df_train) ``` -------------------------------- ### RMSELoss Forward Pass Example Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/losses.html Shows an example of using the RMSELoss function with provided input and target tensors. This loss function calculates the root mean square error. ```python import torch from pytorch_widedeep.losses import RMSELoss target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1) input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1) loss = RMSELoss()(input, target) ``` -------------------------------- ### Initialize Torchmetrics Metrics Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/10_3rd_party_integration-RayTune_WnB.ipynb Sets up various metrics from the torchmetrics library for evaluation. Ensure these metrics are compatible with your task (e.g., binary classification). ```python # Metrics from torchmetrics accuracy = Accuracy_torchmetrics(average=None, num_classes=1, task="binary") precision = Precision_torchmetrics(average="micro", num_classes=1, task="binary") f1 = F1_torchmetrics(average=None, num_classes=1, task="binary") recall = Recall_torchmetrics(average=None, num_classes=1, task="binary") ``` -------------------------------- ### Tab2Vec Example: Vectorizing a DataFrame Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/tab2vec.html This example demonstrates how to use the Tab2Vec class to transform a pandas DataFrame into a vectorized representation. It involves setting up a TabPreprocessor, defining and training a TabMlp model, and then initializing Tab2Vec to transform a new DataFrame. ```python >>> import string >>> from random import choices >>> import numpy as np >>> import pandas as pd >>> from pytorch_widedeep import Tab2Vec >>> from pytorch_widedeep.models import TabMlp, WideDeep >>> from pytorch_widedeep.preprocessing import TabPreprocessor >>> >>> colnames = list(string.ascii_lowercase)[:4] >>> cat_col1_vals = ["a", "b", "c"] >>> cat_col2_vals = ["d", "e", "f"] >>> >>> # Create the toy input dataframe and a toy dataframe to be vectorised >>> cat_inp = [np.array(choices(c, k=5)) for c in [cat_col1_vals, cat_col2_vals]] >>> cont_inp = [np.round(np.random.rand(5), 2) for _ in range(2)] >>> df_inp = pd.DataFrame(np.vstack(cat_inp + cont_inp).transpose(), columns=colnames) >>> cat_t2v = [np.array(choices(c, k=5)) for c in [cat_col1_vals, cat_col2_vals]] >>> cont_t2v = [np.round(np.random.rand(5), 2) for _ in range(2)] >>> df_t2v = pd.DataFrame(np.vstack(cat_t2v + cont_t2v).transpose(), columns=colnames) >>> >>> # fit the TabPreprocessor >>> embed_cols = [("a", 2), ("b", 4)] >>> cont_cols = ["c", "d"] >>> tab_preprocessor = TabPreprocessor(cat_embed_cols=embed_cols, continuous_cols=cont_cols) >>> X_tab = tab_preprocessor.fit_transform(df_inp) >>> >>> # define the model (and let's assume we train it) >>> tabmlp = TabMlp( ... column_idx=tab_preprocessor.column_idx, ... cat_embed_input=tab_preprocessor.cat_embed_input, ... continuous_cols=tab_preprocessor.continuous_cols, ... mlp_hidden_dims=[8, 4]) >>> model = WideDeep(deeptabular=tabmlp) >>> # ...train the model... >>> >>> # vectorise the dataframe >>> t2v = Tab2Vec(tab_preprocessor, model, device="cpu") >>> X_vec = t2v.transform(df_t2v) ``` -------------------------------- ### Initialize and Fit TrainerFromFolder Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/sources/examples/19_load_from_folder_functionality.ipynb Initialize the TrainerFromFolder with a model and objective, then fit it using train and evaluation data loaders. ```python trainer = TrainerFromFolder( model, objective="regression", ) trainer.fit( train_loader=train_loader, eval_loader=eval_loader, ) ``` -------------------------------- ### Image Resizing Output Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/01_preprocessors_and_utils.ipynb Indicates that the image resizing process has started and is in progress. ```text Reading Images from ../tmp_data/airbnb/property_picture/ Resizing ``` -------------------------------- ### Initialize and Use TextPreprocessor Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/preprocessing.html Demonstrates initializing TextPreprocessor with specific parameters and then fitting and transforming training data, followed by transforming test data. This is useful for preparing text columns in a DataFrame for model input. ```python import pandas as pd from pytorch_widedeep.preprocessing import TextPreprocessor df_train = pd.DataFrame({'text_column': ["life is like a box of chocolates", ... "You never know what you're gonna get"]}) text_preprocessor = TextPreprocessor(text_col='text_column', max_vocab=25, min_freq=1, maxlen=10) text_preprocessor.fit_transform(df_train) The vocabulary contains 24 tokens array([[ 1, 1, 1, 1, 10, 11, 12, 13, 14, 15], [ 5, 9, 16, 17, 18, 9, 19, 20, 21, 22]], dtype=int32) df_te = pd.DataFrame({'text_column': ['you never know what is in the box']}) text_preprocessor.transform(df_te) array([[ 1, 1, 9, 16, 17, 18, 11, 0, 0, 13]], dtype=int32) ``` -------------------------------- ### Drop Unnecessary Columns Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/11_auc_multiclass.ipynb Removes the 'SequenceName' column as it is not needed for the model training in this example. ```python # drop columns we won't need in this example df_enc = df_enc.drop(columns=["SequenceName"]) ``` -------------------------------- ### Get Shape of Processed Image Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/01_preprocessors_and_utils.ipynb Retrieves the shape of the first processed image in the X_images array. ```python X_images[0].shape ``` -------------------------------- ### Configure and Train the Model Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/11_auc_multiclass.ipynb Sets up the optimizer, learning rate scheduler, and trainer with the specified model, objective function, and metrics. It then trains the model for a set number of epochs. ```python # Optimizers deep_opt = SGD(model.deeptabular.parameters(), lr=0.1) # LR Scheduler deep_sch = lr_scheduler.StepLR(deep_opt, step_size=3) # Hyperparameters trainer = Trainer( model, objective="multiclass_focal_loss", lr_schedulers={"deeptabular": deep_sch}, initializers={"deeptabular": XavierNormal}, optimizers={"deeptabular": deep_opt}, metrics=[auroc], ) trainer.fit(X_train=X_train, X_val=X_val, n_epochs=5, batch_size=50) ``` -------------------------------- ### Drop Unnecessary Columns Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/13_model_uncertainty_prediction.ipynb Removes the 'EXAMPLE_ID' and 'BLOCK_ID' columns from the DataFrame as they are not needed for model training in this example. ```python # drop columns we won't need in this example df.drop(columns=["EXAMPLE_ID", "BLOCK_ID"], inplace=True) ``` -------------------------------- ### Setup Trainer with Callbacks Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/05_save_and_load_model_and_artifacts.ipynb Configures the Trainer with an objective, metrics, and callbacks for early stopping and model checkpointing. The model checkpoint saves the best performing model during training. ```python early_stopping = EarlyStopping() model_checkpoint = ModelCheckpoint( filepath="tmp_dir/adult_tabmlp_model", save_best_only=True, verbose=1, max_save=1, ) trainer = Trainer( model, objective="binary", callbacks=[early_stopping, model_checkpoint], metrics=[Accuracy], ) ``` -------------------------------- ### Initialize Trainer for Fine-tuning Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/sources/examples/06_finetune_and_warmup.ipynb Sets up a new Trainer instance for the fine-tuning process. Only the model is passed, as objective and metrics are often the same. ```python trainer_1 = Trainer(model_1, objective="binary", metrics=[Accuracy]) ``` -------------------------------- ### Training Wide Component Output Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/06_finetune_and_warmup.ipynb Displays the output message indicating the start of training for the wide component of the model. ```text Training wide for 2 epochs ``` -------------------------------- ### Display Model Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/03_binary_classification_with_defaults.ipynb Prints the model object to the console, showing its structure and configuration. Useful for verifying the model setup. ```python tab_model ``` -------------------------------- ### Configure Model Components and Callbacks Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/04_regression_with_images_and_text.ipynb Set up optimizers, learning rate schedulers, initializers, image transformations, and callbacks for the Wide & Deep model. Note that initializers can be configured with regular expressions to exclude specific parameters. ```python optimizers = { "wide": wide_opt, "deeptabular": deep_opt, "deeptext": text_opt, "deepimage": img_opt, "deephead": head_opt, } schedulers = { "wide": wide_sch, "deeptabular": deep_sch, "deeptext": text_sch, "deepimage": img_sch, "deephead": head_sch, } # Now...we have used pretrained word embeddings, so you do not want to # initialise these embeddings. However you might still want to initialise the # other layers in the DeepText component. No probs, you can do that with the # parameter pattern and your knowledge on regular expressions. Here we are # telling to the KaimingNormal initializer to NOT touch the parameters whose # name contains the string word_embed. initializers = { "wide": KaimingNormal, "deeptabular": KaimingNormal, "deeptext": KaimingNormal(pattern=r"^(?!.*word_embed).*$"), "deepimage": KaimingNormal, } mean = [0.406, 0.456, 0.485] # BGR std = [0.225, 0.224, 0.229] # BGR transforms = [ToTensor, Normalize(mean=mean, std=std)] callbacks = [ LRHistory(n_epochs=10), EarlyStopping, ModelCheckpoint(filepath="model_weights/wd_out"), ] ``` -------------------------------- ### Import necessary libraries Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/05_save_and_load_model_and_artifacts.ipynb Imports all required libraries for data manipulation, model building, and training. Ensure these are installed in your environment. ```python import pickle import numpy as np import pandas as pd import torch import shutil from pytorch_widedeep.preprocessing import TabPreprocessor from pytorch_widedeep.training import Trainer from pytorch_widedeep.callbacks import EarlyStopping, ModelCheckpoint, LRHistory from pytorch_widedeep.models import TabMlp, WideDeep from pytorch_widedeep.metrics import Accuracy from pytorch_widedeep.datasets import load_adult from sklearn.model_selection import train_test_split ``` -------------------------------- ### Initialize and fit DINPreprocessor Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/preprocessing.html Demonstrates initializing the DINPreprocessor with various column configurations and then fitting it to a pandas DataFrame. This is used to prepare data for Deep Interest Network models. ```python import pandas as pd from pytorch_widedeep.preprocessing import DINPreprocessor data = { 'user_id': [1, 1, 1, 2, 2, 2, 3, 3, 3], 'item_id': [101, 102, 103, 101, 103, 104, 102, 103, 104], 'timestamp': [1, 2, 3, 1, 2, 3, 1, 2, 3], 'category': ['A', 'B', 'A', 'B', 'A', 'C', 'B', 'A', 'C'], 'price': [10.5, 15.0, 12.0, 10.5, 12.0, 20.0, 15.0, 12.0, 20.0], 'rating': [0, 1, 0, 1, 0, 1, 0, 1, 0] } df = pd.DataFrame(data) din_preprocessor = DINPreprocessor( user_id_col='user_id', item_embed_col='item_id', target_col='rating', max_seq_length=2, action_col='rating', other_seq_embed_cols=['category'], cat_embed_cols=['user_id'], continuous_cols=['price'], cols_to_scale=['price'] ) X, y = din_preprocessor.fit_transform(df) ``` -------------------------------- ### Combining Wide and Tabular Components Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/03_binary_classification_with_defaults.ipynb Instantiates the Wide and TabMlp components and combines them into a WideDeep model. This setup is for binary classification. ```python wide = Wide(input_dim=np.unique(X_wide).shape[0], pred_dim=1) tab_mlp = TabMlp( column_idx=tab_preprocessor.column_idx, cat_embed_input=tab_preprocessor.cat_embed_input, cat_embed_dropout=0.1, continuous_cols=continuous_cols, mlp_hidden_dims=[400, 200], mlp_dropout=0.5, mlp_activation="leaky_relu", ) wd_model = WideDeep(wide=wide, deeptabular=tab_mlp) ``` -------------------------------- ### Instantiate and Use FocalR_MSELoss Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/losses.html Example of how to instantiate and use the FocalR_MSELoss. This loss function is suitable for regression tasks with imbalanced data. ```python import torch from pytorch_widedeep.losses import FocalR_MSELoss target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1) input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1) loss = FocalR_MSELoss()(input, target) ``` -------------------------------- ### SAINT Model Initialization Example Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/model_components.html Demonstrates how to initialize and use the SAINT model with specified column indices, categorical embedding inputs, and continuous columns. Ensure torch is imported. ```python import torch from pytorch_widedeep.models import SAINT X_tab = torch.cat((torch.empty(5, 4).random_(4), torch.rand(5, 1)), axis=1) colnames = ['a', 'b', 'c', 'd', 'e'] cat_embed_input = [(u,i) for u,i in zip(colnames[:4], [4]*4)] continuous_cols = ['e'] column_idx = {k:v for v,k in enumerate(colnames)} model = SAINT(column_idx=column_idx, cat_embed_input=cat_embed_input, continuous_cols=continuous_cols) out = model(X_tab) ``` -------------------------------- ### Get Output Dimension of ContextAttentionMLP Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/model_components.html Retrieves the output dimension of the ContextAttentionMLP model. This is a required property for building the WideDeep class. ```python @property def output_dim(self) -> int: r"""The output dimension of the model. This is a required property neccesary to build the `WideDeep` class """ return ( self.input_dim if self.with_cls_token else ((self.n_cat + self.n_cont) * self.input_dim) ) ``` -------------------------------- ### Building a WideDeep Model with Various Components Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/model_components.html Example demonstrating how to instantiate a WideDeep model by combining different pre-defined components for wide, deep tabular, deep text, and deep image data. Ensure all necessary components are imported before use. ```python >>> from pytorch_widedeep.models import TabResnet, Vision, BasicRNN, Wide, WideDeep >>> embed_input = [(u, i, j) for u, i, j in zip(["a", "b", "c"][:4], [4] * 3, [8] * 3)] >>> column_idx = {k: v for v, k in enumerate(["a", "b", "c"])} >>> wide = Wide(10, 1) >>> deeptabular = TabResnet(blocks_dims=[8, 4], column_idx=column_idx, cat_embed_input=embed_input) >>> deeptext = BasicRNN(vocab_size=10, embed_dim=4, padding_idx=0) >>> deepimage = Vision() >>> model = WideDeep(wide=wide, deeptabular=deeptabular, deeptext=deeptext, deepimage=deepimage) ``` -------------------------------- ### Initialize and Use StackedAttentiveRNN Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/model_components.html Demonstrates how to initialize the StackedAttentiveRNN model with specified parameters and pass text data through it. Ensure torch and StackedAttentiveRNN are imported. ```python import torch from pytorch_widedeep.models import StackedAttentiveRNN X_text = torch.cat((torch.zeros([5,1]), torch.empty(5, 4).random_(1,4)), axis=1) model = StackedAttentiveRNN(vocab_size=4, hidden_dim=4, padding_idx=0, embed_dim=4) out = model(X_text) ``` -------------------------------- ### Tokenizer Representation Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/utils/fastai_transforms.html Get a string representation of the Tokenizer, listing the tokenizer function, language, and all applied pre and post rules. ```python res = f"Tokenizer {self.tok_func.__name__} in {self.lang} with the following rules:\n" for rule in self.pre_rules: res += f" - {rule.__name__}\n" for rule in self.post_rules: res += f" - {rule.__name__}\n" return res ``` -------------------------------- ### Get Embedding Shape Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/09_extracting_embeddings.ipynb Check the shape of the generated embeddings. The output dimension is calculated as input_dim multiplied by the number of columns. ```python # X vec is the dataframe turned into the embeddings X_vec.shape ``` -------------------------------- ### Initialize and Use SelfAttentionMLP Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/model_components.html Demonstrates how to initialize and use the SelfAttentionMLP model with sample data. Requires torch and SelfAttentionMLP imports. ```python import torch from pytorch_widedeep.models import SelfAttentionMLP X_tab = torch.cat((torch.empty(5, 4).random_(4), torch.rand(5, 1)), axis=1) colnames = ['a', 'b', 'c', 'd', 'e'] cat_embed_input = [(u,i,j) for u,i,j in zip(colnames[:4], [4]*4, [8]*4)] column_idx = {k:v for v,k in enumerate(colnames)} model = SelfAttentionMLP(column_idx=column_idx, cat_embed_input=cat_embed_input, continuous_cols = ['e']) out = model(X_tab) ``` -------------------------------- ### Initialize Seaborn for Plotting Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/examples/notebooks/00_airbnb_data_preprocessing.ipynb Initializes the Seaborn plotting library with a specified color code. This is a common setup step before creating visualizations. ```python import matplotlib.pyplot as plt import seaborn as sns sns.set(color_codes=True) ``` -------------------------------- ### Initialize and Run TabNet Model Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/model_components.html Demonstrates how to initialize a TabNet model with specified column indices and embedding inputs, and then run a forward pass with sample data. ```python X_tab = torch.cat((torch.empty(5, 4).random_(4), torch.rand(5, 1)), axis=1) colnames = ["a", "b", "c", "d", "e"] cat_embed_input = [(u, i, j) for u, i, j in zip(colnames[:4], [4] * 4, [8] * 4)] column_idx = {k: v for v, k in enumerate(colnames)} model = TabNet(column_idx=column_idx, cat_embed_input=cat_embed_input, continuous_cols=["e"]) out = model(X_tab) ``` -------------------------------- ### TabPreprocessor with Categorical and Continuous Features Source: https://github.com/jrzaurin/pytorch-widedeep/blob/main/mkdocs/site/pytorch-widedeep/preprocessing.html Example of initializing TabPreprocessor for categorical embedding columns and continuous columns, then fitting and transforming a DataFrame. ```python import pandas as pd import numpy as np from pytorch_widedeep.preprocessing import TabPreprocessor df = pd.DataFrame({'color': ['r', 'b', 'g'], 'size': ['s', 'n', 'l'], 'age': [25, 40, 55]}) cat_embed_cols = [('color',5), ('size',5)] cont_cols = ['age'] deep_preprocessor = TabPreprocessor(cat_embed_cols=cat_embed_cols, continuous_cols=cont_cols) X_tab = deep_preprocessor.fit_transform(df) print(deep_preprocessor.cat_embed_cols) print(deep_preprocessor.column_idx) ```