Creating methods#
This page looks at different ways of creating a tensor in Pytorch.
import torch
Allocating memory#
torch.Tensor
, when initialized with a set of integers as arguments, simply allocates memory for the tensor and uses the values that were previously present from earlier processes.
torch.Tensor(3, 2, 5)
tensor([[[ nan, 3.4890e-41, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]],
[[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]],
[[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00, 9.8686e+17, 2.3612e+21]]])
The same output has torch.empty
function.
torch.empty(3, 2, 5)
tensor([[[ nan, 3.4890e-41, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]],
[[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]],
[[0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00, 9.8686e+17, 2.3612e+21]]])
Specify values#
For creating tensor with specified values use torch.tensor
.
torch.tensor([[1, 2, 3], [3, 4, 5]])
tensor([[1, 2, 3],
[3, 4, 5]])
You will get the same output if you pass a list to torch.Tensor
.
torch.Tensor([[1, 2, 3], [3, 4, 5]])
tensor([[1., 2., 3.],
[3., 4., 5.]])
Note: You can specify the dtype
argument only when creating a tensor using torch.tensor
, so this is the preferred method. The following cell demonstrates how to specify dtype=torch.float
for an originally integer-type tensor.
torch.tensor([1, 2, 3], dtype=torch.float)
tensor([1., 2., 3.])
The same attempt with torch.Tensor
will result in an error.
torch.Tensor([1, 2, 3], dtype=torch.float)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[8], line 1
----> 1 torch.Tensor([1, 2, 3], dtype=torch.float)
TypeError: new() received an invalid combination of arguments - got (list, dtype=torch.dtype), but expected one of:
* (*, torch.device device)
didn't match because some of the keywords were incorrect: dtype
* (torch.Storage storage)
* (Tensor other)
* (tuple of ints size, *, torch.device device)
* (object data, *, torch.device device)
Squences#
There are functions in torch
that allow you to create sequences of elements.
torch.arange
creates values in a range with a specified step.
torch.arange(-5, 5, 1)
tensor([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4])
torch.linspace
creates a specified number of values in a range.
torch.linspace(-3, 3, 10)
tensor([-3.0000, -2.3333, -1.6667, -1.0000, -0.3333, 0.3333, 1.0000, 1.6667,
2.3333, 3.0000])
torch.logspace
creates a 1D tensor with values spaced evenly on a log scale.
torch.logspace(0, 2, 10)
tensor([ 1.0000, 1.6681, 2.7826, 4.6416, 7.7426, 12.9155, 21.5443,
35.9381, 59.9484, 100.0000])
Random distributions#
There are some functions in Torch that allow you to generate arrays of what values belong to the particle distribution. Such function listed in the following table:
Function |
Description |
---|---|
|
Generates a tensor with random values from a uniform distribution in the range [0, 1). |
|
Generates a tensor with random values from a standard normal distribution (mean 0, variance 1). |
|
Generates a tensor with random integers within a specified range. |
|
Generates a tensor with random values from a normal distribution with specified mean and std. dev. |
|
Generates a tensor of random binary values drawn from a Bernoulli distribution with specified probability of success. |
|
Generates a tensor of indices drawn from a multinomial distribution based on specified probabilities. |
|
Generates a tensor of indices drawn from a categorical distribution based on specified probabilities. |
|
Seeds the random number generator to ensure reproducibility. |
Some of them considered closer in the specific page.
Here is example of using torch.rand
.
torch.rand(3, 3, 3)
tensor([[[0.1544, 0.3327, 0.8823],
[0.7947, 0.1878, 0.3090],
[0.3538, 0.1027, 0.3000]],
[[0.2457, 0.3978, 0.9850],
[0.8457, 0.8203, 0.3024],
[0.0055, 0.9485, 0.3854]],
[[0.7654, 0.3898, 0.1176],
[0.0242, 0.6451, 0.4490],
[0.8737, 0.9906, 0.2242]]])
Same values#
There are many cases where you need a tensor that contains all the same values. This section shows some related functions.
With torch.zeros
you can generate all zeros.
torch.zeros(2, 2, 2)
tensor([[[0., 0.],
[0., 0.]],
[[0., 0.],
[0., 0.]]])
With torch.ones
you can generate all ones.
torch.ones(2, 2, 2)
tensor([[[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.]]])
With torch.full
you can generate array filled with specified value.
torch.full([2, 4], 8)
tensor([[8, 8, 8, 8],
[8, 8, 8, 8]])
Dimentionality like#
There are a number of functions in torch that generate arrays that copy dimensionality from given array. It’s typical if they end with _like
pattern. Such known functions are listed in the following table.
Function |
Description |
---|---|
|
Returns a tensor with the same size as the input tensor, but with uninitialized data. |
|
Returns a tensor with the same size as the input tensor, filled with a specified value. |
|
Returns a tensor with the same size as the input tensor, filled with ones. |
|
Returns a tensor with the same size as the input tensor, filled with random values from a uniform distribution. |
|
Returns a tensor with the same size as the input tensor, filled with random integers within a specified range. |
|
Returns a tensor with the same size as the input tensor, filled with random values from a normal distribution. |
|
Returns a tensor with the same size as the input tensor, filled with zeros. |
Here is an example of using torch.randint_like
, which is actually the same as torch.randint
but follows the dimentionality of the input tensor.
torch.randint_like(torch.Tensor(10,10), 0, 10)
tensor([[9., 2., 8., 0., 1., 0., 4., 4., 5., 0.],
[1., 2., 9., 2., 2., 8., 5., 0., 7., 9.],
[4., 2., 2., 5., 5., 9., 7., 6., 0., 7.],
[5., 5., 5., 7., 7., 0., 6., 1., 4., 2.],
[3., 5., 3., 9., 2., 4., 2., 2., 4., 5.],
[7., 5., 2., 0., 9., 8., 4., 8., 1., 7.],
[8., 5., 2., 2., 4., 2., 7., 9., 3., 8.],
[9., 0., 0., 0., 9., 7., 9., 6., 9., 7.],
[8., 9., 7., 1., 7., 4., 1., 0., 2., 6.],
[1., 2., 8., 8., 0., 7., 2., 3., 1., 9.]])
Unit matrix#
The torch.eye
function allows you to create a unit matrix.
torch.eye(5)
tensor([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])