Search code examples
pytorchtensorboard

PyTorch TensorBoard add_graph() dictionary input error


What would be the proper way of passing the PyTorch dictionary dataset to the TensorBoard add_graph(model, data).

May seems similar to the Question1, Qeustion2 and Question3, however, couldn't find the right way of handling with dictionary dataset.

Error Message

Dictionary inputs to traced functions must have consistent type. Found Tensor and List[str]
Error occurs, No graph saved

Below are my anonymized scripts of the project.

train.py

from torch.utils.tensorboard import SummaryWriter
from models import CustomModel
from datasets import CustomDataset
writer = SummaryWriter()

# Dataset
dataset = CustomDataset(params ...)
train_dataset = [dataset[i] for i in range(0, k)]
train_dataloader = DataLoader(train_dataset, batch_size=32, shuffle=True)

# Model & TensorBoard
model = CustomModel(params....)
writer.add_graph(model, next(iter(train_dataloader)))  # ---- HERE ----

datasets.py

class CustomDataset(Dataset):
    def __init__(self, ...):
        ...
        self.x_sequences = pad_sequence(x_sequences, batch_first=True, padding_value=0)
        self.y_label = torch.LongTensor(label_list)
        ...

    def __len__(self):
        return len(self.y_label)
        
    def __getitem__(self, index):
        ...
        return {
            "x_categoricals": self.x_categoricals[index],
            "x_sequences": self.x_sequences[index],
            "y_label": self.y_label[index],
            "info": self.info[index],
        }

Solution

  • The error message tells you that the entries of the dictionary must all be the same type, but in your case you seem to have a Tensor in one entry but a list of strings in another entry. You'd have to make sure that all entries have the same type.