Hi, Dear forum:
I want to get the signbit of a tensor. Pytorch provide a sign() function, however, it return 0 for zero. Is any way to return 1 if 0 and pos, otherwise -1? I only want [1, -1] rather [1, 0, -1].
Hi @singleroc,
this should give you what you want
float arr[] = {-1, 0, 1};
auto data = torch::from_blob(arr, 3);
auto sizes = data.sizes();
auto sign = torch::ones(sizes).where(data.ge(torch::zeros(sizes)), torch::full(sizes, -1));
std::cout << sign << std::endl;
Hi, dear @mhubii
Thanks for your answer. The code seems to be C++ version. Today I come up with an idea with python:
def get_copysign(x):
y = x.sign()
y.masked_fill_(x == 0, 1)
return y
hm yes because this is the c++ channel of the forum