1.什么是Numpy
Numpy系统是Python的一种开源的数值计算扩展,用python实现的科学计算包。这种工具可用来存储和处理大型矩阵,包括强大的N维数组对象Array,比较成熟的函数库等。numpy和稀疏矩阵运算包scipy配合使用更加方便。

2.用Numpy还是Torch
Torch自称为神经网络界的Numpy,它能将torch产生的tensor放在GPU中加速运算,就想Numpy会把array放在CPU中加速运算。所以在神经网络中,用Torch的tensor形式更优。

但是为了减少用户的学习成本,Torch对Numpy实现了很好的兼容。可以用如下形式在numpy array和torch tensor之间自由转换。

import torch
import numpy as np

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]]
)

3.Torch中的数学运算

torch中的tensor运算和numpy的array运算很相似,具体参看下面的代码:

# abs 绝对值计算
data = [-1, -2, 1, 2]
tensor = torch.FloatTensor(data) # 转换成32位浮点 tensor
print(
'\nabs',
'\nnumpy: ', np.abs(data), # [1 2 1 2]
'\ntorch: ', torch.abs(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) # [-0.8415 -0.9093 0.8415 0.9093]
)

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

numpy和torch的矩阵乘法还是有点不同的,下面将对其区别进行展示:

# matrix multiplication 矩阵点乘
data = [[1,2], [3,4]]
tensor = torch.FloatTensor(data) # 转换成32位浮点 tensor
# correct method
print(
'\nmatrix multiplication (matmul)',
'\nnumpy: ', np.matmul(data, data), # [[7, 10], [15, 22]]
'\ntorch: ', torch.mm(tensor, tensor) # [[7, 10], [15, 22]]
)

# !!!! 下面是错误的方法 !!!!
# 注意这里要转换成array,因为data原来是list对象,其没有.dot操作
data = np.array(data)
print(
'\nmatrix multiplication (dot)',
'\nnumpy: ', data.dot(data), # [[7, 10], [15, 22]] 在numpy 中可行
'\ntorch: ', tensor.dot(tensor) # torch 会转换成 [1,2,3,4].dot([1,2,3,4]) = 30.0
)

Torch或Numpy的更多相关文章

  1. 莫烦pytorch学习笔记(一)——torch or numpy

    Q1:什么是神经网络? Q2:torch vs numpy Numpy:NumPy系统是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(neste ...

  2. torch or numpy

    黄色:重点 粉色:不懂 Torch 自称为神经网络界的 Numpy, 因为他能将 torch 产生的 tensor 放在 GPU 中加速运算 (前提是你有合适的 GPU), 就像 Numpy 会把 a ...

  3. 关于类型为numpy,TensorFlow.tensor,torch.tensor的shape变化以及相互转化

    https://blog.csdn.net/zz2230633069/article/details/82669546 2018年09月12日 22:56:50 一只tobey 阅读数:727   1 ...

  4. torch.Tensor和numpy.ndarray

    1. torch.Tensor和numpy.ndarray相互转换 import torch import numpy as np # <class 'numpy.ndarray'> np ...

  5. 奉献pytorch 搭建 CNN 卷积神经网络训练图像识别的模型,配合numpy 和matplotlib 一起使用调用 cuda GPU进行加速训练

    1.Torch构建简单的模型 # coding:utf-8 import torch class Net(torch.nn.Module): def __init__(self,img_rgb=3,i ...

  6. pytorch之 compare with numpy

    import torch import numpy as np # details about math operation in torch can be found in: http://pyto ...

  7. torch文档学习笔记

    下面为官方文档学习笔记    http://pytorch.org/docs/0.3.0/index.html 1.torch.Tensor from __future__ import print_ ...

  8. torch基础学习

    目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...

  9. 使用torch实现RNN

    (本文对https://blog.csdn.net/out_of_memory_error/article/details/81456501的结果进行了复现.) 在实验室的项目遇到了困难,弄不明白LS ...

随机推荐

  1. mybatis oracle 插入自增记录 获取主键值 写回map参数

    网上搜了好多文章照着弄都返回不了主键给map, 实践证明要在传入的map参数里写回插入的主键,要这样写 <selectKey resultType="java.lang.Integer ...

  2. [Python] Frequently used method or solutions for issues

    Web Scraping爬虫 for Mac urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] cer ...

  3. Redis:Sentinel哨兵

    简介 Sentinel的作用就是主从切换:Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案,当用Redis做Master-slave的高可用方案时,假如master宕机了,R ...

  4. node.js初识01

    1.对于node.js的安装在这里就不做过多的介绍了,安装成功后,可以通过cmd 输入node -v查看node的版本号,如图所示 2.开始我们的hello world,通过cmd进入所属文件夹,输入 ...

  5. VS 星期作业 if else的应用 做一个受不受异性欢迎的小程序

    static void Main(string[] args) { //漏掉代码 输入错误 进行提示! string T1, T2, T3, T4, T5, T6, T7, T8, T9, T10=& ...

  6. Ajax-创建ajax

    UNSENT : 未发送,刚开始创建完成AJAX对象,默认的状态就是0 OPENED : 已打开,执行了xhr.open之后状态变为1 HEADERS_RECEIVED :响应头信息已经成功的返回并且 ...

  7. d'jang基础

    1,建立一个名为guest的django项目,django-admin startproject  guest 生成一个guest文件夹,包含一个guest文件夹和一个manage.py cd gue ...

  8. Rpgmakermv(25) 游戏数据

    随着对RMMV插件了解的深入,我们会发现如果我们想要对游戏数据进行一些扩展,首先要了解游戏数据,游戏数据在官方代码中的rpg_managers.js里,这一节我们将要对这个官方类有一些基础的了解,并且 ...

  9. Join The Future (剪枝 + 状态压缩)

    一道暴力搜索的恶心剪枝题目. 先处理好某个点确定之后其他点的也确定的是谁,还有分别为什么情况,分别用vis,sta来记录.当然可以直接使用一个3进制数来表示,但是这里需要额外写一个三进制数求值的函数较 ...

  10. 【Linux学习六】用户管理

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.增加删除用户或组新增用户useradd scott修改用户密码pa ...