资讯详情

资讯详情

【深度学习】Day1

张量的创建张量张量TensorPyTorch 张量是一种用于存储多维数组的数据结构类似于 NumPy 的多维数组是深度学习里最基础的数据载体张量按维度分为下面几类张量基本创建方式torch.tensor()根据给定数据创建张量小写tensor1传入列表 / 数组自动推断数据类型# 用列表创建张量 t1torch.tensor([1,2,3]) print(t1) # tensor([1, 2, 3])2特点拷贝数据新建张量日常开发首选torch.Tensor()大写Tensor有两种用法1传入数字形状创建指定 shape 的张量内容未初始化随机垃圾值t2torch.Tensor(2,3) # 创建2行3列张量 print(t2)2传入列表数据也可以创建张量不推荐容易混淆t3torch.Tensor([1,2,3]) print(t3)3注意大写torch.Tensor默认是float32类型小写torch.tensor会根据输入自动判断类型torch.IntTensor / torch.FloatTensor / torch.DoubleTensor1创建指定数据类型的张量旧 API 写法新版本更推荐在torch.tensor里用dtype指定类型2# 整型张量 t_inttorch.IntTensor([1,2,3]) # 浮点型张量 float32 t_floattorch.FloatTensor([1.1,2.2]) # 双精度浮点 float64 t_doubletorch.DoubleTensor([1.1,2.2]) print(t_int) print(t_float) print(t_double)更推荐写法t torch.tensor([1, 2, 3], dtypetorch.int32) print(t)重点区分torch.tensor ()torch.tensor()根据传入的数据创建张量可以传入数字、numpy 数组、Python 列表会自动继承输入的数据类型# 1 创建张量标量0维张量 datatorch.tensor(10) print(data) data1np.random.randn(2,3) # numpy 默认 float64 data2torch.tensor(data1) print(data2) # 3. python 创建列表 data3[[1,2,3],[4,5,6]] data4torch.tensor(data3) print(data4)关键点1torch.tensor(10)传入单个数字生成0 维标量张量输出tensor(10)不是数组形式2numpy 数组转张量anumpy 的浮点数默认是float64所以生成的张量dtypetorch.float64btorch.tensor()会拷贝一份数据新张量和原 numpy 数组互不影响3Python 列表创建张量a列表里写带小数点数字10.代表浮点数PyTorch 默认转成float32b两层列表[[...],[...]]→2 维张量矩阵torch.Tensor ()两种写法1传数字形状参数创建指定形状的张量不初始化里面是内存里残留的随机垃圾值2传列表创建带指定数据的张量默认数据类型float32# 1. 创建2行3列的张量只指定形状里面的值没初始化的随机值 datatorch.Tensor(2,3) print(data) # 2. 传入列表按照列表数据创建张量 data1torch.Tensor([10,20,30]) print(data1) data2torch.Tensor([10]) print(data2)核心对比torch.IntTensor/ FloatTensor / DoubleTensor传数字(2,3)创建指定形状、对应数据类型的未初始化张量内存残留垃圾值传列表数据自动把里面元素强制转换成对应类型# 1. 创建2行3列dtypeint32 的张量,没初始化里面的是随机脏整数 datatorch.IntTensor(2,3) print(data) # 2. 传入浮点列表自动强制转成int32,小数直接砍掉不是四舍五入是截断 data1torch.IntTensor([2.5,3.3]) print(data1) # 3. 其他旧版类型 data2torch.ShortTensor() # int 16 data3torch.LongTensor() # int 64 data4torch.FloatTensor() # int 32 data5torch.DoubleTensor() # int 64新版推荐写法1torch.tensor() 指定 dtype 参数可读性更好datatorch.tensor([2.5,3.3],dtypetorch.int32) print(data)创建线性 随机张量线性张量torch.arange(start, end, step)1区间[start, end)左闭右开按步长生成datatorch.arange(0,10,2) print(data) # tensor([0, 2, 4, 6, 8])torch.linspace(start, end, steps)1区间[start, end]首尾都包含按元素总个数均分data torch.linspace(0,9,10) print(data) # tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])随机张量相关torch.manual_seed(seed_num)1设置随机种子固定种子后后续随机数每次运行结果完全一样用于实验复现2torch.initial_seed()查看当前随机种子torch.randn(*size)1生成标准正态分布均值 0方差 1的浮点张量torch.manual_seed(100) datatorch.randn(2,3) # 2 行3 列 print(data) # tensor([[ 0.3607, -0.2859, -0.3938], # [ 0.2429, -1.3833, -2.3134]])torch.randint(low, high, size())1生成随机整数张量区间[low, high)datatorch.randint(0,10,size(2,3)) # 2 行3列 print(data) # tensor([[2, 0, 4], # [2, 3, 8]])使用torch.arange() 和 torch.linspace()创建线性张量def demo1(): # 1.使用torch.arange()创建一个从0到9的线性张量 [0,10) t1torch.arange(0,10) print(ft1:{t1},shape:{t1.shape}, dtype: {t1.dtype}) # 2.使用torch.linspace()创建一个从0到9的线性张量一共10个点 t2 torch.linspace(0,9,10) print(ft2: {t2}, shape:{t2.shape}, dtype:{t2.dtype})torch.Size() 就是专门用来描述张量形状的类型和列表很像1torch.Size([10])→ 1 维长度 102torch.Size([3,5])→ 2 维3 行 5 列3 个行每行 5 个元素3torch.Size([2,3,4])→ 3 维创建随机张量def demo02(): # 0.设置随机种子 # torch.random.manual_seed(28) torch.manual_seed(28) # 1.使用torch.rand创建一个随机张量shape(3,5)[0,1)均匀分布 t1 torch.rand(3,5) print(ft1:{t1}, shape: {t1.shape}, dtype: {t1.dtype}) # 2.使用torch.randn创建一个随机张量(3,5)标准正态分布 t2 torch.randn(3,5) print(ft2:{t2}, shape: {t2.shape}, dtype: {t2.dtype}) # 3.使用torch.randint创建一个随机整数张量(3,5)区间[0,10) t3 torch.randint(0,10,(3,5)) print(ft3:{t3}, shape: {t3.shape}, dtype: {t3.dtype}) # 4.打印当前随机种子 print(f当前随机种子:{torch.initial_seed()})总结1torch.arange(0,10)整数序列左闭右开默认 int642torch.linspace(0,9,10)均分首尾都包含默认 float323torch.rand(3,5)0~1 均匀分布只有正数4torch.randn(3,5)标准正态分布有正有负5torch.randint(0,10,(3,5))整数[low, high)6torch.manual_seed(28)固定随机种子保证每次运行随机数不变全 0 / 全 1 / 指定值张量后缀带 _like 的函数参考另一个张量的 shape、dtype、设备来新建张量torch.zeros() / torch.zeros_like() —— 全 0 张量# zeros(shape)直接指定形状 datatorch.zeros(2,3) print(data) # tensor([[0., 0., 0.], # [0., 0., 0.]]) # zeros_like(已有张量)复用data的shape data1torch.zeros_like(data) print(data1) # tensor([[0., 0., 0.], # [0., 0., 0.]])torch.ones()/torch.ones_like()—— 全 1 张量# ones(shape) datatorch.ones(2,3) print(data) # tensor([[1., 1., 1.], # [1., 1., 1.]]) # ones_like data1torch.ones_like(data) print(data1) # tensor([[1., 1., 1.], # [1., 1., 1.]])torch.full()/torch.full_like()—— 填充指定数值# full(shape, fill_value) 注意shape要写成列表 [2,3] datatorch.full([2,3],10) print(data) # tensor([[10, 10, 10], # [10, 10, 10]]) data1torch.full_like(data,20) print(data1) # tensor([[20, 20, 20], # [20, 20, 20]])代码torch.zeros 和 torch.zeros_like 创建全0张量torch.ones 和 torch.ones_like 创建全1张量torch.full 和 torch.full_like 创建全为指定值张量用处 网络参数初始化偏置参数初始化创建全0张量def demo01(): # 1.使用torch.zeros创建全0张量(3,4) t1 torch.zeros(3,4) print(ft1:{t1}, shape: {t1.shape}, dtype: {t1.dtype}) # 2.创建一个随机张量(3,5) t2 torch.randn(3,5) print(ft2:{t2}, shape: {t2.shape}, dtype: {t2.dtype}) # 3.使用torch.zeros_like创建相同形状的全0张量 t3 torch.zeros_like(t2) # (3,5) print(ft3:{t3}, shape: {t3.shape}, dtype: {t3.dtype})创建全1张量def demo02(): # 1.使用torch.ones创建全1张量(3,4) t1 torch.ones(3,4) print(ft1:{t1}, shape: {t1.shape}, dtype: {t1.dtype}) # 2.创建一个随机张量(3,5) t2 torch.randn(3,5) print(ft2:{t2}, shape: {t2.shape}, dtype: {t2.dtype}) # 3.使用torch.ones_like创建相同形状的全1张量 t3 torch.ones_like(t2) # (3,5) print(ft3:{t3}, shape: {t3.shape}, dtype: {t3.dtype})创建指定值张量def demo03(): # 1.使用torch.full创建指定值张量(3,4) t1 torch.full((3,4),10) print(ft1:{t1}, shape: {t1.shape}, dtype: {t1.dtype}) # 2.创建一个随机张量(3,5) t2 torch.randn(3,5) print(ft2:{t2}, shape: {t2.shape}, dtype: {t2.dtype}) # 3.使用torch.full_like创建相同形状的指定值张量 t3 torch.full_like(t2,10) # (3,5) print(ft3:{t3}, shape: {t3.shape}, dtype: {t3.dtype})
觉得有用,分享给同行:

为您的企业打造数字门面

稳重轻奢商务风格,端正雅致视觉,长效耐看不易过时。

立即咨询 →