如何在PyTorch中使用torch.rand()和torch.rand_like()来创建随机张数

1,672 阅读4分钟

简介

本教程将告诉你如何在PyTorch中通过使用torch.rand()和torch.rand_like()函数来创建具有随机值的张量。在深度学习中,创建用随机数初始化的张量是一个非常常见的操作。我们将借助实例来解释这些函数,以便更好地理解。

使用torch.rand()的PyTorch随机张量

torch.rand函数用于用均匀分布中的随机值创建一个张量,该张量位于区间[0,1]之间,即大于或等于0且小于1。

让我们通过例子更好地理解这一点,但在此之前让我们导入PyTorch库。

在[0]

import torch;

例子 - 1:用torch.rand()创建二维随机张量

在第一个例子中,我们要用torch.rand函数生成一个大小为3×3的随机张量。有三种不同的语法可以做到这一点 --

方法 - 1:

在这个例子中,我们只是把3,3的大小传递给torch.rand(),如下图所示。

In [1]:

tensor = torch.rand(3,3)

tensor

输出[1]

tensor([[0.6389, 0.8245, 0.2545],
        [0.5153, 0.2170, 0.6151],
        [0.3091, 0.7528, 0.5183]])

方法--2。

在这个方法中,我们可以把大小作为一个列表[3,3]传给torch.rand(),如下图所示。

在[2]中

tensor = torch.rand(size = [3,3])

tensor

Out[2]:

tensor([[0.6389, 0.8245, 0.2545],
        [0.5153, 0.2170, 0.6151],
        [0.3091, 0.7528, 0.5183]])

方法 - 3:

在这个方法中,我们可以将大小作为一个元组(3,3)传递给torch.rand(),如下图所示。

在[3]中

tensor = torch.rand(size = (3,3))

tensor

输出[3]

tensor([[0.6389, 0.8245, 0.2545],
        [0.5153, 0.2170, 0.6151],
        [0.3091, 0.7528, 0.5183]])

例子-2:创建一维随机张量

在这个例子中,我们要创建一个10个元素的一维随机张量。

在[4]中

tensor = torch.rand(10);

tensor

输出[4]

tensor([0.8266, 0.7850, 0.4197, 0.9229, 0.7172, 0.8324, 0.3692, 0.4916, 0.7819,
        0.7687])

例子-3:创建3维随机张量

在下面的例子中,我们将创建大小为2x3x3的三维PyTorch张量。

在[5]中

tensor = torch.rand(size=(2,3,3))

tensor

Out[5]:

tensor([[[0.6133, 0.9458, 0.9013],
         [0.4031, 0.5596, 0.0875],
         [0.5414, 0.9022, 0.8020]],

        [[0.2309, 0.2696, 0.6551],
         [0.7222, 0.2852, 0.8607],
         [0.8311, 0.4471, 0.2992]]])

例子 - 4: 使用dtype参数与torch.rand的关系

在torch.rand函数中,type参数被用来指定数据类型,如下图所示。

在[6]中

tensor = torch.rand(size=(3,3), dtype=torch.float32)

tensor

输出[6]

tensor([[0.3449, 0.0083, 0.6991],
        [0.4702, 0.8682, 0.2594],
        [0.1540, 0.4070, 0.5919]])

使用Torch.rand()的Int数据类型的问题。

如果你试图用type参数指定int数据类型,这里有一个陷阱。让我们看看下面的例子,当我们用dtype传递int时,输出会出错。这是因为torch.rand只生成区间[0,1]之间的数据,即大于等于0和小于1的数据,因为它总是低于1,所以它永远无法生成一个整数,因此抛出了错误。

在[7]中

tensor = torch.rand(size=(3,3), dtype=torch.int32);

tensor

输出[7]

---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-28-9554a5ee2190> in <module>()
----> 1 tensor = torch.rand(size=(3,3), dtype=torch.int32);
2 tensor

RuntimeError: "check_uniform_bounds" not implemented for 'Int'

例子 - 5: 使用torch.rand的种子值

每当你使用torch.rand()函数时,它每次都会随机地产生不同的值。然而,如果你想确保每次调用这个函数时都能生成相同的随机值,那么我们就必须传递种子值,如下所述。

我们首先创建一个生成器对象gen,并用manual_seed()初始化它的种子值。然后我们在torch.rand中使用这个生成器对象,如下图所示。现在,无论何时我们使用这段代码,它都会生成具有相同数值的张量。

In [8]:

gen = torch.Generator()
gen.manual_seed(2947587447)
tensor = torch.rand(size=(4,3), generator = gen)
tensor

输出[8]

tensor([[0.0371, 0.6312, 0.3432],
        [0.3925, 0.3106, 0.4346],
        [0.1148, 0.1085, 0.2297],
        [0.7899, 0.3889, 0.0603]])

使用torch.rand_like()的PyTorch随机张量

有时,你想在PyTorch中创建一个随机张量,其大小与另一个张量相同。你可以手动获取其他张量的大小,然后创建随机张量。然而,有一个更方便的方法,即使用torch.rand_like()函数来做这件事。让我们通过实例来了解这个问题。

例子-1:用torch.rand_like()创建2维随机张量

首先,我们将创建一个零值的张量,它将被用来创建相同大小的随机张量。接下来,我们将这个张量的名称传递给torch.rand_like函数。

In[9]:

reference_tensor = torch.zeros(size=(3,4))

reference_tensor

Out[9]:

tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])

在[10]中

tensor = torch.rand_like(reference_tensor)

tensor

Out[10]:

tensor([[[0.8383, 0.9491, 0.6800, 0.6901],
         [0.4085, 0.5711, 0.7195, 0.0400],
         [0.7486, 0.7667, 0.2196, 0.8198]],

        [[0.7664, 0.1315, 0.7272, 0.4294],
         [0.7604, 0.1315, 0.7219, 0.7277],
         [0.5066, 0.0683, 0.4788, 0.6700]]])

例子 - 2: 用torch.rand_like()创建3维随机张量

这里我们创建一个三维零张量,然后用它作为参考来创建随机张量。

在[11]

reference_tensor = torch.zeros(size=(2,3,4))

reference_tensor

Out[11]:

tensor([[[0., 0., 0., 0.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]],

        [[0., 0., 0., 0.],
         [0., 0., 0., 0.],
         [0., 0., 0., 0.]]])

在[12]中

tensor = torch.rand_like(reference_tensor)

tensor

Out[12]:

tensor([[[0.1704, 0.4320, 0.6740, 0.4405],
         [0.5392, 0.0576, 0.6135, 0.2433],
         [0.2174, 0.9259, 0.7687, 0.5202]],

        [[0.8903, 0.2329, 0.1755, 0.6491],
         [0.5983, 0.3904, 0.4308, 0.4082],
         [0.6989, 0.9511, 0.8717, 0.9099]]])