Reputation: 82
I want to make a torch tensor composed of only 1 or -1.
I just try to use torch.empty()
and torch.randn()
.
tmp = torch.randint(low=0, high=2, size=(4, 4))
tmp[tmp==0] = -1
tmp
>> tensor([[ 1, -1, -1, 1],
[-1, 1, 1, -1],
[-1, -1, 1, 1],
[-1, 1, -1, 1]])
However, I don`t know what method is most efficient in time.
And I want to make this code to one line as possible because this code is positioned at __init__ ()
Upvotes: 2
Views: 112