Skip to content

Instantly share code, notes, and snippets.

@andreaschandra
Created August 26, 2021 04:30
Show Gist options
  • Save andreaschandra/22e769d0d7bbd29d1b3a01abf3154d31 to your computer and use it in GitHub Desktop.
Save andreaschandra/22e769d0d7bbd29d1b3a01abf3154d31 to your computer and use it in GitHub Desktop.
import torch
# create a tensor 1D
torch.rand(10)
> tensor([0.5998, 0.7840, 0.1017, 0.5188, 0.7417, 0.3671, 0.7304, 0.4467, 0.7782,
0.0533])
# create a tensor 2D
torch.rand(4,4)
> tensor([[0.8032, 0.8408, 0.6130, 0.8030],
[0.8343, 0.0078, 0.9008, 0.8328],
[0.7299, 0.3207, 0.2760, 0.9779],
[0.5821, 0.5505, 0.0618, 0.4833]])
# create a tensor integer
torch.randint(low=0, high=100, size=(4,))
> tensor([63, 74, 75, 71])
# create a tensor from list
torch.Tensor([1,2,3])
> tensor([1., 2., 3.])
# create a Long Tensor
torch.LongTensor([1,2,3])
> tensor([1, 2, 3])
torch.FloatTensor([1.1, 1.2, 1.3])
> tensor([1.1000, 1.2000, 1.3000])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment