pytorch 中的 torch.Tensor() 与 torch.empty() 有什么区别? [英] what's the difference between torch.Tensor() vs torch.empty() in pytorch?

查看:52
本文介绍了pytorch 中的 torch.Tensor() 与 torch.empty() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过如下.在我看来他们是一样的.pytorch 中的 torch.Tensor() 与 torch.empty() 有什么区别?

I have tried it out as below. It seems to me they're the same. What's the difference between torch.Tensor() vs torch.empty() in pytorch?

推荐答案

torch.Tensor() 只是 torch.FloatTensor() 的别名,它是张量的默认类型,当没有 dtype 时在张量构造期间指定.

torch.Tensor() is just an alias to torch.FloatTensor() which is the default type of tensor, when no dtype is specified during tensor construction.

来自 numpy 用户笔记的火炬,似乎 torch.Tensor()numpy.empty()

因此,本质上 torch.FloatTensor()torch.empty() 执行相同的工作,即返回一个充满 dtype torch 垃圾值的张量.float32.下面是一个小运行:

So, in essence torch.FloatTensor() and torch.empty() does the same job of returning a tensor filled with garbage values of dtype torch.float32. Below is a small run:

In [87]: torch.FloatTensor(2, 3)
Out[87]: 
tensor([[-1.0049e+08,  4.5688e-41, -8.9389e-38],
        [ 3.0638e-41,  4.4842e-44,  0.0000e+00]])

In [88]: torch.FloatTensor(2, 3)
Out[88]: 
tensor([[-1.0049e+08,  4.5688e-41, -1.6512e-38],
        [ 3.0638e-41,  4.4842e-44,  0.0000e+00]])

<小时>

In [89]: torch.empty(2, 3)
Out[89]: 
tensor([[-1.0049e+08,  4.5688e-41, -9.0400e-38],
        [ 3.0638e-41,  4.4842e-44,  0.0000e+00]])

In [90]: torch.empty(2, 3)
Out[90]: 
tensor([[-1.0049e+08,  4.5688e-41, -9.2852e-38],
        [ 3.0638e-41,  4.4842e-44,  0.0000e+00]])

这篇关于pytorch 中的 torch.Tensor() 与 torch.empty() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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