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. HDU 6278 主席树(区间第k大)+二分

    Just h-index Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)To ...

  2. Sudoku---hdu2676(数独DFS)

    http://poj.org/problem?id=2676 递归深搜 #include<stdio.h> #include<string.h> #include<alg ...

  3. UESTC 1087 【二分查找】

    问了某明==shit 中文题意不解释. 因为数据的范围是1e9以内的所以我们可以通过二分的方法枚举可能的中位数. 用二分法每次判断某一数字比中位数大还是比中位数小. 判断方法是,枚举以第n个数为起点的 ...

  4. asterisk 相关数据库配置 使用

    Linux/Unix下ODBC的安装: 先下载最新的unixODBC源码包(http://www.unixodbc.org/unixODBC-2.2.1.tar.gz)放到/usr/local下,然后 ...

  5. QT窗体间传值总结之Signal&Slot

    在写程序时,难免会碰到多窗体之间进行传值的问题.依照自己的理解,我把多窗体传值的可以使用的方法归纳如下: 1.使用QT中的Signal&Slot机制进行传值: 2.使用全局变量: 3.使用pu ...

  6. ASP.NET MVC 学习笔记-7.自定义配置信息 ASP.NET MVC 学习笔记-6.异步控制器 ASP.NET MVC 学习笔记-5.Controller与View的数据传递 ASP.NET MVC 学习笔记-4.ASP.NET MVC中Ajax的应用 ASP.NET MVC 学习笔记-3.面向对象设计原则

    ASP.NET MVC 学习笔记-7.自定义配置信息   ASP.NET程序中的web.config文件中,在appSettings这个配置节中能够保存一些配置,比如, 1 <appSettin ...

  7. 在Oracle数据库中使用NFS,怎样调优?

    MOS上有好多文章,基本上都跑不了以下三点: Setup can make a big difference 1. Network topology and load 2. NFS mount opt ...

  8. mysql学习笔记之mysql数据库的安装

    1.执行mysql安装包选择自己定义安装(安装路径不要带中文,否则安装会出错! ) 2.一个mysql想要操作成功须要有三部分:server端,数据段,数据. 3.server软件文件夹: 4.数据文 ...

  9. SQL Server索引原理解析

    此文是我之前的笔记整理而来,以索引为入口进行探讨相关数据库知识(又做了修改以让人更好消化).SQL Server接触不久的朋友可以只看以下蓝色字体字,简单有用节省时间:如果是数据库基础不错的朋友,可以 ...

  10. autofac如何注册静态方法里的接口对象

    标题可能是不准确的,因为我不知道如何描述.不知道的原因,是对依赖注入一知半解. Autofac可以自动注册对象实例到接口,人所尽知.而在asp.net mvc中,这个实例化的工作,通常在每个控制器的构 ...