1.torch.tensor的初始化和基本操作

torch.tensor的初始化和基本操作

  • 预先准备和设置
  • 1.tensor相关的函数和运算
    • 1.1创建数组
    • 1.2基于已存在的tensor去操作、创建、运算
    • 1.3 操作、取、查看、更改维度

预先准备和设置

#让jupyterx显示一个代码块的完整结果,而不是仅显示最后一行结果
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
#导入相应的包
import torch
import numpy as np
# 测试torch是否可用
torch.cuda.is_available()
True

1.tensor相关的函数和运算

1.1创建数组

np.array([1,2,3])#tensor
array([1, 2, 3])
#torch与numpy数组比较
torch.tensor([1,2,3])#ndarrays
tensor([1, 2, 3])
torch.empty(5,3)
tensor([[5.1492e+31, 2.2234e-10, 1.7033e+25],
        [1.5766e-19, 1.7753e+28, 1.3458e-14],
        [1.4585e-19, 3.7293e-08, 1.3472e+37],
        [3.1360e+27, 1.6636e+22, 3.0555e-18],
        [2.0863e+37, 4.7851e+22, 2.8826e+32]])
torch.zeros(5,3,dtype = torch.long)#dtype表示数据格式
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])
torch.randn(5,3)#随机初始化的矩阵,默认浮点型数据
tensor([[ 0.3436, -0.0894,  1.6433],
        [-1.5638,  0.4153, -1.1772],
        [-0.5727, -0.5392, -0.3253],
        [ 0.6257,  0.6096,  0.5668],
        [-0.3543, -0.5637, -0.8136]])

1.2基于已存在的tensor去操作、创建、运算

x = torch.tensor([1,2,3])
x.new_ones(5,3)
tensor([[1, 1, 1],
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]])
torch.rand_like(x,dtype = torch.float)
torch.zeros_like(x)
tensor([0.4059, 0.9531, 0.1069])
tensor([0, 0, 0])
# 运算 加减乘除
x = torch.randn(5,3)
y = torch.randn(5,3)
x+y
torch.add(x,y)
tensor([[-0.1848, -1.3045, -0.3650],
        [ 1.7452,  0.4371, -0.6667],
        [ 0.1065,  0.0808,  0.6039],
        [ 1.5757,  1.2262, -0.3884],
        [ 0.3145,  1.2459, -2.6667]])
tensor([[-0.1848, -1.3045, -0.3650],
        [ 1.7452,  0.4371, -0.6667],
        [ 0.1065,  0.0808,  0.6039],
        [ 1.5757,  1.2262, -0.3884],
        [ 0.3145,  1.2459, -2.6667]])
z = torch.empty(5,3)
torch.add(x,y,out = z)
z
tensor([[-0.1848, -1.3045, -0.3650],
        [ 1.7452,  0.4371, -0.6667],
        [ 0.1065,  0.0808,  0.6039],
        [ 1.5757,  1.2262, -0.3884],
        [ 0.3145,  1.2459, -2.6667]])
tensor([[-0.1848, -1.3045, -0.3650],
        [ 1.7452,  0.4371, -0.6667],
        [ 0.1065,  0.0808,  0.6039],
        [ 1.5757,  1.2262, -0.3884],
        [ 0.3145,  1.2459, -2.6667]])
y.add_(x)
y
tensor([[-0.1848, -1.3045, -0.3650],
        [ 1.7452,  0.4371, -0.6667],
        [ 0.1065,  0.0808,  0.6039],
        [ 1.5757,  1.2262, -0.3884],
        [ 0.3145,  1.2459, -2.6667]])
tensor([[-0.1848, -1.3045, -0.3650],
        [ 1.7452,  0.4371, -0.6667],
        [ 0.1065,  0.0808,  0.6039],
        [ 1.5757,  1.2262, -0.3884],
        [ 0.3145,  1.2459, -2.6667]])

1.3 操作、取、查看、更改维度

#操作、取
x
x[:,1:] #:表示不指定行,取所有
x[0,1].item()
tensor([[ 0.1691, -0.8111, -0.5507],
        [ 0.6116,  0.7782, -0.6893],
        [-0.3884, -0.7625,  0.3701],
        [ 0.7274,  0.0859, -0.8919],
        [ 0.7042,  0.2210, -2.3020]])
tensor([[-0.8111, -0.5507],
        [ 0.7782, -0.6893],
        [-0.7625,  0.3701],
        [ 0.0859, -0.8919],
        [ 0.2210, -2.3020]])
-0.8111448287963867
#查看维度
np.array([1,2,3]).shape # 无np.array([1,2,3]).size()
x.shape
x.size()
x.size()[1]
(3,)
torch.Size([5, 3])
torch.Size([5, 3])
3
# 更改维度,下面两者都不会更改原来的数组形状,相当于换了一个视角,除非重新赋值
z
z.reshape(1,15)
z.view(-1,5) #-1表示自动计算维度
z
tensor([[-0.1848, -1.3045, -0.3650],
        [ 1.7452,  0.4371, -0.6667],
        [ 0.1065,  0.0808,  0.6039],
        [ 1.5757,  1.2262, -0.3884],
        [ 0.3145,  1.2459, -2.6667]])
tensor([[-0.1848, -1.3045, -0.3650,  1.7452,  0.4371, -0.6667,  0.1065,  0.0808,
          0.6039,  1.5757,  1.2262, -0.3884,  0.3145,  1.2459, -2.6667]])
tensor([[-0.1848, -1.3045, -0.3650,  1.7452,  0.4371],
        [-0.6667,  0.1065,  0.0808,  0.6039,  1.5757],
        [ 1.2262, -0.3884,  0.3145,  1.2459, -2.6667]])
tensor([[-0.1848, -1.3045, -0.3650],
        [ 1.7452,  0.4371, -0.6667],
        [ 0.1065,  0.0808,  0.6039],
        [ 1.5757,  1.2262, -0.3884],
        [ 0.3145,  1.2459, -2.6667]])

其余与tensor相关的基本操作如索引、切片、拼接、维度、计算等后续熟悉后补充

你可能感兴趣的:(深度学习入门,numpy,python,机器学习,pytorch)