정성헌
정성헌

Reputation: 82

How can I make torch tensor?

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

Answers (1)

Shai
Shai

Reputation: 114926

Why not:

tmp = -1 + 2 * torch.randint(low=0, high=2, size=(4, 4))

Upvotes: 3

Related Questions