pytorch torch.Stroage();torch.cuda()

转自:https://ptorch.com/news/52.html

torch.Storage是单个数据类型的连续的一维数组,每个torch.Tensor都具有相同数据类型的相应存储。他是torch.tensor底层数据结构,他除了像Tensor一样定义数值,还可以直接把文件映射到内存中进行操作,如果你使用的是pytorch神经网络,你不需要直接使用它们。中文文档地址:https://www.ptorch.com/docs/1/Storage

注意:任何比一维数组更复杂的都需要用到张量。

torch.Storage主要有以下几种类型:

torch.FloatStorage([1,2,3,4,5])
torch.ByteStorage([1,2,3,4,5])
torch.ShortStorage([1,2,3,4,5])
torch.IntStorage([1,2,3,4,5])
torch.LongStorage([1,2,3,4,5])
torch.FloatStorage([1,2,3,4,5])
torch.DoubleStorage([1,2,3,4,5])
torch.ShortStorage([1,2,3,4,5])

一、构造函数

  • 你可以使用torch.FloatStorage()构造一个空的Storage,构造函数的数值是随机初始化的。
  • 我们可以使用2种方法来构造torch.FloatStorage()并把它存入内存中
# 通过数组
torch.FloatStorage([1,2,3,4,5])
# 随机生成一个Storage
torch.FloatStorage(5)

二、torch.Storage的操作以及类型的转变

  • torch.Storage包含多种操作例如:size()返回此存储转的大小,tolist()返回一个包含此存储中元素的列表等等,具体参考文档torch.Storage
  • torch.FloatStorage()类型之间的转换,其实文档里有介绍,我这里给个例子:
a = torch.FloatTensor([1,2,3,4])
print a.char()
print a.long()

三、torch.Storagetorch.Tensor之间的转化

将Tensor转换为Storage非常简单,只需要storage()函数即可

import torch

a = torch.FloatTensor([1,2,3,4])
print a.storage()

它没有其他高级的用法,它是张量的一层。它可以共享多个张量的相同存储。或者更有效地处理一些操作。

torch.cuda()

转自:https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-cuda/

1

原文地址:https://www.cnblogs.com/Manuel/p/10997042.html