Fix similar to numpy

please help me,
code in numpy

x_np= np.array([1,2,3])
def phi_func_np(x):
    return np.array([x, x**2])



[[1 2 3]
 [1 4 9]]

But in pytorch, it’s error

x= torch.tensor([1,2,3])
def phi_func(x):
    return torch.tensor([x, x**2])

[x, x**2]
 [tensor([1, 2, 3]), tensor([1, 4, 9])]

You cannot convert that into a tensor. You need to use or torch.stack depending on how do you want to convert it.

1 Like

Can you please explain more detail?

@JuanFMontesinos thank you, I see.

x= torch.tensor([1,2,3])
def phi_func(x):
    return torch.stack([x, x**2])


Basically you may want to concatenate in the same row or to create a new one. That’s the difference between both. I don’t know what does numpy do by default

1 Like