首先学习Numpy的相关操作...

似乎当时没有写下来...有时间再补吧


Pytorch

import torch
import numpy as np

# 判断tensor
print(torch.is_tensor([[1, 2], [3, 4]]))

# 获得元素个数
a = torch.tensor([1, 2])
print(a.size())
print(torch.numel(a))

# build tensor
a = torch.empty(2, 2, 2)  # 未初始化
a = torch.zeros(2, 2, 2)
a = torch.ones(2, 2, 2, dtype=torch.long)  # 可以指定数据类型
a = torch.rand(2, 2, 2)  # 均匀分布
a = torch.randn(2, 2, 2)  # 标准正态分布
a = torch.randn_like(a, dtype=torch.double)  # 重载类型
a = torch.eye(2)
# 还可以创建正态分布的tensor

# reshape (share memory)
b = a.view(4)
c = a.view(2, -1)

# torch.tensor <-> numpy.array (share memory)
np_arr = a.numpy()
tc_tensor = torch.from_numpy(np_arr)

# 特定构造
a = torch.arange(1, 2, 0.2)  # 左闭右开以固定间隔选点(不常用,因为精度不够,请使用linspace)
a = torch.linspace(0, 1, 100)  # 在[0,1]均匀选择100个点
a = torch.logspace(-10, 10, 8)  # 在[10^start 10^end]上以对数刻度均匀选择steps个点

# 拼接&拆分
a = a.view(2, 4)
b = torch.cat((a, a, a), 0)  # 按行拼接
b = torch.cat((a, a, a), 1)  # 按列拼接
c = torch.split(b, (2, 4, 6), dim=1)  # 将dim=1拆成2,4,6大小的三部分

# 转置
a = a.t()

# 保存
torch.save(a, f='./t.pkl')
a = torch.load(f='./t.pkl')

# 运算
# +
# .add()
# .add_()
# torch.add(,,out=)

autograd

创建一个张量并设置requires_grad=True用来追踪其计算历史