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

南昌网站建设哪家比较好无锡城乡建设局网站

南昌网站建设哪家比较好,无锡城乡建设局网站,免费的域名网站,next.js做纯静态网站index_add paddle.index_add(x, index, axis, value, nameNone)[源代码] 沿着指定轴 axis 将 index 中指定位置的 x 与 value 相加,并写入到结果 Tensor 中的对应位置。这里 index 是一个 1-D Tensor。除 axis 轴外,返回的 Tensor 其余维度大小和输入 …

index_add¶

paddle.index_add(xindexaxisvaluename=None)[源代码]¶

沿着指定轴 axis 将 index 中指定位置的 x 与 value 相加,并写入到结果 Tensor 中的对应位置。这里 index 是一个 1-D Tensor。除 axis 轴外,返回的 Tensor 其余维度大小和输入 x 相等, axis 维度的大小等于 index 的大小。

官方文档:index_add-API文档-PaddlePaddle深度学习平台

我们还是通过一个代码示例来学习:

x = paddle.ones([5, 3])
value = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=paddle.float32)
index = paddle.to_tensor([0, 4, 2])
print(x)x = paddle.index_add(x, index, 0, value)
print(x)

 输出

Tensor(shape=[5, 3], dtype=float32, place=Place(cpu), stop_gradient=True,[[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]])
Tensor(shape=[5, 3], dtype=float32, place=Place(cpu), stop_gradient=True,[[2. , 3. , 4. ],[1. , 1. , 1. ],[8. , 9. , 10.],[1. , 1. , 1. ],[5. , 6. , 7. ]])

API 解析:index_add

查看前面的例子输出,可以看到,index_add就是把value的各个值,按照index里的值为索引,加入到源x里面去,比如

value = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=paddle.float32)
index = paddle.to_tensor([0, 4, 2])

首先取出value[0] ,发现index[0]是 0,那么就把value[0] 跟x[0]相加

取出value[1] ,发现index[1] 是4,那么就把value[1] 跟x[4]相加

取出value[2] ,发现index[2] 是2,那么就把value[2] 跟x[2]相加

在飞桨官方没有index_add函数的时候,可以用python来实现,当然速度会慢很多:

def paddleindex_add(x, dim, index, source): # 飞桨的index_add'''
x = paddle.ones([5, 3])
t = paddle.to_tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=paddle.float32)
index = paddle.to_tensor([0, 4, 2])
# print(x)
with Benchmark("paddleindex_add"):x = paddleindex_add(x, 0, index, t)
print(x)'''for i in range(len(index)):x[index[i]] += source[i]return x

可以从赋值语句看到,就是从index里面取出值,然后x和source的相关值相加:x[index[i]] += source[i]

当然注释里面用了Benchmark函数,抄李沐老师的,源码如下

import time
class Timer:  #@save"""记录多次运行时间"""def __init__(self):self.times = []self.start()def start(self):"""启动计时器"""self.tik = time.time()def stop(self):"""停止计时器并将时间记录在列表中"""self.times.append(time.time() - self.tik)return self.times[-1]def avg(self):"""返回平均时间"""return sum(self.times) / len(self.times)def sum(self):"""返回时间总和"""return sum(self.times)def cumsum(self):"""返回累计时间"""return np.array(self.times).cumsum().tolist()class Benchmark:"""用于测量运行时间"""def __init__(self, description='Done'):self.description = descriptiondef __enter__(self):self.timer = Timer()return selfdef __exit__(self, *args):print(f'{self.description}: {self.timer.stop():.4f} sec')

http://www.yayakq.cn/news/54366/

相关文章:

  • 做暖暖小视频网站编辑网页软件
  • 千素网站建设五棵松网站建设
  • 有人模仿qq音乐做的h5网站吗网站变灰是什么事
  • 社交网站建设网站广州海珠区最新通告
  • 免费商城版网站成都网站制作scgckj
  • 中国公司100强排名seo推广小分享
  • 如何申请网址域名深圳企业网站seo
  • 网站app软件下载安装wordpress post请求
  • 网站建设可视化企业年金可以取出来吗
  • 一个网站怎么建设凡客诚品上一年度市场份额
  • 东莞网站优化排名网站前端网页设计样例
  • wordpress恢复老版本seo优化首页
  • 企业网站建设的具体需求玉溪网站制作公司
  • 婴儿网站建设住栏目如何建立一个网站视频教学
  • 福田企业网站优化最好的方法网站过期就可以抢注
  • 岳阳网站定制开发设计出站链接对网站有什么影响
  • 在线网站建设建议mvc做门户网站
  • 银川网站建设广告公司名单给 小企业 建设网站
  • 手机建网站软件图片制作软件哪个好用
  • 湖北智能建站系统价格工信部网站备案查询验证码错误
  • 合肥网站营销推广网站开发技术课程报告
  • 网站建设开发案例教程视频教程网站动态和静态
  • 北京市网站开发沈阳好的男科医院是哪一家
  • 做网站app的工资高吗sticklr wp wordpress
  • php众筹网站程序源码汕头站扩建
  • tp5网站开发模板wordpress带支付主题
  • 福州建设厅官方网站微信app下载安装旧版本
  • 做网站大概要自在威海智慧旅游平台app
  • wordpress建站服务器选择jsp 网站开发
  • 谷歌seo网站优化网站后台需要ie6修改