pytorch 1 torch_numpy, 对比
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, 对比的更多相关文章
- 库、教程、论文实现,这是一份超全的PyTorch资源列表(Github 2.2K星)
项目地址:https://github.com/bharathgs/Awesome-pytorch-list 列表结构: NLP 与语音处理 计算机视觉 概率/生成库 其他库 教程与示例 论文实现 P ...
- PyTorch官方教程中文版
首先呈上链接:http://pytorch123.com/ PyTorch是一个基于Torch的Python开源机器学习库,用于自然语言处理等应用程序.它主要由Facebookd的人工智能小组开发,不 ...
- PyTorch专栏(一)
专栏目录: 第一章:PyTorch之简介与下载 PyTorch简介 PyTorch环境搭建 第二章:PyTorch之60min入门 PyTorch 入门 PyTorch 自动微分 PyTorch 神经 ...
- 总结笔记 | 深度学习之Pytorch入门教程
笔记作者:王博Kings 目录 一.整体学习的建议 1.1 如何成为Pytorch大神? 1.2 如何读Github代码? 1.3 代码能力太弱怎么办? 二.Pytorch与TensorFlow概述 ...
- 『PyTorch』第一弹_静动态图构建if逻辑对比
对比TensorFlow和Pytorch的动静态图构建上的差异 静态图框架设计好了不能够修改,且定义静态图时需要使用新的特殊语法,这也意味着图设定时无法使用if.while.for-loop等结构,而 ...
- 【转载】 Caffe BN+Scale层和Pytorch BN层的对比
原文地址: https://blog.csdn.net/elysion122/article/details/79628587 ------------------------------------ ...
- 对比学习:《深度学习之Pytorch》《PyTorch深度学习实战》+代码
PyTorch是一个基于Python的深度学习平台,该平台简单易用上手快,从计算机视觉.自然语言处理再到强化学习,PyTorch的功能强大,支持PyTorch的工具包有用于自然语言处理的Allen N ...
- PyTorch载入图片后ToTensor解读(含PIL和OpenCV读取图片对比)
概述 PyTorch在做一般的深度学习图像处理任务时,先使用dataset类和dataloader类读入图片,在读入的时候需要做transform变换,其中transform一般都需要ToTensor ...
- 深度学习框架Keras与Pytorch对比
对于许多科学家.工程师和开发人员来说,TensorFlow是他们的第一个深度学习框架.TensorFlow 1.0于2017年2月发布,可以说,它对用户不太友好. 在过去的几年里,两个主要的深度学习库 ...
随机推荐
- Spring MVC 的概念1
---恢复内容开始--- SpringMVC是一个采用模型----视图------控制器(MVC)的WEb框架建立在中央前端控制器的 Servlet(DispatcherServlet),他负责发送每 ...
- 非阻塞IO函数
关于效率的优化:
- [Android]RecyclerView的简单演示样例
去年google的IO上就展示了一个新的ListView.它就是RecyclerView. 下面是官方的说明,我英语能力有限,只是我大概这么理解:RecyclerView会比ListView更具有拓展 ...
- hdu5319 Painter(模拟)
题目链接:点击打开链接 题目大意:给一个矩形.有两把刷子,一把刷红色,一把刷蓝色,红色的方向是东南,蓝色的方向是西北,红色加蓝色等于绿色,如今已知这面墙当前的状态.求从白墙到这个状态最少刷了多少次. ...
- 用jquery ajax做的select菜单,选中的效果
//用server端语言赋值给js变量 var departmentId = '<%=提交的值 %>', deviceId='<%=提交的值 %>' $(fun ...
- angularjs1- ng-include
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Sublime text3 Emmet使用
Emmet需要配置pyv8 进入 https://github.com/emmetio/pyv8-binaries 下载解压文件放入Sublime Installed Packages下面 就可以使用 ...
- USACO 2.2 Preface Numbering
Preface Numbering A certain book's prefaces are numbered in upper case Roman numerals. Traditional R ...
- Thread.setDaemon设置说明
转载地址:http://blog.csdn.net/m13666368773/article/details/7245570 Thread.setDaemon的用法,经过学习以后了解: 1. setD ...
- CentOS 6.7操作系统安装
如果由于是显卡驱动不兼容的话,在选择安装界面按tab键,进入命令行,然后在命令行后加上 nodmraid 关键字回车开始安装. 接下来选择hard driver 选择最后一个分区进行系统安装,然后 ...