- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试深入了解 PyTorch 张量内存模型的工作原理。
# input numpy array
In [91]: arr = np.arange(10, dtype=float32).reshape(5, 2)
# input tensors in two different ways
In [92]: t1, t2 = torch.Tensor(arr), torch.from_numpy(arr)
# their types
In [93]: type(arr), type(t1), type(t2)
Out[93]: (numpy.ndarray, torch.FloatTensor, torch.FloatTensor)
# ndarray
In [94]: arr
Out[94]:
array([[ 0., 1.],
[ 2., 3.],
[ 4., 5.],
[ 6., 7.],
[ 8., 9.]], dtype=float32)
我知道 PyTorch 张量共享 NumPy ndarray 的内存缓冲区。因此,改变一个将反射(reflect)在另一个。所以,我在这里切片和更新张量中的一些值 t2
In [98]: t2[:, 1] = 23.0
正如预期的那样,它在 t2
和 arr
中更新,因为它们共享相同的内存缓冲区。
In [99]: t2
Out[99]:
0 23
2 23
4 23
6 23
8 23
[torch.FloatTensor of size 5x2]
In [101]: arr
Out[101]:
array([[ 0., 23.],
[ 2., 23.],
[ 4., 23.],
[ 6., 23.],
[ 8., 23.]], dtype=float32)
但是,t1
也更新了。请记住,t1
是使用 torch.Tensor()
构造的,而 t2
是使用 torch.from_numpy()
< 构造的/p>
In [100]: t1
Out[100]:
0 23
2 23
4 23
6 23
8 23
[torch.FloatTensor of size 5x2]
所以,无论我们是否使用 torch.from_numpy()
或 torch.Tensor()
要从 ndarray 构造张量,所有这样的张量和 ndarray 共享相同的内存缓冲区。
基于这样的理解,我的问题是为什么一个专用函数torch.from_numpy()
仅在 torch.Tensor()
时存在能胜任吗?
我查看了 PyTorch 文档,但它没有提到任何关于此的内容?有什么想法/建议吗?
最佳答案
from_numpy()
自动继承输入数组dtype
。另一方面,torch.Tensor
是 torch.FloatTensor
的别名。
因此,如果将 int64
数组传递给 torch.Tensor
,则输出张量为浮点张量,它们不会共享存储。 torch.from_numpy
按预期为您提供 torch.LongTensor
。
a = np.arange(10)
ft = torch.Tensor(a) # same as torch.FloatTensor
it = torch.from_numpy(a)
a.dtype # == dtype('int64')
ft.dtype # == torch.float32
it.dtype # == torch.int64
关于python - PyTorch 内存模型 : "torch.from_numpy()" vs "torch.Tensor()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48482787/
我想向 Torch 添加一个损失函数,用于计算预测值和目标值之间的编辑距离。 有没有一种简单的方法来实现这个想法? 还是我必须编写自己的具有向后和向前功能的类? 最佳答案 如果您的标准可以表示为现有模
我如何沿着 torch 中的列求和?我有一个 128*1024 的张量,我想通过对所有行求和得到一个 1*1024 的张量。 例如:一个: 1 2 3 4 5 6 我想要b 5 7 9 最佳答案 为此
阅读pytorch文档后,我仍然需要帮助来理解torch.mm、torch.matmul和torch.mul之间的区别.由于我不完全理解它们,我无法简明扼要地解释这一点。 B = torch.tens
minibatch = torch.Tensor(5, 2, 3,5) m = nn.View(-1):setNumInputDims(1) m:forward(minibatch) 给出一个大小
有两个 PyTorch 存储库: https://github.com/hughperkins/pytorch https://github.com/pytorch/pytorch 第一个显然需要 T
晚上好, 我刚刚安装了 PyTorch 0.4.0,我正在尝试执行第一个教程“什么是 PyTorch?” 我编写了一个 Tutorial.py 文件,我尝试使用 Visual Studio Code
我有一个浮点值列表(或一个 numpy 数组)。我想创建一个包含所有这些值的一维 torch 张量。我可以创建 torch 张量并运行循环来存储值。 但我想知道有没有什么办法,我可以使用列表或数组中的
这是我在将 convertinf DQN 转换为 Double DQN 来解决 cartpole 问题时遇到的问题。我快要弄清楚了。 tensor([0.1205, 0.1207, 0.1197, 0
鉴于: x_batch = torch.tensor([[-0.3, -0.7], [0.3, 0.7], [1.1, -0.7], [-1.1, 0.7]]) 然后申请 torch.sigmoid(
我正在学习一门类(class),该类(class)使用已弃用的 PyTorch 版本,该版本不会根据需要将 torch.int64 更改为 torch.LongTensor。当前引发错误的代码部分是:
我正在尝试从 this repo 运行代码.我通过将 main.py 中的第 39/40 行从更改为禁用了 cuda parser.add_argument('--type', default='to
从 0.4.0 版本开始,可以使用 torch.tensor 和 torch.Tensor 有什么区别?提供这两个非常相似且令人困惑的替代方案的原因是什么? 最佳答案 在 PyTorch 中,torc
用于强化学习的 OpenAI REINFORCE 和 actor-critic 示例具有以下代码: REINFORCE : policy_loss = torch.cat(policy_loss).s
我在装有 CentOS Linux 7.3.1611(核心)操作系统的计算机上使用 Python 3.5.1。 我正在尝试使用 PyTorch 并开始使用 this tutorial . 不幸的是,示
我正在尝试使用 torch.load 加载预训练模型。 我收到以下错误: ModuleNotFoundError: No module named 'utils' 我已通过从命令行打开它来检查我使用的
这篇文章与我之前的 How to define a Python Class which uses R code, but called from rTorch? 有关. 我在 R ( https:/
是否torch.manual_seed包括torch.cuda.manual_seed_all的操作? 如果是,我们可以使用 torch.manual_seed设置种子。否则我们应该调用这两个函数。
我们可以使用 torch.Tensor([1., 2.], device='cuda') 在 GPU 上分配张量.使用这种方式而不是torch.cuda.Tensor([1., 2.])有什么不同吗?
我正在尝试深入了解 PyTorch 张量内存模型的工作原理。 # input numpy array In [91]: arr = np.arange(10, dtype=float32).resha
我同时安装了 python38,37 和 anaconda,操作系统 - win10,x64。 我无法在 py38,37 中安装 torch - 但在 anaconda 中安装了它。 系统环境变量“路
我是一名优秀的程序员,十分优秀!