Reuction operations

Reduction operations

A reduction operations on a tensor is an operation that reduces the number of elements contained within the tensor.

Tensors give us the ability to manage out data. Reduction operations allow us to perform operations on elements within a single tensor.

Suppose we have the following 3$\times$3 rank-2 tensor.

> t = torch.tensor([
[0, 1, 0],
[2, 0, 2],
[0, 3, 0]
], dtype=torch.float32)

Common tensor reduction operations

> t.sum()
tensor(8.) > t.numel()
9 > t.prod()
tensor(0.) > t.mean()
tensor(.8889) > t.std()
tensor(1.1667)

Reducing tensors by axes

Suppose we have the following tensor:

> t = torch.tensor([
[1,1,1,1],
[2,2,2,2],
[3,3,3,3]
], dtype=torch.float32)

This time , we will specify a dimension to reduce.

> t.sum(dim=0)
tensor([6., 6., 6., 6.]) > t.sum(dim=1)
tensor([4., 8., 12.])

Argmax tensor reduction operation

Argmax returns the index location of the maximum value inside a tensor.

t = torch.tensor([
[1,0,0,2],
[0,3,3,0],
[4,0,0,5]
], dtype=torch.float32)

If we don't specific an axis to the argmax() method, it returns the index location of the max value from the flattened tensor, which in the case is indeed 11.

> t.max()
tensor(5.) > t.argmax()
tensor(11) > t.flatten()
tensor([1., 0., 0., 2., 0., 3., 3., 0., 4., 0., 0., 5.])

Work with specific axis now:

> t.max(dim=0)
(tensor([4., 3., 3., 5.]), tensor([2, 1, 1, 2])) > t.argmax(dim=0)
tensor([2, 1, 1, 2]) > t.max(dim=1)
(tensor([2., 3., 5.]), tensor([3, 1, 3])) > t.argmax(dim=1)
tensor([3, 1, 3])

In practice, we often use the argmax() function on a network's output prediction tensor, to determine which category has the highest prediction value.

Reduction operations的更多相关文章

  1. Java8初体验(二)Stream语法详解

    感谢同事[天锦]的投稿.投稿请联系 tengfei@ifeve.com 上篇文章Java8初体验(一)lambda表达式语法比 较详细的介绍了lambda表达式的方方面面,细心的读者会发现那篇文章的例 ...

  2. (转)分布式深度学习系统构建 简介 Distributed Deep Learning

    HOME ABOUT CONTACT SUBSCRIBE VIA RSS   DEEP LEARNING FOR ENTERPRISE Distributed Deep Learning, Part ...

  3. atitit. 集合groupby 的实现(2)---自定义linq查询--java .net php

    atitit.  集合groupby 的实现(2)---自定义linq查询--java .net php 实现方式有如下 1. Linq的实现原理流程(ati总结) 1 2. groupby  与 事 ...

  4. Java8 如何进行stream reduce,collection操作

    Java8 如何进行stream reduce,collection操作 2014-07-16 16:42 佚名 oschina 字号:T | T 在java8 JDK包含许多聚合操作(如平均值,总和 ...

  5. OpenMP初步(英文)

    Beginning OpenMP OpenMP provides a straight-forward interface to write software that can use multipl ...

  6. Stream语法详解

    1. Stream初体验 我们先来看看Java里面是怎么定义Stream的: A sequence of elements supporting sequential and parallel agg ...

  7. Lambdas in Java 8--reference

    Part 1 reference:http://jaxenter.com/lambdas-in-java-8-part-1-49700.html Get to know lambda expressi ...

  8. JDK1.8聚合操作

    在java8 JDK包含许多聚合操作(如平均值,总和,最小,最大,和计数),返回一个计算流stream的聚合结果.这些聚合操作被称为聚合操作.JDK除返回单个值的聚合操作外,还有很多聚合操作返回一个c ...

  9. 关于并行计算的Scan操作

    simple and common parallel algorithm building block is the all-prefix-sums operation. In this chapte ...

随机推荐

  1. Delphi接口使用实例介绍

    对于Object Pascal语言来说,最近一段时间最有意义的改进就是从Delphi3开始支持接口(interface),接口定义了能够与一个对象进行交互操作的一组过程和函数.对一个接口进行定义包含两 ...

  2. Reverse Nodes in k-Group (链表)

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  3. 安卓之webview

    实例 http://blog.csdn.net/carson_ho/article/details/52693322 UI http://blog.csdn.net/fancylovejava/art ...

  4. Centos5.11 //IP/phpmyadmin 远程无法登入

    异地登入phpmyadmin时,会出现"You don't have permission to access /phpmyadmin/ on this server."这是因为配 ...

  5. Firefox下td用display控制页面导致页面变形

    Firefox下table的td元素假设使用了display:'block'会使得table变形.原因是block会将对象强制作为块对象呈递,为对象之后加入新行,所以并不适合td,改成display: ...

  6. HTC 328T 提示手机存储不足 out of space怎么办

    不要再用什么软件做优化,做清理,都是治标不治本啊. 1 用豌豆荚备份数据   2 手机恢复出厂设置   3 用豌豆荚恢复数据

  7. Python中字符运算的优先级

    表1-2 运算符优先级 运算符 描述 lambda Lambda表达式 or 布尔“或” and 布尔“与” not x 布尔“非” in,not in 成员测试 is,is not 同一性测试 &l ...

  8. AIDL/IPC Android AIDL/IPC 进程通信机制——超具体解说及使用方法案例剖析(播放器)

    首先引申下AIDL.什么是AIDL呢?IPC? ------ Designing a Remote Interface Using AIDL 通常情况下,我们在同一进程内会使用Binder.Broad ...

  9. 修正iOS从照相机和相册中获取的图片 方向

    修正iOS从照相机和相册中获取的图片 方向   修正iOS从照相机和相册中获取的图片 方向 使用系统相机拍照得到的图片的默认方向有时不是ImageOrientationDown,而是ImageOrie ...

  10. CronTab命令实例

    每2分钟 将date写入到time.log(以下的为奇数分钟运行) */2 * * * * date >> ~/time.log 1-59/2 * * * * date >> ...