骁勇善战的将军是在不断地的战斗中积累经验变强,好的学习方法是在实战中积累经验成长!
话不多少,上代码:
Tensor(张量)
import torch
import numpy as np
# help() 查看 detach() 的用法,用这个函数输出loss的结果 loss.detach().numpy()
#print(help(torch.Tensor().detach()))
# 下面开始学习 tensor 的基本用法
# torch -> numpy
# Tensor -> array 这样最容易理解
# numpy的 array 利用 CPU 加速,torch 的 Tensor 利用 GPU 加速 ,也可以把 torch 理解为 numpy 的GPU 替代品
x_torch = torch.Tensor([[1,2,3],[4,5,6],[7,8,9]])
x_numpy = np.array([[9,8,7],[6,5,4],[3,2,1]])
print(x_torch)
print( x_numpy)
结果为:
tensor([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.]])
[[9 8 7]
[6 5 4]
[3 2 1]]
# 构建一个 5X3 的矩阵,未初始化的
x = torch.Tensor((5,3))
print(x)
结果为:
tensor([ 5., 3.])
# 构建一个随机初始化的矩阵
x = torch.rand((5,3))
print(x)
结果为:
tensor([[ 0.7100, 0.3320, 0.5254],
[ 0.9006, 0.2045, 0.6394],
[ 0.8076, 0.9547, 0.8414],
[ 0.4217, 0.5714, 0.5655],
[ 0.3591, 0.0914, 0.6318]])
# 获取 x 的属性 size
print(x.size())
结果为:
torch.Size([5, 3])
# 对 tensor 类型的 操作
# 加法: 方法一
y = torch.rand(5,3)
print(x+y)
结果为:
tensor([[ 1.0701, 0.5820, 1.3096],
[ 1.8020, 0.7354, 1.2867],
[ 1.0784, 1.2207, 1.4266],
[ 1.0801, 1.2514, 0.8699],
[ 0.9364, 0.5626, 0.7570]])
# 加法: 方法二
print(torch.add(x,y))
结果为:
tensor([[ 1.0701, 0.5820, 1.3096],
[ 1.8020, 0.7354, 1.2867],
[ 1.0784, 1.2207, 1.4266],
[ 1.0801, 1.2514, 0.8699],
[ 0.9364, 0.5626, 0.7570]])
# 验证这两种方式的等效性
print((x+y)==(torch.add(x,y))) # 返回的结果为 5X3 的全幺矩阵,即对应位置的元素是相等的
结果为:
tensor([[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1]], dtype=torch.uint8)
# 亦可以将 x+y 的结果输出到一个指定的 trnsor 中
result = torch.Tensor((5,3)) # 此处的 result 为一个 未初始化的 5X3 的Tensor ,大小要与 x+y 的结果一致
torch.add(x,y,out=result)
print(result)
print((x+y) == (result)) # 验证是否等效,结果为 5X3 的全幺矩阵
结果为:
tensor([[ 1.0701, 0.5820, 1.3096],
[ 1.8020, 0.7354, 1.2867],
[ 1.0784, 1.2207, 1.4266],
[ 1.0801, 1.2514, 0.8699],
[ 0.9364, 0.5626, 0.7570]])
# in-place(就地操作)
# add x to y
y.add_(x)
print(y) # 此处 y 的值被改变,其实 y.add_(x) 等价于 y +=x 即 y = y + x
print(result == y) # 验证等价性
结果为:
tensor([[ 1.0701, 0.5820, 1.3096],
[ 1.8020, 0.7354, 1.2867],
[ 1.0784, 1.2207, 1.4266],
[ 1.0801, 1.2514, 0.8699],
[ 0.9364, 0.5626, 0.7570]])
tensor([[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1]], dtype=torch.uint8)
# Note: 任何改变张量的操作操作方法都是以后缀 _ 结尾的,例如: x.copy_(y),x.t_(y), 将改变张量 x
# numpy 中的索引,切片操作 在 Tensor 中一样适用
print(x[:,1]) # 输出 x 的第一列
结果为:
tensor([ 0.3320, 0.2045, 0.9547, 0.5714, 0.0914])
# 利用 torch,view() 改变 Tensor 的大小
x = torch.rand((4,4))
y = x.view(16)
z = x.view((-1,8)) # 和 numpy 中的用法类似 ,自动计算 行数,结果为 2X8 的tensor
print(x.size(), y.size(), z.size())
结果为:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
# numpy-Bridge (即 numpy 桥),用于实现 numpy 与 tensor 的相互转换
x_torch = torch.Tensor([[1,2,3],[4,5,6],[7,8,9]])
x_numpy = np.array([[1,2,3],[4,5,6],[7,8,9]])
x_numpy_to_torch = torch.from_numpy(x_numpy)
x_torch_to_numpy = x_torch.numpy()
print(x_numpy)
print(x_torch_to_numpy)
print("----------------我是可爱的分界线-----------------")
print(x_torch)
print(x_numpy_to_torch)
print("----------------我是可爱的分界线-----------------")
# 这里不能直接比较转换后的 数据 因为数据类型不同,会报错
# numpy的 array默认的数据类型为 numpy.int32, 而 torch 的 Tensor 的默认输出数据类型为 torch.float32,所以要进行数据类型转换
# numpy 中 用 astype() , torch 中用 type()
x_numpy_to_torch = torch.from_numpy(x_numpy).type(torch.float32)
x_torch_to_numpy = x_torch.numpy().astype(np.int32)
print(x_torch == x_numpy_to_torch)
print(x_numpy == x_torch_to_numpy)
结果为:
[[1 2 3]
[4 5 6]
[7 8 9]]
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]
----------------我是可爱的分界线-----------------
tensor([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.]])
tensor([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]], dtype=torch.int32)
----------------我是可爱的分界线-----------------
tensor([[ 1, 1, 1],
[ 1, 1, 1],
[ 1, 1, 1]], dtype=torch.uint8)
[[ True True True]
[ True True True]
[ True True True]]
# Note: 除了 Char Tensor 外,CPU 上的所有的 Tensor 都支持与 numpy 相互转换
# cuda Tensor
# 可以使用 .cuda 方法将 Tensors 在 GPU 上 加速
print(torch.cuda.is_available()) # 结果为 True ,说明 cuda 是正确安装的且能用的
if torch.cuda.is_available():
x_torch = x_torch.cuda()
x_numpy_to_torch = x_numpy_to_torch.cuda()
result = x_torch + x_numpy_to_torch
print(result)
结果为:
True
tensor([[ 2., 4., 6.],
[ 8., 10., 12.],
[ 14., 16., 18.]], device='cuda:0')
Reference: