pytorch.empty 函数中的未初始化数据是什么 [英] what is uninitialized data in pytorch.empty function

查看:283
本文介绍了pytorch.empty 函数中的未初始化数据是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 pytorch 教程并遇到了 pytorch.empty 函数.有人提到空可以用于未初始化的数据.但是,当我打印它时,我得到了一个值.这和 pytorch.rand 有什么区别,它也生成数据(我知道 rand 在 0 和 1 之间生成).下面是我试过的代码

a = torch.empty(3,4)打印(一)

输出:

<块引用>

张量([[ 8.4135e-38, 0.0000e+00, 6.2579e-41, 5.4592e-39],[-5.6345e-08, 2.5353e+30, 5.0447e-44, 1.7020e-41],[ 1.4000e-38, 5.7697e-05, 2.5353e+30, 2.1580e-43]])

b = torch.rand(3,4)打印(b)

输出:

<块引用>

张量([[ 0.1514, 0.8406, 0.2708, 0.3422],[ 0.7196, 0.6120, 0.4476, 0.6705],[ 0.6989, 0.2086, 0.5100, 0.8285]])

这里是官方文档的链接

解决方案

一旦您致电 torch.empty(),根据张量的大小(形状)分配一块内存.未初始化的数据是指torch.empty() 将简单地按原样返回内存块中的值.这些值可能是默认值,也可能是由于某些其他操作而存储在这些内存块中的值,这些操作之前使用了该部分内存块.

<小时>

这是一个简单的说明:

# 一个包含值的内存块在 [74] 中:torch.empty(2, 3)出[74]:张量([[-1.0049e+08, 4.5688e-41, -9.1450e-38],[ 3.0638e-41, 4.4842e-44, 0.0000e+00]])# 相同的运行;但请注意值的变化.# 即使用了与上一次运行不同的内存地址.在 [75] 中:torch.empty(2, 3)出[75]:张量([[-1.0049e+08, 4.5688e-41, -7.9421e-38],[ 3.0638e-41, 4.4842e-44, 0.0000e+00]])

i was going through pytorch tutorial and came across pytorch.empty function. it was mentioned that empty can be used for uninitialized data. But, when i printed it, i got a value. what is the difference between this and pytorch.rand which also generates data(i know that rand generates between 0 and 1). Below is the code i tried

a = torch.empty(3,4)
print(a)

Output:

tensor([[ 8.4135e-38,  0.0000e+00,  6.2579e-41,  5.4592e-39],
        [-5.6345e-08,  2.5353e+30,  5.0447e-44,  1.7020e-41],
        [ 1.4000e-38,  5.7697e-05,  2.5353e+30,  2.1580e-43]])

b = torch.rand(3,4)
print(b)

Output:

tensor([[ 0.1514,  0.8406,  0.2708,  0.3422],
        [ 0.7196,  0.6120,  0.4476,  0.6705],
        [ 0.6989,  0.2086,  0.5100,  0.8285]])

Here is the link to official documentation

解决方案

Once you call torch.empty(), a block of memory is allocated according to the size (shape) of the tensor. By uninitialized data, it's meant that torch.empty() would simply return the values in the memory block as is. These values could be default values or it could be the values stored in those memory blocks as a result of some other operations, which used that part of the memory block before.


Here's a simple illustration:

# a block of memory with the values in it
In [74]: torch.empty(2, 3)
Out[74]: 
tensor([[-1.0049e+08,  4.5688e-41, -9.1450e-38],
        [ 3.0638e-41,  4.4842e-44,  0.0000e+00]])

# same run; but note the change in values.
# i.e. different memory addresses than on the previous run were used.
In [75]: torch.empty(2, 3)
Out[75]: 
tensor([[-1.0049e+08,  4.5688e-41, -7.9421e-38],
        [ 3.0638e-41,  4.4842e-44,  0.0000e+00]])

这篇关于pytorch.empty 函数中的未初始化数据是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