PyTorch学习笔记——Tensor张量的数据类型的转化、Tensor常见的数据类型、快速创建Tensor

Tensor类型与numpy类型、list类型数据的相互转化

函数 功能
tensor.numpy() Tensor类型转变为numpy类型
torch.from_numpy(ndarray) numpy类型转变为Tensor类型
tensor.tolist() Tensor类型转变为list类型
torch.tensor(list) list类型转变为Tensor类型
import torch 
from torch.autograd import Variable

x = torch.ones([2,5])
print(x)
>>>
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])
# tensor ----->numpy
b = x.numpy()
print(b)
>>>
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]], dtype=float32)
# numpy ----->tensor
torch.from_numpy(b) 
>>>
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])
# tensor—>list
c = x.tolist()
print(c)
>>>
[[1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0]]
# list—>tensor
d = list(range(1,6))
print(d)
>>>
[1, 2, 3, 4, 5]
e = torch.tensor(a)
print(e)
>>>
tensor([1, 2, 3, 4, 5])                     

参考链接:
pytorch中tensor张量数据类型的转化

pytorch中Tensor的数据类型

函数 数据类型 实例
torch.FloatTensor() 32位浮点型——torch.Tensor默认的数据类型是32位float类型 torch.Tensor( [[2,3],[4,8],[7,9]] )或者torch.FloatTensor( [[2,3],[4,8],[7,9]] )
torch.DoubleTensor() 64位浮点型 torch.DoubleTensor( [[2,3],[4,8],[7,9]] )
torch.ShortTensor() 16位整型 torch.ShortTensor( [[2,3],[4,8],[7,9]] )
torch.IntTensor() 32位整型 torch.IntTensor( [[2,3],[4,8],[7,9]] )
torch.LongTensor() 64位整型 torch.LongTensor( [[2,3],[4,8],[7,9]] )
import torch 
from torch.autograd import Variable

# 32位浮点型
torch.FloatTensor([[2,3],[4,5],[6,7]])
>>>
tensor([[2., 3.],
        [4., 5.],
        [6., 7.]])
# 64位浮点型
torch.DoubleTensor([[2,3],[4,5],[6,7]])
>>>
tensor([[2., 3.],
        [4., 5.],
        [6., 7.]], dtype=torch.float64)
# 16位整型
torch.ShortTensor([[2,3],[4,5],[6,7]])
>>>
tensor([[2, 3],
        [4, 5],
        [6, 7]], dtype=torch.int16)
# 32位整型
torch.IntTensor([[2,3],[4,5],[6,7]])
>>>
tensor([[2, 3],
        [4, 5],
        [6, 7]], dtype=torch.int32)
# 64为整型
torch.LongTensor([[2,3],[4,5],[6,7]])
>>>
tensor([[2, 3],
        [4, 5],
        [6, 7]])
        

Tensor的数据类型转化——int、long、double、float、half等


tensor = torch.Tensor(3, 5)  # 每次调用,随时生成一组数据
print(tensor)
>>>
tensor([[0.0000e+00, 4.6566e-10, 0.0000e+00, 4.6566e-10, 1.1210e-44],
        [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]])
        
# torch.long() 将tensor投射为long类型
newtensor = tensor.long()
print(newtensor)
>>>
tensor([[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]])

# torch.half()将tensor投射为半精度浮点类型
newtensor = tensor.half()
print(newtensor)
>>>
tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]], dtype=torch.float16)
        
# torch.int()将该tensor投射为int类型
newtensor = tensor.int()
print(newtensor)
>>>
tensor([[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]], dtype=torch.int32)

# torch.double()将该tensor投射为double类型
newtensor = tensor.double()
print(newtensor)
>>>
tensor([[0.0000e+00, 4.6566e-10, 0.0000e+00, 4.6566e-10, 1.1210e-44],
        [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]],
       dtype=torch.float64)
# torch.float()将该tensor投射为float类型
newtensor = tensor.float()
print(newtensor)
>>>
tensor([[0.0000e+00, 4.6566e-10, 0.0000e+00, 4.6566e-10, 1.1210e-44],
        [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00],
        [0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00, 0.0000e+00]])

# torch.char()将该tensor投射为char类型
newtensor = tensor.char()
print(newtensor)
>>>
tensor([[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]], dtype=torch.int8)
        
# torch.byte()将该tensor投射为byte类型
newtensor = tensor.byte()
print(newtensor)
>>>
tensor([[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]], dtype=torch.uint8)
        
# torch.short()将该tensor投射为short类型
newtensor = tensor.short()
print(newtensor)
>>>
tensor([[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]], dtype=torch.int16)

快速创建Tensor一览表

