import torch
import numpy as np

details about math operation in torch can be found in: http://pytorch.org/docs/torch.html#math-operations

convert numpy to tensor or vise versa

# convert numpy to tensor or vise versa
np_data = np.arange(6).reshape((2, 3))
torch_data = torch.from_numpy(np_data) # np转成torch
tensor2array = torch_data.numpy() # torch转成np
print(
'\nnumpy array:', np_data, # [[0 1 2] [3 4 5]]
'\ntorch tensor:', torch_data, # tensor([[0, 1, 2], [3, 4, 5]])
'\ntensor to array:', tensor2array, # [[0 1 2], [3 4 5]]
)

abs 绝对值

# abs
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data) # 32-bit floating point
print(
'\nabs',
'\nnumpy: ', np.abs(data), # [1 2 1 2]
'\ntorch: ', torch.abs(tensor) # tensor([1., 2., 1., 2.])
)

sin 正弦值

# sin
print(
'\nsin',
'\nnumpy: ', np.sin(data), # [-0.84147098 -0.90929743 0.84147098 0.90929743]
'\ntorch: ', torch.sin(tensor) # tensor([-0.8415, -0.9093, 0.8415, 0.9093])
)

mean 均值

# mean
print(
'\nmean',
'\nnumpy: ', np.mean(data), # 0.0
'\ntorch: ', torch.mean(tensor) # tensor(0.)
)

matrix multiplication 矩阵乘法

# matrix multiplication
data = [[1, 2], [3, 4]]
tensor = torch.FloatTensor(data) # 32-bit floating point
# correct method
print(
'\nmatrix multiplication (matmul)',
'\nnumpy: ', np.matmul(data, data), # [[7, 10], [15, 22]]
'\ntorch: ', torch.mm(tensor, tensor) # tensor([[ 7., 10.], [15., 22.]])
)

incorrect method 不正确的方法

# incorrect method
data = np.array(data)
print(
'\nmatrix multiplication (dot)',
'\nnumpy: ', data.dot(data), # [[7, 10], [15, 22]]
'\ntorch: ', tensor.dot(tensor) # this will convert tensor to [1,2,3,4], you'll get 30.0, RuntimeError: dot: Expected 1-D argument self, but got 2-D
)

pytorch 1 torch_numpy, 对比的更多相关文章

  1. 库、教程、论文实现,这是一份超全的PyTorch资源列表(Github 2.2K星)

    项目地址:https://github.com/bharathgs/Awesome-pytorch-list 列表结构: NLP 与语音处理 计算机视觉 概率/生成库 其他库 教程与示例 论文实现 P ...

  2. PyTorch官方教程中文版

    首先呈上链接:http://pytorch123.com/ PyTorch是一个基于Torch的Python开源机器学习库,用于自然语言处理等应用程序.它主要由Facebookd的人工智能小组开发,不 ...

  3. PyTorch专栏(一)

    专栏目录: 第一章:PyTorch之简介与下载 PyTorch简介 PyTorch环境搭建 第二章:PyTorch之60min入门 PyTorch 入门 PyTorch 自动微分 PyTorch 神经 ...

  4. 总结笔记 | 深度学习之Pytorch入门教程

    笔记作者:王博Kings 目录 一.整体学习的建议 1.1 如何成为Pytorch大神? 1.2 如何读Github代码? 1.3 代码能力太弱怎么办? 二.Pytorch与TensorFlow概述 ...

  5. 『PyTorch』第一弹_静动态图构建if逻辑对比

    对比TensorFlow和Pytorch的动静态图构建上的差异 静态图框架设计好了不能够修改,且定义静态图时需要使用新的特殊语法,这也意味着图设定时无法使用if.while.for-loop等结构,而 ...

  6. 【转载】 Caffe BN+Scale层和Pytorch BN层的对比

    原文地址: https://blog.csdn.net/elysion122/article/details/79628587 ------------------------------------ ...

  7. 对比学习:《深度学习之Pytorch》《PyTorch深度学习实战》+代码

    PyTorch是一个基于Python的深度学习平台,该平台简单易用上手快,从计算机视觉.自然语言处理再到强化学习,PyTorch的功能强大,支持PyTorch的工具包有用于自然语言处理的Allen N ...

  8. PyTorch载入图片后ToTensor解读(含PIL和OpenCV读取图片对比)

    概述 PyTorch在做一般的深度学习图像处理任务时,先使用dataset类和dataloader类读入图片,在读入的时候需要做transform变换,其中transform一般都需要ToTensor ...

  9. 深度学习框架Keras与Pytorch对比

    对于许多科学家.工程师和开发人员来说,TensorFlow是他们的第一个深度学习框架.TensorFlow 1.0于2017年2月发布,可以说,它对用户不太友好. 在过去的几年里,两个主要的深度学习库 ...

随机推荐

  1. PHP 7.1.15安装zabbix-3.2.6出现问题解决

    出现问题,显示 A non well formed numeric value encountered [zabbix.php:21 → require_once() → ZBase->run( ...

  2. Anaconda安装tensorflow遇到的问题(转)

    tensorflow安装教程 1.找不到Anaconda Prompt 其他教程中说AnacondaPrompt在安装的Anaconda文件夹下面(如D:/Anaconda),但是我在安装中没有找到, ...

  3. jQuery scrollFix滚动定位插件

    [插件功能] 当用户向上或向下滚动页面到一定位置时,目标元素开始固定定位(position:fixed),当回滚到原位置时目标元素恢复到原状态,可以定制触发滚动相对屏幕位置和触发滚动方向,兼容IE6 ...

  4. mybatis入门截图四(订单商品数据模型 一对一,一对多,多对多)

    --------------------------------- 一对一查询 查询订单信息,关联查询创建订单的用户信息 1.高级映射-一对一查询-使用resultType 2.高级映射-一对一查询- ...

  5. String String s = new String("asd") 涉及对象数目

    问题·:.String str = new String("abc")创建了多少个对象? 这个问题在很多书籍上都有说到比如<Java程序员面试宝典>,包括很多国内大公司 ...

  6. 解决 Mac OS X 10.11 安装 sip 没有权限的问题

    在搭建 PYQT 的过程中我遇上了一个非常恶心的问题,在安装 sip 的时候编译源代码之后的安装过程中一直提示我:Operation not permitted ,我甚至重装了系统也无济于事,终于通过 ...

  7. 自己定义button

    我们应该建立自己的代码库,建立自己的工厂 苹果公司给我们提供了强大的利器 可是我们不应该以简简单单的实现基本功能就满足了 大牛的成长之路.都是自己慢慢深入研究 我们要有成长为大牛的目标 今天给大家写个 ...

  8. struts2在action中获取request、session、application,并传递数据

    假设仅仅是通过request.session.application传递数据,则不须要获取对应的对象也能够传递数据,代码例如以下: ScopeAction.java: package com.ithe ...

  9. tflearn中一些CNN RNN的例子

    lstm.py # -*- coding: utf-8 -*- """ Simple example using LSTM recurrent neural networ ...

  10. tabBar的图标不被系统渲染

    navi.tabBarItem.selectedImage = [[UIImage imageNamed:imageStr]imageWithRenderingMode:UIImageRenderin ...