深度学习02-pytorch-01-张量的创建
深度学习 pytorch 框架 是目前最热门的。
深度学习 pytorch 框架相当于 机器学习阶段的 numpy + sklearn
它将数据封装成张量(Tensor)来进行处理,其实就是数组。也就是numpy 里面的 ndarray .
pip install torch===1.10.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
import torch
import numpy as np
# 1.tensor:指定数据
# 数值
print(torch.tensor(100))
# 列表:只能是数值
# data =[[2,'int',4],[4,5,6]]
data =[[2,3,4],[4,5,6]]
print(torch.tensor(data))
# ndarray
data =np.random.randint(1,10,(2,3))
print(data)
print(torch.tensor(data))
# 2.Tensor
# # 数值
print(torch.Tensor([100]))
#
# # 列表:只能是数值
# # data =[[2,'int',4],[4,5,6]]
data =[[2,3,4],[4,5,6]]
print(torch.Tensor(data))
#
# # ndarray
data =np.random.randint(1,10,(2,3))
print(data)
print(torch.tensor(data))
#
# # 形状
print(torch.Tensor(4, 5))
# 3.IntTensor
print(torch.IntTensor(2, 3))
data =np.random.randint(1,10,(2,3))
print(torch.FloatTensor(data))
1. torch.Tensor(data) 默认类型是float 32,所以输出in t 会转成 float 32
import torch
# 线性
# arange:左闭右开
print(torch.arange(0, 10, 1))
# linspcae:左闭右闭
print(torch.linspace(0, 10, 21))
# 随机数
# 设置随机数种子
torch.random.manual_seed(22)
print(torch.randn((2, 3)))
print(torch.randint(1,10,(2,3)))
# 查看随机数种子
print(torch.random.initial_seed())
import torch
# 形状
print(torch.zeros((3, 3)))
print(torch.ones((3, 3)))
print(torch.full((3, 3),100))
# 指定张量数据
data = torch.randint(1,10,(3,4))
print(torch.zeros_like(data))
print(torch.ones_like(data))
print(torch.full_like(data,300))