函数 功能 实例
torch.zeros(*size, out=None, dtype=None) 创建一个元素全部填充 0,形状等于参数size的Tensor torch.zeros( size=(4,5),dtype=torch.float32)
torch.randn(*size, out=None, dtype=None) 创建一个元素填充随机数,形状等于参数size的Tensor——随机数满足标准正态分布(mean 0 and variance 1 torch.randn( size=(4,5),dtype=torch.float32)
torch.rand(*sizes, out=None) 返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数。张量的形状由参数sizes定义。 torch.rand(5,3)
torch.normal(means, std, out=None) 离散正态分布
torch.linspace(start, end, steps=100, out=None) 线性间距向量
torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False) -> Tensor 直接使用数据data,构造一个张量(Constructs a tensor with :attr:data.) torch.tensor([5.5, 3]) # 将列表转换维tensor类型

备注:
torch.IntTensor(2, 4).zero_() 与torch.zeros(2,4).int()的效果相同,均得到一个2*4的填充元素均为零的Tensor。

torch.empty和torch.zeros的区别

torch.empty()函数:

torch.empty(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) -> Tensor

Returns a tensor filled with uninitialized data(未初始化的数). The shape of the tensor is
defined by the variable argument :attr:size.

torch.zeros()函数:

zeros(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor

Returns a tensor filled with the scalar value 0(标准值0), with the shape defined
by the variable argument :attr:size.

torch.Tensor和torch.tensor的区别

  1. torch.Tensor()是Python,更明确讲,是默认张量类型torch.FloatTensor()的别名

创建Tensor原理:

torch.Tensor([1,2]) 会调用Tensor类的构造函数__init__,生成单精度浮点类型的张量

a = torch.Tensor([1, 2])
print(a.type())
print(a)
>>>
torch.FloatTensor
tensor([1., 2.])

torch.Tensor(3, 5)这样仅指定生成Tensor的shape时,将默认生成填充值均为0的单精度浮点类型的张量

扫描二维码关注公众号,回复: 12679550 查看本文章
# 仅指定生成Tensor的shape时,默认生成填充值均为0的单精度浮点型张量
tensor = torch.Tensor(3, 5)
print(tensor)
>>>
tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])

这里再说一下torch.empty(),根据 https://pytorch.org/docs/stable/torch.html?highlight=empty#torch.empty ,我们可以生成指定类型、指定设备以及其他参数的张量,由于torch.Tensor()只能指定数据类型为torch.float,所以torch.Tensor()可以看做torch.empty()的一个特殊情况

  1. torch.tensor()仅仅是Python的函数,函数原型是:
torch.tensor(data, dtype=None, device=None, requires_grad=False)

其中,data可以是:list, tuple, array, scalar等类型

torch.tensor()可以从data中的数据部分做拷贝(而不是直接引用),根据原始数据类型生成相应的torch.LongTensor,torch.FloatTensor,torch.DoubleTensor。

# 根据原始数据类型生成相应的torch.LongTensor
a = torch.tensor([1, 2])
print(a.type())
print(a)
>>>
torch.LongTensor
tensor([1, 2])
# 根据原始数据类型生成相应的torch.FloatTensor
a = torch.tensor([1., 2.])
print(a.type())
print(a)
>>>
torch.FloatTensor
tensor([1., 2.])
# 根据原始数据类型生成相应的torch.DoubleTensor
a = np.zeros(2, dtype=np.float64)
a = torch.tensor(a)
print(a.type())
print(a)
>>>
torch.DoubleTensor
tensor([0., 0.], dtype=torch.float64)

参考链接:
torch.Tensor和torch.tensor的区别

随机抽样类函数——torch.random

1.torch.random.manual_seed(seed) → torch._C.Generator

设置用于生成随机数的种子。返回 torch.Generator对象。

参数:seed(int)–所需的种子。值必须在包含范围内 [-0x8000_0000_0000_0000,0xffff_ffff_ffff_ffff]。否则,将引发RuntimeError。使用公式0xffff_ffff_ffff_ffff + seed将负输入重新映射为正值 。

2.torch.random.initial_seed() → int

返回用于生成随机数的Python的long的初始种子。

3.torch.random.get_rng_state() → torch.Tensor

以torch.ByteTensor的形式返回随机数生成器的状态。

4.torch.random.set_rng_state(new_state) → None

设定随机生成器状态

参数:new_state(torch.Byte Tensor)-期望的状态

5.torch.random.fork_rng(devices=None, enabled=True, _caller='fork_rng', _devices_kw='devices')

派生RNG,以便在您返回时将RNG重置为之前的状态。

参数:
devices (可迭代CUDA ID)–为其派生RNG的CUDA设备。CPU RNG状态始终为派生。默认情况下,该功能fork_rng()可在所有设备上运行,但如果您的计算机上有很多设备,则将发出警告,因为在这种情况下此功能运行非常缓慢。如果您明确指定设备,该警告将被取消

enabled(bool)–如果False,则不派生RNG。这是一个方便的参数,用于轻松禁用上下文管理器,而不必删除它并取消其下的Python代码的缩进。

6.torch.bernoulli(input, *, generator=None, out=None) -> Tensor

从伯努利分布中随机抽取二元随机数(0 or 1)(Draws binary random numbers (0 or 1) from a Bernoulli distribution.)

torch.bernoulli()函数用法实例:

a = torch.empty(3, 3).uniform_(0, 1)  # generate a uniform random matrix with range [0, 1]
>>> a
 tensor([[ 0.1737,  0.0950,  0.3609],
         [ 0.7148,  0.0289,  0.2676],
         [ 0.9456,  0.8937,  0.7202]])
 >>> torch.bernoulli(a)  # a的元素值>0.5则取1,否则,取值0
 tensor([[ 1.,  0.,  0.],
         [ 0.,  0.,  0.],
         [ 1.,  1.,  1.]])

 >>> a = torch.ones(3, 3) # probability of drawing "1" is 1
 >>> torch.bernoulli(a)
 tensor([[ 1.,  1.,  1.],
         [ 1.,  1.,  1.],
         [ 1.,  1.,  1.]])
 >>> a = torch.zeros(3, 3) # probability of drawing "0" is 0
 >>> torch.bernoulli(a)
 tensor([[ 0.,  0.,  0.],
         [ 0.,  0.,  0.],
         [ 0.,  0.,  0.]])

7.multinomial(input, num_samples, replacement=False, *, generator=None, out=None) -> LongTensor

返回一个张量,其中每一行都包含num_samples从位于张量对应行中的多项式概率分布中采样的索引input。(Returns a tensor where each row contains :attr:num_samples indices sampled from the multinomial probability distribution located in the corresponding row
of tensor :attr:input.), 官方文档:TORCH.MULTINOMIAL

注意

的各行input不必总和为1(在这种情况下,我们将这些值用作权重),但必须为非负数,有限且总和为非零。

根据每个样本的采样时间,索引从左到右排序(第一个样本放在第一列中)

如果input是向量,out则是size的向量num_samples。

如果input是具有m行out的矩阵,则是形状矩阵 (m \ times \ text {num \ _samples})(米×num_samples) 。

如果替换为True,则抽取样本进行替换。

如果不是,它们将被替换而不会被绘制,这意味着当为一行绘制样本索引时,将无法为该行再次绘制它。

注意

如果绘制时没有替换,则num_samples必须少于中的非零元素input数(input如果是矩阵,则必须少于每行中的非零元素的最小数)。

参数:input(Tensor)—包含概率的张量

num_samples(int)—抽取的样本数

replacement(bool,optional)

out(Tensor,optional)

weights = torch.tensor([0, 10, 3, 0], dtype=torch.float) # create a tensor of weights
weights
>>>
tensor([ 0., 10.,  3.,  0.])

torch.multinomial(weights, 2)
>>>
tensor([1, 2])

torch.multinomial(weights, 4)
>>>
RuntimeError: invalid argument 2: invalid multinomial distribution (with replacement=False,
    not enough non-negative category to sample) at ../aten/src/TH/generic/THTensorRandom.cpp:320

torch.multinomial(weights, 4, replacement=True)
>>>
tensor([ 2,  1,  1,  1])

8.torch.cat(tensors, dim=0, out=None) → Tensor

在给seq定维度上连接给定张量序列。所有张量必须具有相同的形状(在连接维中除外)或为空。(Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty.) 官方帮助文档:TORCH.CAT

torch.cat()可以被看作是torch.split()torch.chunk()的逆运算。

参数:

  • tensors (sequence of Tensors)
    –同一类型的任何python张量序列。提供的非空张量必须具有相同的形状,但猫的尺寸除外。

  • dim(int,optional)–张量连接的尺寸

  • out(Tensor,可选)–输出张量

x = torch.randn(2, 3)
x
>>>
tensor([[ 0.3987,  0.7151, -0.1229],
        [ 0.9184, -0.6541,  0.4452]])
torch.cat((x, x, x), 0)   # 行拼接
>>>
tensor([[ 0.3987,  0.7151, -0.1229],
        [ 0.9184, -0.6541,  0.4452],
        [ 0.3987,  0.7151, -0.1229],
        [ 0.9184, -0.6541,  0.4452],
        [ 0.3987,  0.7151, -0.1229],
        [ 0.9184, -0.6541,  0.4452]])

torch.cat((x, x, x), 1) # 列拼接
>>>       
tensor([[ 0.3987,  0.7151, -0.1229,  0.3987,  0.7151, -0.1229,  0.3987,  0.7151,
         -0.1229],
        [ 0.9184, -0.6541,  0.4452,  0.9184, -0.6541,  0.4452,  0.9184, -0.6541,
          0.4452]])

