thogaertner
thogaertner

Reputation: 71

How can I add multiple Metadata in Torch Tensorboard Embedding?

I am using torch==1.9.0 and tensorboard==2.5.0. I would like to track data with tensorbaord as an embedding, so I am something like this:

data = np.random.poisson(lam=10.0, size=(4,4))
labels = ["A","A","B","B"]
ids = [1,2,3,4]
writer = SummaryWriter("/runs/")
writer.add_embedding(data,
                    metadata=labels)
writer.close()

But I only can add lables or ids as metadeta and not a combined dictionaty {'ids':ids, 'lables':labels}. Any idea, how to solve this? Thanks!

FYI: Tensorboard docs just descirbes metadata as a list: https://pytorch.org/docs/stable/tensorboard.html

Upvotes: 3

Views: 1632

Answers (3)

thogaertner
thogaertner

Reputation: 71

Found the answer. You could get multiple fields by adding a metadata header and give metadata as list of lists:

writer.add_embedding(data.values,
                    metadata=metadata,
                    metadata_header=["Name","Labels"])

Reference: https://github.com/tensorflow/tensorboard/issues/61

The format should be so that metadata[0] contains one of each metadata field. You can do that with metadata=list(zip([names, labels])) if you already have two lists names and labels.

Upvotes: 4

J. M. Arnold
J. M. Arnold

Reputation: 6799

You could simply iterate over the appropriate Metadata (dictionary) to add all the values:

writer = SummaryWriter("/runs/")

for i in range(len(data)):
     writer.add_embedding(data[i], metadata=labels[i], global_step=ids[i])

writer.close()

whereas data, labels and ids are the values specified by you above.

As described in the appropriate documentation - .add_embedding(...) would also take label_img for each datapoint, but you didn't specify it: So, just adding the data matrix and the appropriate labels can be achieved via iterating and accessing the appropriate labels.

Upvotes: 1

Fenrir
Fenrir

Reputation: 231

A working example was found here: https://github.com/lanpa/tensorboardX/pull/110 by user h0rm

dataset = datasets.MNIST('mnist', train=True, download=True)
images_train = dataset.train_data[:100].float()
labels_train = dataset.train_labels[:100]
features_train = images_train.view(100, 784)

all_features = torch.cat((features, features_train))
all_labels = torch.cat((label, labels_train))
all_images = torch.cat((images, images_train))
dataset_label = ['test']*100 + ['train']*100
all_labels = list(zip(all_labels, dataset_label))

writer.add_embedding(all_features, metadata=all_labels, 
label_img=all_images.unsqueeze(1),
                 metadata_header=['digit', 'database'])

Upvotes: 0

Related Questions