当前位置: 首页 > news >正文

工业设计的网站广元网站建设价格

工业设计的网站,广元网站建设价格,企业门户网站源码,简单的网页设计主题1. 张量 (Tensor): 数学中指的是多维数组#xff1b; torch.Tensor data: 被封装的 Tensor dtype: 张量的数据类型 shape: 张量的形状 device: 张量所在的设备#xff0c;GPU/CPU requires_grad: 指示是否需要计算梯度 grad: data 的梯度 grad_fn: 创建 Tensor 的 Functio…1. 张量 (Tensor): 数学中指的是多维数组 torch.Tensor data: 被封装的 Tensor dtype: 张量的数据类型 shape: 张量的形状 device: 张量所在的设备GPU/CPU requires_grad: 指示是否需要计算梯度 grad: data 的梯度 grad_fn: 创建 Tensor 的 Function是自动求导的关键 is_leaf: 指示是否是叶子结点 (叶子结点指的是用户创建的节点比如 y(xw)*(w1)中x和w就是叶子结点【可以用计算图来清楚地表示该过程】)2. 创建张量: 2.1 直接创建: 2.1.1 从 data 创建 tensor data: 可以是 list, numpy dtype: 数据类型默认与data一致 device: 所在设备 requires_grad: 是否需要梯度 pin_memory: 是否存于锁页内存# 创建方法 torch.tensor(data,dtypeNone,deviceNone,requires_gradFalse,pin_memoryFalse )2.1.2 从 numpy 创建 tensor 用torch.from_numpy()创建的tensor与原始的ndarray共享内存修改其中一个另一个也会变。 # 创建方法 torch.from_numpy(ndarray)# 举例 import torch import numpy as np# 直接创建 ## torch.tensor() arr np.ones((3,3)) t torch.tensor(arr, dtypetorch.float32, devicemps) # 把张量放到 GPU 上 (mac M1)print(t.dtype) print(t)## torch.from_numpy() arr np.array([[1,2,3],[4,5,6]]) t torch.from_numpy(arr) print(t.dtype) print(t)torch.float32 tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]], devicemps:0) torch.int64 tensor([[1, 2, 3],[4, 5, 6]])2.2 根据数值进行创建: 2.2.1 torch.zeros() 创建全0张量 size: 张量的形状 out: 输出的张量 (暂时可以不考虑) layout:内存中布局形式有 strided (通常情况下使用), sparse_coo (读取稀疏矩阵会用到) device: 所在设备 requires_grad: 是否需要梯度# 创建方法 torch.zeros(*size,outNone,dtypeNone,layouttorch.strided,deviceFalse,requires_gradFalse )2.2.2 torch.zeros_lisk() 根据 input 形状创建全0张量 input: 创建与 input 同形状的张量 dtype: 数据类型 layout: 内存中的布局形式# 创建方法 torch.zeros_like(input,dtypeNone,layoutNone,deviceNone,requires_gradFalse )2.2.3 torch.ones() 和 torch.ones_like() 创建全1张量 2.2.4 torch.full()和torch.full_like() 创建自定义数值的张量 size: 张量形状 fill_value: 张量的值2.2.5 torch.arange() 根据数列创建等差1维张量[start, end) start: 起始值 end: 结束值 step: 数列公差默认为12.2.6torch.linspace() 创建均分的1维张量[start, end] start: 起始值 end: 结束值 step: 数列长度2.2.7torch.logspace() 创建对数均分的1D张量 start: 起始值 end: 结束值 steps: 数列长度 base: 对数函数的底默认为10# 创建方法 torch.logspace(start,end,steps100,base10.0,outNone,dtypeNone,layouttorch.strided,deivceNone,requires_gradFalse )2.2.8 torch.eye() 创建单位对角矩阵 (2D张量) n: 矩阵行数 m: 矩阵列数# 创建方法 torch.eye(n,mNone,outNone,dtypeNone,layouttorch.strided,deviceNone,requires_gradFalse )# 举例 t torch.zeros((3,3)) print(t.dtype) print(t)t torch.full((3,3),2) # 创建3x3的全2张量 print(t)t torch.arange(2, 8, 2) print(t)t torch.linspace(2, 10, 5) print(t)torch.float32 tensor([[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]]) tensor([[2, 2, 2],[2, 2, 2],[2, 2, 2]]) tensor([2, 4, 6]) tensor([ 2., 4., 6., 8., 10.])2.3 根据概率分布创建张量: 2.3.1 torch.normal() 生成正态分布高斯分布 四种模式 mean标量std标量mean标量std张量mean张量std标量mean张量std张量 # 张量 torch.normal(mean,std,outNone )# 标量 torch.normal(mean,std,size,outNone )2.3.2 torch.randn(), torch.randn_like() 生成标准正态分布 torch.randn(*size, ## 张量形状outNone,dtypeNone,layouttorch.strided,deviceNone,requires_gradFalse )2.3.3 torch.rand(), torch.rand_like() 在[0,1)区间上生成均匀分布 torch.rand(*size, ## 张量形状outNone,dtypeNone,layouttorch.strided,deviceNone,requires_gradFalse )2.3.4 torch.randint(), torch.randint_like() 在 [low, high) 区间上生成整数均匀分布 torch.randint(low,high,size,outNone,dtypeNone,layouttorch.strided,deviceNone,requires_gradFalse )2.3.5 torch.randperm() 生成0到n-1的随机排列 n: 张量长度torch.randperm(n,outNone,dtypetorch.int64,layouttorch.strided,deviceNone,requires_gradFalse ) 2.3.6torch.bernoulli() 以 input 为概率生成伯努利分布(0-1分布两点分布) torch.bernoulli(input,*,generatorNone,outNone )3. 张量的操作 张量的操作拼接、切分、索引和变换 1. 拼接 1.1 torch.cat() 将张量按维度 dim 进行拼接 (不回扩张张量维度) torch.cat(tensors, ## 张量序列dim, ## 要拼接的维度outNone )1.2 torch.stack() 在新创建的维度 dim 上进行拼接 (会扩张张量的维度) torch.stack(tensors, ## 张量序列dim0, ## 要拼接的维度outNone )import torcht torch.ones((2,3))# torch.cat() t1 torch.cat([t,t], dim0) ## 按照 dim0 对t进行拼接得到(4,3)# torch.stack() t2 torch.stack([t,t], dim0) ## 按照 dim0 对t进行拼接得到(2,2,3)会在dim0创建一个新维度print(t1 shape: , t1.shape) print(t2 shape: , t2.shape)t1 shape: torch.Size([4, 3]) t2 shape: torch.Size([2, 2, 3])2. 切分 2.1 torch.chunk() 将张量按照维度 dim 进行平均切分返回张量列表 (如果不能整除最后一份张量小于其他张量) torch.chunk(input, ## 要切分的张量chunks, ## 要切分的份数dim0 ## 要切分的维度 )2.2 torch.split() 将张量按照维度 dim 进行切分返回张量列表 torch.split(tensor, ## 要切分的张量split_size_of_sections, ## 为 int 时表示每一份的长度为 list 时按 list 元素切分dim0 ## 要切分的维度 )t1 torch.ones((2,5))# torch.chunk() list_of_tensors torch.chunk(t1, dim1, chunks2) ## 5不能被2整除所以最后一个张量形状小雨前面的张量 for idx, t in enumerate(list_of_tensors):print(order {}, shape is {}.format(idx1, t.shape))# torch.split() list_of_tensors torch.split(t1, [2,1,2], dim1) for idx, t in enumerate(list_of_tensors):print(order {}, shape is {}.format(idx1, t.shape))order 1, shape is torch.Size([2, 3]) order 2, shape is torch.Size([2, 2]) order 1, shape is torch.Size([2, 2]) order 2, shape is torch.Size([2, 1]) order 3, shape is torch.Size([2, 2])3. 索引 3.1 torch.index_select() 在维度 dim 上按照 index 索引数据返回依index索引数据拼接的张量 (先索引再拼接) torch.index_select(input, ## 要索引的张量dim, ## 要索引的维度index, ## 要索引数据的序号数据类型必须是 torch.longoutNone )3.2 torch.masked_select() 按照 mask 中的 True 进行索引返回一维张量 torch.masked_select(input,mask, ## 与 input 同形状的布尔类型张量outNone )# torch_index_select() t torch.randint(0,9,size(3,3)) idx torch.tensor([0,2],dtypetorch.long) t_select torch.index_select(t, dim1, indexidx) print(t) print(t_select)# torch.masked_select() mask t.ge(5) ## 表示张量中 5的元素ge()表示大于等于gt()表示大于le()表示小于等于lt()表示小于 t_select torch.masked_select(t, maskmask) print(t) print(mask) print(t_select)tensor([[3, 8, 1],[6, 0, 0],[7, 6, 8]]) tensor([[3, 1],[6, 0],[7, 8]]) tensor([[3, 8, 1],[6, 0, 0],[7, 6, 8]]) tensor([[False, True, False],[ True, False, False],[ True, True, True]]) tensor([8, 6, 7, 6, 8])4. 变换 4.1 torch.reshape() 变换张量形状 (张量在内存中是连续时新张量与input共享数据内存) torch.reshape(input,shape ## 新张量的形状 )4.2 torch.transpose() 交换张量的两个维度 torch.transpose(input,dim0,dim1 )4.3 torch.t() 2维张量转置 torch.t(input)4.4 torch.squeeze() 压缩长度为1的维度 torch.squeeze(input,dimNone, ## 若 dimNone则移除所有长度为1的轴若指定维度而且维度长度为1则可以移除该维度outNone )4.5 torch.unsqueeze() 根据 dim 扩展维度 torch.unsqueeze(input,dim,outNone )## torch.reshapet torch.randperm(8) t_reshape torch.reshape(t, (2,4)) ## 如果是 (-1,2,2)那么-1表示的是不需要关心-1处的维度是多少根据后两个维度进行计算得到(比如这里就是 8/2/2 2即-1处的维度是2)print(t.shape) print(t_reshape.shape)## torch.transpose() t torch.rand((2,3,4)) t_transpose torch.transpose(t, dim01, dim12) ## 把第1维和第2维进行交换 print(t.shape) print(t_transpose.shape)## torch.squeeze() t torch.rand((1,2,3,1)) t_squeeze torch.squeeze(t) t_squeeze_0 torch.squeeze(t,dim0) t_squeeze_1 torch.squeeze(t,dim1) print(t_squeeze.shape) print(t_squeeze_0.shape) print(t_squeeze_1.shape)## torch.unsqueeze() t torch.rand((2,3)) t_unsqueeze torch.unsqueeze(t, dim2) print(t_unsqueeze.shape)torch.Size([8]) torch.Size([2, 4]) torch.Size([2, 3, 4]) torch.Size([2, 4, 3]) torch.Size([2, 3]) torch.Size([2, 3, 1]) torch.Size([1, 2, 3, 1]) torch.Size([2, 3, 1])3.2 张量的数学运算 加减乘除 torch.add() torch.addcdiv() torch.addcmul() torch.sub() torch.div() torch.mul()重点: torch.add()和torch.addcmul() torch.add(): 逐元素计算 input alpha x other torch.add(input, ## 第一个张量alpha1, ## 乘项因子 (alpha x other input)other, ## 第二个张量outNone )torch.addcmul() out input value x tensor1 x tensor2 torch.addcmul(input,value1.tensor1,tensor2,outNone )对数、指数、幂函数 torch.log(input, outNone) torch.log10(input, outNone) torch.log2(input, outNone) torch.exp(input, outNone) torch.pow()三角函数 torch.abs(input, outNone) torch.acos(input, outNone) torch.cosh(input, outNone) torch.cos(input, outNone) torch.asin(input, outNone) torch.atan(input, outNone) torch.atan2(input, otherNone, outNone)4. 计算图与动态图 1. 计算图 计算图是用来描述运算的有向无环图包括两个主要元素节点和边。节点表示数据比如 向量、矩阵、张量边表示运算比如 加减乘除卷积等。 叶子结点指的是由用户创建的节点其他非叶子结点都是由叶子结点通过直接或间接的运算得到的。之所以会有叶子结点的概念是因为梯度反向传播结束后只有叶子结点的梯度会被保留非叶子结点的梯度会被从内存中释放掉。可以通过.is_leaf()方法查看是否为叶子结点。 如果想要使用非叶子结点的梯度实现方法是 在计算梯度之后用.retain_grad()方法保存非叶子结点的梯度。 grad_fn: 记录创建该张量(非叶子结点)时所用的方法比如 ya*b那么 y.grad_fn MulBackward0 2. 动态图 计算图可以根据搭建方式的不同分为动态图 (运算与搭建同时进行灵活易调节比如 pytorch) 和静态图 (先搭建后运算高效不灵活比如 tensorflow)。 5. 自动求导系统 autograd 1. torch.autograd.backward 自动计算图中各个结点的梯度 torch.autograd.backward(tensors, ## 用于求导的张量比如 lossgrad_tensorsNone, ## 多梯度权重retain_grapNone, ## 保存计算图create_graphFalse ## 创建导数的计算图同于高阶求导 )2. torch.autograd.grad 求取梯度 torch.autograd.grad(outputs, ## 用于求导的张量 (比如 loss)inputs, ## 需要梯度的张量grad_outputsNone, ## 多梯度权重 retain_graphNone, ## 保存计算图create_graphFalse ## 创建导数计算图用于高阶求导 )3. Pytorch 自动求导系统中有3个需要注意的点 梯度不自动清零 (手动清零 .grad.zero_()) 依赖于叶子结点结点requires_grad 默认为 True 叶子结点不可执行 in-place即叶子结点不能执行原位操作 4. 机器学习模型的5个训练步骤 数据, 模型, 损失函数, 优化器, 迭代训练
http://www.hkea.cn/news/14445429/

