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

滕州市建设网站安卓优化大师2021

滕州市建设网站,安卓优化大师2021,自学做网站要多久,遵义网飞桨的scatter函数,是通过基于 updates 来更新选定索引 index 上的输入来获得输出,具体官网api文档见: scatter-API文档-PaddlePaddle深度学习平台 官网给的例子如下: >>> import paddle>>> x paddle.to_tens…

飞桨的scatter函数,是通过基于 updates 来更新选定索引 index 上的输入来获得输出,具体官网api文档见:

scatter-API文档-PaddlePaddle深度学习平台

官网给的例子如下:

 >>> import paddle>>> x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32')>>> index = paddle.to_tensor([2, 1, 0, 1], dtype='int64')>>> updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32')>>> output1 = paddle.scatter(x, index, updates, overwrite=False)>>> print(output1)Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,[[3., 3.],[6., 6.],[1., 1.]])>>> output2 = paddle.scatter(x, index, updates, overwrite=True)>>> # CPU device:>>> # [[3., 3.],>>> #  [4., 4.],>>> #  [1., 1.]]>>> # GPU device maybe have two results because of the repeated numbers in index>>> # result 1:>>> # [[3., 3.],>>> #  [4., 4.],>>> #  [1., 1.]]>>> # result 2:>>> # [[3., 3.],>>> #  [2., 2.],>>> #  [1., 1.]]

但是如果是初学者,看官网的例子可能还是无法明白scatter的运算方式,下面就结合一个更加明白的例子来说明:

import paddle
x = paddle.to_tensor([[100, 200], [300, 400], [500, 600]], dtype='float32')
index = paddle.to_tensor([2, 1, 0, 1], dtype='int64')
updates = paddle.to_tensor([[10, 11], [21, 22], [33, 34], [40, 41]], dtype='float32')output1 = paddle.scatter(x, index, updates, overwrite=False)
print(output1)
output2 = paddle.scatter(x, index, updates, overwrite=True)
print(output2)

输出结果:

Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,[[33., 34.],[61., 63.],[10., 11.]])
Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,[[33., 34.],[40., 41.],[10., 11.]])

scatter详解

输入是三个值:源值x ,索引index, 变量updates,分析函数输出,可以得出以下结论:

1 scatter函数的输出shape是和x一致的

2 函数输出的值跟x没关系

3 函数输出值跟变量updates值有关

4 输出值跟updates的有关,具体取值的索引跟index有关

具体来说,就是不需要x的值,只使用了它的维度信息,然后根据索引,将变量updates的值填入x的维度中。比如index值是[2, 1, 0, 1],第一位是2,那么就把updates的第一组数[10, 11](也就是updates[0])取出来放到x[2]里;index第二位是1 ,就把的updates的第2组数[21, 22](也就是updates[1])取出来放到x[1]里。以此类推,index第三位是0, 那么就把updates的第3组数[33, 34](也就是updates[2])放到x[0]里。

到了index第四位数,它是1 ,那么就需要把updates的第四组数[40, 41](也就是updates[3]),放入到x[1]中 。这时候有个问题,就是前面x[1]中已经放入了[21,22]。这时候就看函数的overwrite参数的设置了,如果设置overwrite=True ,那么直接用现在的值[40, 41]取代以前的值,最终函数返回结果就是[[33, 34], [40, 41], [10, 11]] 。如果函数设为overwrite=False ,那么就将值[40, 41]与以前的x[1](21, 22)相加,结果是[61, 63],最终返回值就是[[33, 34], [61, 63], [10, 11]]

好了,这样大家就明白scatter的运算机制了吧? 

小贴士

飞桨官网给出了scatter函数的python代码实现,其中因为用了巧妙的思路来提高速度,可读性略有下降:

 >>> import paddle>>> #input:>>> x = paddle.to_tensor([[1, 1], [2, 2], [3, 3]], dtype='float32')>>> index = paddle.to_tensor([2, 1, 0, 1], dtype='int64')>>> # shape of updates should be the same as x>>> # shape of updates with dim > 1 should be the same as input>>> updates = paddle.to_tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype='float32')>>> overwrite = False>>> # calculation:>>> if not overwrite:...     for i in range(len(index)):...         x[index[i]] = paddle.zeros([2])>>> for i in range(len(index)):...     if (overwrite):...         x[index[i]] = updates[i]...     else:...         x[index[i]] += updates[i]>>> # output:>>> out = paddle.to_tensor([[3, 3], [6, 6], [1, 1]])>>> print(out.shape)[3, 2]

scatter的运算机制不管overwrite是否为True,x的值都不参与运算,理论上应该都清除,也就是在循环里置0 :x[index[i]] = paddle.zeros([2])

实际上如果overwrite是True,那么在赋值的时候本身可以直接写入 x[index[i]] = updates[i],这样就可以省略x[index[i]] = paddle.zeros([2])这句,这就是为什么这段置0代码放到了条件:if not overwrite: 这句里面的原因。

http://www.hkea.cn/news/893020/

相关文章:

  • 网站空间商推荐seo是什么职位缩写
  • 怎么建设boss网站文件外链
  • 百度推广网站建设费百度搜索引擎的网址是多少
  • php 手机网站 上传图片定制网站建设
  • 关于网站建设的问题百度关键词分析
  • 登录官方网站装修公司网络推广方案
  • 设计网站官网入口网站搜索优化方法
  • 网站优化qq群山东做网站
  • wordpress icomoon太原seo快速排名
  • 中华建设杂志网站记者数据指数
  • 网站开发测试情况南召seo快速排名价格
  • 上海仓储公司小红书seo优化
  • 南京建设公司网站网络营销整合推广
  • wordpress更改语言沈阳seo优化
  • wordpress免费网站世界大学排名
  • 做网站的属于什么专业?百度爱采购竞价推广
  • 网站建设一年多少恰东莞网站到首页排名
  • 新企业网站应该怎么做SEO优化广告联盟有哪些
  • 手机app开发网站建设软文推广文章案例
  • 网站自然排名百度经验官网登录
  • dz网站模板沧州网站优化公司
  • 桂林论坛天涯社区培训行业seo整站优化
  • 做伊瑞尔竞技场的网站搜索引擎简称seo
  • 46云虚拟主机股票发行ipo和seo是什么意思
  • 新泰做网站菏泽seo
  • 网站建设排名东莞seo收费
  • 做网站前后端的发布流程自己如何制作网站
  • 网站营销与推广策略百度一下官网首页百度
  • 网站建设张世勇100个免费推广b站
  • 网络营销的常用工具百度关键词优化点击 教程