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
np_data = np.arange(6).reshape((2, 3))
torch_data = torch.from_numpy(np_data)
tensor2array = torch_data.numpy()
print(
'\nnumpy array:', np_data, # [[0 1 2], [3 4 5]]
'\ntorch tensor:', torch_data, # 0 1 2 \n 3 4 5 [torch.LongTensor of size 2x3]
'\ntensor to array:', tensor2array, # [[0 1 2], [3 4 5]]
) # 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) # [1 2 1 2]
) # sin
print(
'\nsin',
'\nnumpy: ', np.sin(data), # [-0.84147098 -0.90929743 0.84147098 0.90929743]
'\ntorch: ', torch.sin(tensor) # [-0.8415 -0.9093 0.8415 0.9093]
) # mean
print(
'\nmean',
'\nnumpy: ', np.mean(data), # 0.0
'\ntorch: ', torch.mean(tensor) # 0.0
) # 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) # [[7, 10], [15, 22]]
)
# 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
)

仅为自己练习,没有其他用途

pytorch之 compare with numpy的更多相关文章

  1. Tensor:Pytorch神经网络界的Numpy

    摘要:Tensor,它可以是0维.一维以及多维的数组,你可以将它看作为神经网络界的Numpy,它与Numpy相似,二者可以共享内存,且之间的转换非常方便. 本文分享自华为云社区<Tensor:P ...

  2. RuntimeError: PyTorch was compiled without NumPy support

    原因:Pytorch和Numpy版本不匹配 查看自己Pytorch和Numpy版本 (1)执行[pip show torch]和[pip show numpy]查看版本信息(可通过[pip -h]查看 ...

  3. pytorch中tensor数据和numpy数据转换中注意的一个问题

    转载自:(pytorch中tensor数据和numpy数据转换中注意的一个问题)[https://blog.csdn.net/nihate/article/details/82791277] 在pyt ...

  4. PyTorch入门(一)向量

    什么是PyTorch?   PyTorch是Facebook人工智能团队开发的一个机器学习和深度学习工具,用于处理大规模图像分析,包括物体检测,分割与分类.但是它的功能不仅限于此.它与其它深度学习框架 ...

  5. 神经网络架构PYTORCH-几个概念

    使用Pytorch之前,有几个概念需要弄清楚. 什么是Tensors(张量)? 这个概念刚出来的时候,物理科班出身的我都感觉有点愣住了,好久没有接触过物理学的概念了. 这个概念,在物理学中怎么解释呢? ...

  6. pytorch visdom可视化工具学习—1—详细使用-1—基本使用函数

    使用教程,参考: https://github.com/facebookresearch/visdom https://www.pytorchtutorial.com/using-visdom-for ...

  7. pytorch visdom可视化工具学习—1—详细使用-2-plotting绘图

    3)plotting绘图 我们已经包装了几种常见的plot类型,以便轻松创建基本的可视化.这些可视化是由Plotly驱动的. Visdom支持下列API.由 Plotly 提供可视化支持. vis.s ...

  8. Pytorch Visdom可视化工具

    2018-12-04 14:05:49 Visdom是Facebook专门为PyTorch开发的一款可视化工具,其开源于2017年3月.Visdom十分轻量级,但却支持非常丰富的功能,能胜任大多数的科 ...

  9. pytorch 0.3 win7 安装

    pytorch 0.3 win7 安装 参考这个文章:https://github.com/peterjc123/pytorch-scripts 首先安装 conda 这个链接下载: python 3 ...

随机推荐

  1. 唬人的Java泛型并不难

    泛型 public interface Foo<E> {}public interface Bar<T> {}public interface Zar<?> {} ...

  2. 全网最详细的Linux命令系列-sed文本处理命令

    Sed简介 SED是一个非交互式文本编辑器,它可对文本文件和标准输入进行编辑,标准输入可以来自键盘输入.文本重定向.字符串.变量,甚至来自于管道的文本,与VIM编辑器类似,它一次处理一行内容,Sed可 ...

  3. 数字金字塔 动态规划(优化版) USACO 一维dp压缩版

    1016: 1.5.1 Number Triangles 数字金字塔 时间限制: 1 Sec  内存限制: 128 MB提交: 9  解决: 8[提交] [状态] [讨论版] [命题人:外部导入] 题 ...

  4. .net core 不是开源的么 作为菜 不能贡献源码 只有 欣赏额

    step one 去download一份 与前辈在一起

  5. KALI美化-设置CONKY开机启动

    简介 Conky 是一个应用于桌面环境的系统监视软件,可以在桌面上监控系统运行状态.网络状态等一系列参数 https://github.com/brndnmtthws/conky/ 详细配置文档:ht ...

  6. printf 函数笔记

    函数声明 int printf (const char*, ...); 说明 <返回值> printf ("<格式化字符串>", <参数表>); ...

  7. 小白学Java:I/O流

    目录 小白学Java:I/O流 基本分类 发展史 文件字符流 输出的基本结构 流中的异常处理 异常处理新方式 读取的基本结构 运用输入与输出 文件字节流 缓冲流 字符缓冲流 装饰设计模式 转换流(适配 ...

  8. 笔记常用Linux命令(二) 进程和端口

    查看系统进程 ps:用于报告当前系统的进程状态 a:显示所有终端机下执行的程序 ps -ef/ps aux: 这两个命令都是查看当前系统正在运行进程,两者的区别是展示格式不同. 如果想要查看特定的进程 ...

  9. Git基础常用功能

    一.安装 具体查看 安装Git 二.使用 基础知识 工作区(Workspace):就是你在电脑里能看到的项目目录. 暂存区(Index / Stage):临时存放更改的地方,使用命令"git ...

  10. Springboot | Failed to execute goal org.springframework.boot:spring-boot-maven-plugin

    案例 今天搭建spring boot 环境时,使用mvn install ,出现Failed to execute goal org.springframework.boot:spring-boot- ...