I tried doing a ToTensor transform on data that is already a torch tensor but got this error:
TypeError: pic should be PIL Image or ndarray. Got <class 'torch.Tensor'>
Why doesn’t it just do a No Op and return the original data?
sample code:
# saving torch tensors
import torch
import torch.nn as nn
import torchvision
from pathlib import Path
from collections import OrderedDict
path = Path('~/data/tmp/').expanduser()
path.mkdir(parents=True, exist_ok=True)
tensor_a = torch.rand(2,3)
tensor_b = torch.rand(1,3)
db = {'a': tensor_a, 'b': tensor_b}
torch.save(db, path/'torch_db')
loaded = torch.load(path/'torch_db')
print( loaded['a'] == tensor_a )
print( loaded['b'] == tensor_b )
# testing if ToTensor() screws things up
lb, ub = -1, 1
N, Din, Dout = 3, 1, 1
x = torch.distributions.Uniform(low=lb, high=ub).sample((N, Din))
print(x)
f = nn.Sequential(OrderedDict([
('f1', nn.Linear(Din,Dout)),
('out', nn.SELU())
]))
y = f(x)
transform = torchvision.transforms.transforms.ToTensor()
y_proc = transform(y)
print(y_proc)
related: What is the recommended format to save data in pytorch?