相关文章:

  • 网站首页怎么制作免费网站建设哪家好
  • 综合返利商城网站建设淄博网站建设优化
  • wordpress修改站点名论坛外链代发
  • 全网营销建设网站vi设计开题报告
  • 广州公司网站托管用dreamware做网站
  • 那些使用vue做的网站wordpress 批量加密
  • 上海注册建网站做门窗生意进哪个网站
  • 自助定制网站开发公司医疗器械网站建设
  • 重庆免费网站建站模板考研培训
  • 网站建设大企业wordpress单页下载
  • 手机网站开发周期世界工厂网怎么拿货
  • 石油网站建设价格企查查企业信息查询网站
  • 南宁网站建设哪怎样让自己做的网站被百度收录
  • 一搜个人网站制作东莞做网站it s
  • 如何快速提高网站权重网站开发为什么要用框架
  • 网站流量好难做网站建设是什么专业啊
  • 理解电子商务网站建设与管理怎么做自己的品牌网站
  • 网站建设介绍语广州市财贸建设开发监理网站
  • 给艺术家做网站的工作软件开发工程师的发展前景
  • 加盟做网站定制营销的推广方式
  • asp做网站很少网上购物平台有哪些?
  • 淘宝返利网站建设平台电商运营
  • 网站设计和制作费用在凡科网申请的网站设置网页访问密码
  • 公司网站点击量如何看windows搭建网站开发
  • wordpress网站首页链接乱码公众号开发展模式下文章归类到菜单
  • 海外网站seo住房与城乡建设部网站
  • 孝感网站建设软件冠县网站开发
  • app安装官方免费下载站长seo工具
  • 门户网站服务范围建设部网站公示
  • 专门做单页的网站wordpress添加 logo