Skip to content

Instantly share code, notes, and snippets.

@Infinitusvoid
Created August 24, 2023 20:55
Show Gist options
  • Save Infinitusvoid/0b4e2ca92b625d3659cf08c0e35fd716 to your computer and use it in GitHub Desktop.
Save Infinitusvoid/0b4e2ca92b625d3659cf08c0e35fd716 to your computer and use it in GitHub Desktop.
PyTorch : How to convert tensor to numpy array, and back ?
import numpy as np
import torch
# Tensor to NumPy array
tensor = torch.ones(7)
numpy_tensor = tensor.numpy()
tensor, numpy_tensor
#What about going back
tensor = torch.from_numpy(array) # warning: when converting from numpy -> pytorch, pytorch reflects numpy's default datatype of float64 unless specefied otherwise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment