0

What does it mean by only one element tensors can be converted to Python scalars in this case and how do I suppose to debug it?

x1 = (max-min)*torch.rand(1, 21) + min
x2 = (max-min)*torch.rand(1, 21) + min
zipped_list = zip(x1, x2)
y = [math.sin(2*x1+2) * math.cos(0.5*x2)+0.5 for (x1, x2) in zipped_list]

output

ValueError: only one element tensors can be converted to Python scalars
MIT
  • 17
  • 4

2 Answers2

0

You get that error because your torch tensors (x1 and x2) are not a single element tensor.

t = torch.tensor([10, 20])
print(t.item()) # This will throw an error since the tensor has more than 1 element
t = torch.tensor([10])
t.item() # This will print 10

To fix your issue, replace math.sin and math.cos calls with torch.sin and torch.cos. The torch.sin or torch.cos computes the sine or cosine value on all the elements.

planet_pluto
  • 742
  • 4
  • 15
0

problem is because of using math.cos and math.sin. math.cos isn't vectorized, but np.cos is. use np.sin and np.cos or torch.sinand torch.cos.

Pygirl
  • 12,969
  • 5
  • 30
  • 43