Pytorch——Introduction

Pytorch——Introduction,第1张

Introduction to PyTorch

文章目录
  • Introduction to PyTorch
    • What are Tensors?
    • Loading and normalizing datasets

What are Tensors?
import torch
import numpy as np

# 初始化一个tensor
# data = [[1, 2, 3], [4, 5, 6]]
# print(data)
# x_data = torch.tensor(data) #使用torch.tensor(list)进行初始化为张量tensor
# print(x_data)


# 从numpy转化为tensor
# np_array = np.array(data)
# print(np_array)
# x_np = torch.from_numpy(np_array) #可以直接将numpy型数组初始化为tensor,使用torch.from_numpy()
# print(x_np)


# 传入现有tensor,传出新的tensor
# x_ones = torch.ones_like(x_data) #使用torch.ones_like(tensor)来创建一个所有元素都为1的tensor
# print(x_ones)
# x_rand = torch.rand_like(x_data, dtype = torch.float)#使用torch.rand_like(tensor,dtype=torch.float)创建所有元素为随机的tensor
# print(x_rand)


# 上述为传入tensor来创建相应tensor,下面为直接通过shape创建相应tensor
# shape = (2, 3)
# rand_tensor = torch.rand(shape)
# ones_tensor = torch.ones(shape)
# zeros_tensor = torch.zeros(shape)
# print(f"Random Tensor:\n{rand_tensor}\n")
# print(f"Ones Tensor:\n {ones_tensor} \n")
# print(f"Zeros Tensor:\n{zeros_tensor} \n")


# Tensor的属性
# tensor = torch.rand(3, 4)
# print(tensor.shape)
# print(tensor.dtype)
# print(tensor.device)


# Tensor的 *** 作
'''
有超过100个张量 *** 作,包括算术,线性代数,矩阵 *** 作(转置,索引,切片)。为了抽样和审查这里的全面描述。
这些 *** 作都可以在GPU上运行(速度通常比CPU快)。
cpu最多16核。核心是进行实际计算的单位。每个核心按顺序处理任务(一次处理一个任务)。
GPU有1000个核心。GPU核心以并行处理的方式处理计算。任务被划分并在不同的核心中处理。这使得gpu在大多数情况下都比cpu快。gpu处理大数据比处理小数据更好。GPU通常用于图形或神经网络的高强度计算(我们将在稍后的神经网络单元中看到更多)。
PyTorch可以使用Nvidia CUDA库来利用

默认情况下,CPU上创建了张量。
张量也可以计算到GPU上——要做到这一点,你需要使用.to方法移动它们(在检查GPU可用性之后)。
请记住,跨设备复制大张量在时间和内存方面是非常昂贵的!
'''
# if torch.cuda.is_available():
#     tensor = tensor.to('cuda')
# print(torch.cuda.is_available())


# pytorch为类numpy的索引和切分
# tensor = torch.ones(4, 4)
# print('First row: ', tensor[0])
# print('First column: ', tensor[:, 0])
# print('Last column:', tensor[..., -1])
# tensor[:, 1] = 0
# print(tensor)


# 连接运算
'''
你可以用torch.cat()。将一个张量序列沿给定的维级联起来。torch.stack()是另一个张量连接选项,它与torch.cat略有不同
cat 对应 concatenate
'''
# tensor = torch.rand(5, 3)
# print(tensor)
# t1 = torch.cat([tensor,tensor,tensor], dim=1)#dim=1为横向
# t2 = torch.cat([tensor, tensor, tensor]) #默认dim为0,纵向
# t3 = torch.stack([tensor, tensor, tensor]) #比cat多一个维度
# print(t1)
# print(t2)
# print(t3)


# 算数运算
# 矩阵乘法(数学)
tensor = torch.tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]])
# y1 = tensor @ tensor.T #tensor.T与tensor.t()一致,都是取tensor的转置
# y2 = tensor.matmul(tensor.T)
# y3 = torch.rand_like(tensor)
# torch.matmul(tensor, tensor.T, out = y3)
# print(y1)
# print(y2)
# print(y3)#y1 y2 y3结果一致
# 矩阵点乘
# z1 = tensor * tensor
# z2 = tensor.mul(tensor)
# z3 = torch.rand_like(tensor)
# torch.mul(tensor, tensor, out = z3)
# print(z1)
# print(z2)
# print(z3)


#单元素张量
'''
如果你有一个单元素张量,例如,通过将一个张量的所有值聚合为一个值,你可以使用item()将其转换为一个Python数值:
'''
# n维数组,需要n次sum()方可变成单个数值
# agg = tensor.sum()
# print(agg)
# agg_item = agg.item()
# print(agg_item, type(agg_item))

#原地 *** 作
'''注意:就地 *** 作可以节省一些内存,但在计算导数时可能会出现问题,因为会立即丢失历史记录。因此,不鼓励使用它们。
'''
# print(tensor, '\n')
# tensor.add_(5)
# print(tensor)


# # 与NumPy桥接 torch与ndarray共享内存空间
# t = torch.ones(5)
# print(f"t: {t}")
# n = t.numpy()
# print(f"n: {n}")
# # t2 = n.torch()    # 报错
# # print(f"t2: {t2}")# 报错 numpy没有torch对象   torch有numpy对象
# # 张量的变化反映在NumPy数组中。
# t.add_(1)
# print(f"t: {t}")
# print(f"t: {n}")
#numpy的ndarray转化为tensor
# n = np.ones(5)
# t = torch.from_numpy(n)     # np = tensor.numpy(ndarray)   tensor = torch.from_numpy()


Loading and normalizing datasets

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/886027.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-14
下一篇 2022-05-14

发表评论

登录后才能评论

评论列表(0条)

保存