PyTorch-Torch01:tensor type and creation ops

TORCH

Torch包包含了多维度的张量操作和定义之外的一些数学操作,除此之外为有效的连接张量和其他数据类型提供了许多实用工具,和其他的实用工具。

import torch
#查看tensor默认数据类型
print(torch.tensor([1,1]).dtype)
print(torch.tensor([1.1,2]).dtype)
torch.int64
torch.float32
# 设置指定类型
torch.set_default_dtype(torch.float64)
torch.tensor([1.1,2]).dtype
torch.float64
#还可以通过下面形式设置
torch.set_default_tensor_type(torch.FloatTensor)
torch.get_default_dtype()
torch.float32

torch.numel计算张量元素总数

a = torch.randn(1,2,3,4,5)
torch.numel(a)
120

Creation tensor ops

随机样本创建操作是一个表,随机样本的创建有下面几种操作
torch.rand()
torch.rand_like()
torch.randn()
torch.randn_like() torch.randint() torch.randint_like() torch.randperm()
也可以使用torch.empty()使得样本值服从一个更广范围的分布

torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False) → Tensor

|-参数解释:
    data:数据可以是列表元组或者是Numpy数组,标量或者是其他类型
    dtype:数据类型如果None则数据类型来自于data
    device:设备设置CPU FOR CPU,CUDA FOR CUDA
    requires_grad:自动梯度计算设置返回一个梯度张量,默认为False
    pin_memory:锁业内存设置默认False具体理解看下面链接:
        http://www.voidcn.com/article/p-fsdktdik-bry.html
Warning:torch.tensor()总是在拷贝数据,如果想避免拷贝可以使用torch.Tensor.requires_grad_() or torch.Tensor.detach(),if 有一个Numpy数组需要避免拷贝可以使用torch.as_tensor()
import numpy as np
a = torch.tensor([[0.1,1.2],[1,2],[3,4]])
print(a)
c = np.random.randint(10,size=(3,4)) # Numpy随机数组
print(c)
#将Numpy随机数组转化成tensor
print(torch.tensor(c))
tensor([[0.1000, 1.2000],
        [1.0000, 2.0000],
        [3.0000, 4.0000]])
[[8 8 9 6]
 [7 2 2 3]
 [0 0 3 4]]
tensor([[8, 8, 9, 6],
        [7, 2, 2, 3],
        [0, 0, 3, 4]], dtype=torch.int32)

torch.eye(n,m=None,out=None)
返回一个对角线元素为一的2维张量

  • n:行数
  • m:列数
  • out:输出张量类型
print(torch.eye(3))
print(torch.eye(3,4))
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
tensor([[1., 0., 0., 0.],
        [0., 1., 0., 0.],
        [0., 0., 1., 0.]])

torch.from_numpy(ndarray)->tensor
将Numpy数组装换成张量形式且返回的张量tensor和numpy共享同一
内存空间修改一个会改变另一个

a = np.array([1,2,3,4])
t = torch.from_numpy(a)
print(a)
print(t)
t[0] = 0
print(t)
print(a)
[1 2 3 4]
tensor([1, 2, 3, 4], dtype=torch.int32)
tensor([0, 2, 3, 4], dtype=torch.int32)
[0 2 3 4]

torch.linspace(start,end,steps=100,out=None)->tensor
返回star和end间元素以等差数列排列个数为steps的一维张量其实就是均匀采样

torch.linspace(1,10,steps=5)
tensor([ 1.0000,  3.2500,  5.5000,  7.7500, 10.0000])
torch.linspace(-10,10,steps=5)
tensor([-10.,  -5.,   0.,   5.,  10.])

torch.logspace(start,end,steps=100,out=None)->tensor
和上面的线性采样类似,这个是在以10为底的对数函数上的均匀采样范围为10^{star}到10^{end},采样个数为steps

torch.logspace(start=-10,end=10,steps=5)
tensor([1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10])

torch.ones(*sizes,out=None)->Tensor
返回一个全为1的张量,shape由可变参数sizes定义

#下面三式等价sizes可以为序列
print(torch.ones(3,3))
print(torch.ones([3,3]))
print(torch.ones((3,3)))
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])

torch.rand(*size,out=None)->tensor
返回一个在[0,1]范围均匀分布的随机数

print(torch.rand((3,3)))
print(torch.rand(3))
tensor([[0.1052, 0.5031, 0.1692],
        [0.0292, 0.3243, 0.1054],
        [0.5000, 0.2522, 0.7380]])
tensor([0.0819, 0.5636, 0.6810])

torch.randn(*sizes,out=None)->Tensor

返回一个从标准正态分布中抽取出的随机张量

torch.randn([3,3])
tensor([[-0.0763, -1.2889, -1.7553],
        [ 2.5629,  0.5363, -0.4094],
        [-0.2738, -1.3176, -0.9627]])

torch.randperm(n,out=None)->LongTensor

输入参数n返回一个从0n-1的一个随机整数排列

torch.randperm(5)
tensor([4, 1, 0, 3, 2])

torch.arange(start,end,step=1,out=None)->tensor

返回一个1维张量,长度为floor((end−start)/step),floor代表向下取整。包含从start到end,以step为步长的一组序列值(默认步长为1)。

print(torch.arange(*[1,9]))
print(torch.arange(*[1,9],step=2))
tensor([1, 2, 3, 4, 5, 6, 7, 8])
tensor([1, 3, 5, 7])

torch.range(start,end,step=1,out=None)->Tensor

返回一个1维张量,长度为floor((end−start)/step)+1,其中floor代表向下取整数。从start开始,end为结尾,以step为步长的一组值。 step 是两个值之间的间隔,即 Xi+1=Xi+step

print(torch.range(1.0,9))
print(torch.range(1.0,9,step=2))
D:\Anaconda\lib\site-packages\ipykernel_launcher.py:1: UserWarning: torch.range is deprecated in favor of torch.arange and will be removed in 0.5. Note that arange generates values in [start; end), not [start; end].
  """Entry point for launching an IPython kernel.


tensor([1., 2., 3., 4., 5., 6., 7., 8., 9.])


D:\Anaconda\lib\site-packages\ipykernel_launcher.py:2: UserWarning: torch.range is deprecated in favor of torch.arange and will be removed in 0.5. Note that arange generates values in [start; end), not [start; end].

tensor([1., 3., 5., 7., 9.])

torch.zeros(*sizes,out=None)->Tensor

返回一个全零张量

torch.zeros(*[3,3])
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])

英文版官方文档

https://pytorch.org/docs/stable/torch.html#random-sampling

中文版官方文档

https://ptorch.com/docs/1/torchlists
这是某某大佬专门为Deep Learning搭建的学习平台

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容