from torch.utils.tensorboard import SummaryWriter # Define a CNN model model = ... # Initialize SummaryWriter writer = SummaryWriter() # Get embeddings from conv1 layer of the model conv1_weights = model.conv1.weight.data embeddings = conv1_weights.reshape(conv1_weights.shape[0], -1) # Log embeddings in TensorBoard writer.add_embedding( embeddings, metadata=list(range(embeddings.shape[0])), tag='conv1_weights' )
from torch.utils.tensorboard import SummaryWriter from sklearn.feature_extraction.text import TfidfVectorizer # Load text data text_data = ... # Convert text data to a TF-IDF matrix vectorizer = TfidfVectorizer() tfidf_matrix = vectorizer.fit_transform(text_data) # Initialize SummaryWriter writer = SummaryWriter() # Log embeddings in TensorBoard writer.add_embedding( tfidf_matrix.toarray(), metadata=text_data, tag='tfidf_embeddings' )In this example, we are converting text data to a TF-IDF matrix and then using add_embedding to log the embeddings in TensorBoard. We are also adding metadata to the embeddings so that we can see the corresponding text data in TensorBoard. Overall, torch.utils.tensorboard is a powerful package library in PyTorch for visualizing and logging the performance of your model. add_embedding is a useful method of the SummaryWriter class that allows you to visualize embeddings in TensorBoard.