torch.tensor.new_ones()返回一个与size大小相同的用1填充的张量

torch.tensor.new_ones(),返回一个与torch.tensor的size大小相同的用 1 填充的张量。

备注: 默认返回的Tensor具有与此张量相同的torch.dtype和torch.device

官方Docstring:

Docstring:
new_ones(size, dtype=None, device=None, requires_grad=False) -> Tensor

Returns a Tensor of size :attr:`size` filled with ``1``.
By default, the returned Tensor has the same :class:`torch.dtype` and
:class:`torch.device` as this tensor.

Args:
    size (int...): a list, tuple, or :class:`torch.Size` of integers defining the
        shape of the output tensor.
    dtype (:class:`torch.dtype`, optional): the desired type of returned tensor.
        Default: if None, same :class:`torch.dtype` as this tensor.
    device (:class:`torch.device`, optional): the desired device of returned tensor.
        Default: if None, same :class:`torch.device` as this tensor.
    requires_grad (bool, optional): If autograd should record operations on the
        returned tensor. Default: ``False``.

Example::

    >>> tensor = torch.tensor((), dtype=torch.int32)
    >>> tensor.new_ones((2, 3))
    tensor([[ 1,  1,  1],
            [ 1,  1,  1]], dtype=torch.int32)

典型范例:

# 直接根据数据创建tensor
x = torch.tensor([5.5, 3])
print(x)
>>>
tensor([5.5000, 3.0000])
# 通过现有的tensor来创建,此方法会默认重用输入Tensor的一些属性,例如数据类型,除非自定义数据类型。
x = x.new_ones(5,3, dtype=torch.float64)   # 返回的tensor默认具有相同的torc.dtype和torch.device
print(x)
>>>
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)

torch.rand_like生成相同维度的[0, 1)均匀分布的随机张量

torch.rand_like(x, dtype=torch.float),返回一个与torch.tensor的size大小相同的用满足[0, 1)均匀分布的随机数填充的张量。

备注: 默认返回的Tensor具有与此张量相同的torch.dtype和torch.device

官方Docstring:

Docstring:
rand_like(input, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) -> Tensor

Returns a tensor with the same size as :attr:`input` that is filled with
random numbers from a uniform distribution on the interval :math:`[0, 1)`.
``torch.rand_like(input)`` is equivalent to
``torch.rand(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)``.

Args:
    input (Tensor): the size of :attr:`input` will determine size of the output tensor.
    dtype (:class:`torch.dtype`, optional): the desired data type of returned Tensor.
        Default: if ``None``, defaults to the dtype of :attr:`input`.
    layout (:class:`torch.layout`, optional): the desired layout of returned tensor.
        Default: if ``None``, defaults to the layout of :attr:`input`.
    device (:class:`torch.device`, optional): the desired device of returned tensor.
        Default: if ``None``, defaults to the device of :attr:`input`.
    requires_grad (bool, optional): If autograd should record operations on the
        returned tensor. Default: ``False``.
    memory_format (:class:`torch.memory_format`, optional): the desired memory format of
        returned Tensor. Default: ``torch.preserve_format``.
Type:      builtin_function_or_method

典型范例:

# 直接根据数据创建tensor
x = torch.tensor([5.5, 3])
print(x)
>>>
tensor([5.5000, 3.0000])
# 通过现有的tensor来创建,此方法会默认重用输入Tensor的一些属性,例如数据类型,除非自定义数据类型。
x = x.new_ones(5,3, dtype=torch.float64)   # 返回的tensor默认具有相同的torc.dtype和torch.device
print(x)
>>>
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
x = torch.rand_like(x, dtype=torch.float)  # 指定新的数据类型
print(x)
>>>
tensor([[0.5185, 0.6977, 0.8000],
        [0.1610, 0.2823, 0.6816],
        [0.9152, 0.3971, 0.8742],
        [0.4194, 0.5529, 0.9527],
        [0.0362, 0.1852, 0.3734]])

错例分析:
x = torch.tensor(5,3, dtype=torch.float64) # 欲生成5*3的张量
print(x)
报错:
TypeError Traceback (most recent call last)
in
1 # 直接根据数据创建tensor
----> 2 x = torch.tensor(5,3, dtype=torch.float64)
3 print(x)
TypeError: tensor() takes 1 positional argument but 2 were given
原因分析:
tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False) -> Tensor
要求data的数据类型为:array_like,即:Can be a list, tuple,
NumPy ndarray, scalar, and other types
切记:torch.tensor()无法指定生成张量的shape!!!!!

猜你喜欢

转载自blog.csdn.net/weixin_42782150/article/details/106862236