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

预先准备和设置

#让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相关的基本操作如索引、切片、拼接、维度、计算等后续熟悉后补充

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落尘客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值