The torch.logspace() function in Pytorch generates a 1D tensor containing a sequence of numbers spaced logarithmically (base 10 by default) between the inclusive start and end points.
In the above figure, we are generating a tensor of five values, whose starting point is 0 and ending point is 3. The ratio between consecutive elements is constant: base^((end – start)/(steps – 1)).
Unlike torch.linspace(), which creates linearly spaced values, torch.logspace() method generates logarithmically spaced values, making it ideal for scenarios requiring exponential progression.
Syntax
torch.logspace(start,
end,
steps,
base=10.0,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
Parameters
| Argument | Description |
| start (float or Tensor) | It is the starting value of the sequence (base^start). It is a float value, but if it is a Tensor, it must be 0-dimensional. It allows non-integer exponents (e.g., start=0.5). |
| end (float or Tensor) | It is the ending value of the sequence (base^end). It is a float by default, but if it is a Tensor, it must be 0-dimensional. If start > end, values decrease exponentially. |
| steps (int) | It specifies the number of elements of the sequence. It must be an integer. |
| base (float, optional) | It is logarithmic 10 by default, but you can specify the base per your requirement. |
| out (Tensor, optional) | It specifies the output tensor to store the result. |
| dtype (torch.dtype, optional) | It specifies the output tensor’s data type. e.g. torch.int64, torch.float32, torch.float64. |
| layout (torch.layout, optional) | By default, the layout is torch.strided, but you can define other acceptable types. |
| device (torch.device, optional) | The device can be either CPU or CUDA. |
| requires_grad (bool, optional) |
If True, the tensor will track gradients (default: False). |
Basic Logarithmic Sequence (Default Base 10)
Let’s generate five logarithmically spaced values from 10^0 to 10^3.
import torch tensor = torch.logspace(start=0, end=3, steps=5) print(tensor) # Output: tensor([1.0000, 5.6234, 31.6228, 177.8279, 1000.0000])
You can see from the above program that the sequence starts at 10^0 = 1 and ends at 10^3 = 1000, with 5 points spaced logarithmically.
Custom Base (Base 2)
Let’s generate five values from 2^1 to 2^6.
import torch tensor = torch.logspace(start=1, end=6, steps=5, base=2) print(tensor) # Output: tensor([ 2.0000, 4.7568, 11.3137, 26.9087, 64.0000])
In the above program, we used base=2, which generates the sequence generates powers of 2: 2^1, 2^2, 2^3, 2^4, 2^5, and 2^6.
Negative exponents (small values)
You can pass a negative start point, which will give you a smaller value.
Let’s generate five values between 10^-3 and 10^0. (i.e., 0.001 to 1).
import torch tensor = torch.logspace(-3, 0, steps=5) print(tensor) # Output: tensor([0.0010, 0.0056, 0.0316, 0.1778, 1.0000])
Reverse Order (Descending Values)
We can set the start point > end point; in that case, it will generate the descending values.
import torch tensor = torch.logspace(3, 1, steps=3) print(tensor) # Output: tensor([1000., 100., 10.])
Single Point Edge Case
If you pass steps = 1, it will return the start point of the sequence.import torch tensor = torch.logspace(start=3, end=3, steps=1) print(tensor) # Output: tensor([1000.])
With steps=1, only base^start (i.e., 10^3 = 1000) is returned.
Specifying Data Type and Device
We can set up our tensor on Cuda and change its type to float64.
import torch tensor = torch.logspace(start=-1, end=1, steps=3, dtype=torch.float64, device='cuda') print(tensor) # Output: tensor([ 0.1000, 1.0000, 10.0000], device='cuda:0', dtype=torch.float64)
That’s all!
