参考 torch、(三) Random sampling - 云+社区 - 腾讯云
目录
torch.manual_seed(seed)[source]
torch.set_rng_state(new_state)[source]
torch.default_generator Returns the default CPU torch.Generator
torch.bernoulli(input, *, generator=None, out=None) → Tensor
torch.multinomial(input, num_samples, replacement=False, out=None) → LongTensor
torch.normal(mean=0.0, std, out=None) → Tensor
torch.normal(mean, std=1.0, out=None) → Tensor
torch.normal(mean, std, size, *, out=None) → Tensor
torch.rand_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor
torch.randn_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor
class torch.quasirandom.SobolEngine(dimension, scramble=False, seed=None)[source]
draw(n=1, out=None, dtype=torch.float32)[source]
torch.
seed
()[source]
Sets the seed for generating random numbers to a non-deterministic random number. Returns a 64 bit number used to seed the RNG.
torch.
manual_seed
(seed)[source]
Sets the seed for generating random numbers. Returns a torch.Generator object.
Parameters
seed (int) – The desired seed.
torch.
initial_seed
()[source]
Returns the initial seed for generating random numbers as a Python long.
torch.
get_rng_state
()[source]
Returns the random number generator state as a torch.ByteTensor.
torch.
set_rng_state
(new_state)[source]
Sets the random number generator state.
Parameters
new_state (torch.ByteTensor) – The desired state
torch.
default_generator
Returns the default CPU torch.Generator
torch.
bernoulli
(input, *, generator=None, out=None) → Tensor
Draws binary random numbers (0 or 1) from a Bernoulli distribution.
The input
tensor should be a tensor containing probabilities to be used for drawing the binary random number. Hence, all values in input
have to be in the range: 0≤inputi≤10 \leq \text{input}_i \leq 10≤inputi≤1 .
The ith\text{i}^{th}ith element of the output tensor will draw a value 111 according to the ith\text{i}^{th}ith probability value given in input
.
outi∼Bernoulli(p=inputi)\text{out}_{i} \sim \mathrm{Bernoulli}(p = \text{input}_{i}) outi∼Bernoulli(p=inputi)
The returned out
tensor only has values 0 or 1 and is of the same shape as input
.
out
can have integral dtype
, but input
must have floating point dtype
.
Parameters
-
input (Tensor) – the input tensor of probability values for the Bernoulli distribution
-
out (Tensor, optional) – the output tensor.
Example:
>>> a = torch.empty(3, 3).uniform_(0, 1) # generate a uniform random matrix with range [0, 1]
>>> a
tensor([[ 0.1737, 0.0950, 0.3609],
[ 0.7148, 0.0289, 0.2676],
[ 0.9456, 0.8937, 0.7202]])
>>> torch.bernoulli(a)
tensor([[ 1., 0., 0.],
[ 0., 0., 0.],
[ 1., 1., 1.]])
>>> a = torch.ones(3, 3) # probability of drawing "1" is 1
>>> torch.bernoulli(a)
tensor([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> a = torch.zeros(3, 3) # probability of drawing "1" is 0
>>> torch.bernoulli(a)
tensor([[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]])
torch.
multinomial
(input, num_samples, replacement=False, out=None) → LongTensor
Returns a tensor where each row contains num_samples
indices sampled from the multinomial probability distribution located in the corresponding row of tensor input
.
Note
The rows of input
do not need to sum to one (in which case we use the values as weights), but must be non-negative, finite and have a non-zero sum.
Indices are ordered from left to right according to when each was sampled (first samples are placed in first column).
If input
is a vector, out
is a vector of size num_samples
.
If input
is a matrix with m rows, out
is an matrix of shape (m×num_samples)(m \times \text{num\_samples})(m×num_samples) .
If replacement is True
, samples are drawn with replacement.
If not, they are drawn without replacement, which means that when a sample index is drawn for a row, it cannot be drawn again for that row.
Note
When drawn without replacement, num_samples
must be lower than number of non-zero elements in input
(or the min number of non-zero elements in each row of input
if it is a matrix).
Parameters
-
input (Tensor) – the input tensor containing probabilities
-
num_samples (int) – number of samples to draw
-
replacement (bool, optional) – whether to draw with replacement or not
-
out (Tensor, optional) – the output tensor.
Example:
>>> weights = torch.tensor([0, 10, 3, 0], dtype=torch.float) # create a tensor of weights
>>> torch.multinomial(weights, 2)
tensor([1, 2])
>>> torch.multinomial(weights, 4) # ERROR!
RuntimeError: invalid argument 2: invalid multinomial distribution (with replacement=False,
not enough non-negative category to sample) at ../aten/src/TH/generic/THTensorRandom.cpp:320
>>> torch.multinomial(weights, 4, replacement=True)
tensor([ 2, 1, 1, 1])
torch.
normal
()
torch.
normal
(mean, std, out=None) → Tensor
Returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given.
The mean is a tensor with the mean of each output element’s normal distribution
The std is a tensor with the standard deviation of each output element’s normal distribution
The shapes of mean and std don’t need to match, but the total number of elements in each tensor need to be the same.
Note
When the shapes do not match, the shape of mean is used as the shape for the returned output tensor
Parameters
-
mean (Tensor) – the tensor of per-element means
-
std (