深度学习之pytorch(二) 数据并行

又是好久没更新博客,最近琐事缠身,写文档写到吐。没时间学习新的知识,刚空闲下来立刻就学习之前忘得差不多得Pytorch。Pytorch和tensorflow差不多,具体得就不多啰嗦了,觉得还有疑问的童鞋可以自行去官网学习,官网网址包括:

1.https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html

2.https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn/#class-torchnndataparallelmodule-device_idsnone-output_devicenone-dim0source

 

个人觉得值得一说的是关于数据并行这部分:

根据官网的教程代码总结如下所示:

# -*- coding: utf-8 -*-
"""
Created on Tue Apr 16 14:32:58 2019@author: kofzh
"""import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader# Parameters and DataLoaders
input_size = 5
output_size = 2batch_size = 30
data_size = 100device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")class RandomDataset(Dataset):def __init__(self, size, length):self.len = lengthself.data = torch.randn(length, size)def __getitem__(self, index):return self.data[index]def __len__(self):return self.lenrand_loader = DataLoader(dataset=RandomDataset(input_size, data_size),batch_size=batch_size, shuffle=True)class Model(nn.Module):# Our modeldef __init__(self, input_size, output_size):super(Model, self).__init__()self.fc = nn.Linear(input_size, output_size)def forward(self, input):output = self.fc(input)print("\tIn Model: input size", input.size(),"output size", output.size())return outputmodel = Model(input_size, output_size)
if torch.cuda.device_count() > 1:print("Let's use", torch.cuda.device_count(), "GPUs!")# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUsmodel = nn.DataParallel(model)model.to(device)
for data in rand_loader:input = data.to(device)output = model(input)print("Outside: input size", input.size(),"output_size", output.size())

对于上述代码分别是 batchsize= 30在CPU和4路GPU环境下运行得出的结果:

CPU with batchsize= 30结果:

In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

4路 GPU with batchsize= 30 结果:

Let's use 4 GPUs!In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])In Model: input size torch.Size([6, 5]) output size torch.Size([6, 2])In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])In Model: input size    In Model: input sizetorch.Size([8, 5])  In Model: input size torch.Size([8, 5])output size      In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])torch.Size([6, 5]) output size torch.Size([6, 2])output size torch.Size([8, 2])torch.Size([8, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])In Model: input size    In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])
torch.Size([8, 5]) output size torch.Size([8, 2])In Model: input size torch.Size([8, 5]) output size torch.Size([8, 2])In Model: input size torch.Size([6, 5]) output size torch.Size([6, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])In Model: input size    In Model: input sizetorch.Size([3, 5])  In Model: input size torch.Size([3, 5]) output size torch.Size([3, 2])torch.Size([3, 5]) output size torch.Size([3, 2])output size torch.Size([3, 2])In Model: input size torch.Size([1, 5]) output size torch.Size([1, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

对于上述代码分别是 batchsize= 25在CPU和4路GPU环境下运行得出的结果:

CPU with batchsize= 25结果:

 In Model: input size torch.Size([25, 5]) output size torch.Size([25, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size torch.Size([25, 5]) output size torch.Size([25, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size torch.Size([25, 5]) output size torch.Size([25, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size torch.Size([25, 5]) output size torch.Size([25, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])

4路 GPU with batchsize= 25 结果:

Let's use 4 GPUs!In Model: input size    In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size    In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])torch.Size([4, 5]) output size torch.Size([4, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size    In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])torch.Size([4, 5]) output size torch.Size([4, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size    In Model: input size  torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])
torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
Outside: input size torch.Size([25, 5]) output_size torch.Size([25, 2])In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size    In Model: input size torch.Size([7, 5]) output size torch.Size([7, 2])torch.Size([7, 5]) output size torch.Size([7, 2])In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])

个人总结:batchsize在实际训练时对于每个GPU的分配基本上采用的是平均分配数据的原则,每个GPU分配的数据相加 = batchsize。

备注:

1.官网上的CPU结果图是错误的,可能是图截错了,在此对于某些博客直接抄袭官网的结果图表示鄙视。

2.本文针对单服务器多GPU的情况,请不要混淆。

如有错误,敬请指正!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/256529.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JS 转换数字为大写

1 function toUpper(n) {2 n n;3 var unit 十百千万;4 var num 一二三四五六七八九 ;5 var array new Array();6 for (var in.length; i > 0; i--){7 var numIndex parseInt(n.charAt(i-1))-1;8 if(n…

ANSYS——ANSYS后处理操作技巧与各类问题良心大总结

目录 1.ANSYS后处理时如何按灰度输出云图? 2 将云图输出为JPG 3.怎么在计算结果实体云图中切面? 4.非线性计算过程中收敛曲线实时显示 5.运用命令流进行计算时,一个良好的习惯是: 6.应力图中左侧的文字中,SMX与SMN分别代表最大值和最小值 7.在非…

容器的综合应用:文本查询程序

需求 程序读取用户指定的任意文本文件,允许用户从该文件中查找单词。查询结果是该单词出现的次数,并列出每次出现所在行,如果某单词在同一行中多次出现,程序将只显示该行一次。行号按升序显示,即第 7 行应该在第 9 行之…

Anaconda 安装操作及遇到的坑

最近刚用Pytorch,编译开源代码的时候发现缺少n个package,原来是之前在Anaconda3 创建的虚拟环境各自是独立的,tensorflow下安装的不能在别的环境下使用,所以要重新安装。然而关键是国内各种屏蔽资源,无法FQ去直接下载安…

IE浏览器历史版本图标大全

上个月IE团队庆祝了IE的15周岁生日, 并晒了晒IE各个历史版本的图标: Internet Explorer 1.0 图标 Internet Explorer 2.0 图标 Internet Explorer 3.0 图标 Internet Explorer 4.0 图标 Internet Explorer 5.0 图标 Internet Explorer 6.0 图标 Internet…

7.Mybatis关联表查询(这里主要讲的是一对一和一对多的关联查询)

视频地址:http://edu.51cto.com/sd/be679 在Mybatis中的管理表查询这里主要介绍的是一对一和一对多的关联查询的resultMap的管理配置查询,当然你也可以用包装类来实现。不过这里不说,做关联查询的步骤可以简单的总结为以下的几步:…

ANSYS——查看某一截面的云图分布(也叫做切片图)

1.确定截面的位置 此处以图中红色处截面为例 2.将工作平面经过坐标变化移动到指定截面处(工作平面的XY平面与截面重合) 工作平面坐标系默认是与总体坐标系重合的,这里是先平移再进行旋转

深度学习之keras (一) 初探

之前一段时间里,学习过tensorflow和Pytorch也写了点心得,目前是因为项目原因用了一段时间Keras,觉得很不错啊,至少从入门来说对新手极度友好,由于keras是基于tensoflow的基础,相当于tensorflow的高级API吧&…

swift:高级运算符(位运算符、溢出运算符、优先级和结合性、运算符重载函数)...

swift:高级运算符 http://www.cocoachina.com/ios/20140612/8794.html 除了基本操作符中所讲的运算符,Swift还有许多复杂的高级运算符,包括了C语和Objective-C中的位运算符和移位运算。 不同于C语言中的数值计算,Swift的数值计算默…

收集、报告或保存系统活动信息:sar命令

2019独角兽企业重金招聘Python工程师标准>>> 索引 sar命令的使用常用方法 查看网络设备(网卡)的状态信息查看socket使用情况查看cpu使用情况(默认)查看内存和交换空间使用情况查看内存的统计信息查看tty设备的活动状态查看等待运行的进程数和…

【GOF23设计模式】原型模式

【GOF23设计模式】原型模式 来源:http://www.bjsxt.com/ 一、【GOF23设计模式】_原型模式、prototype、浅复制、深复制、Cloneable接口 浅复制 1 package com.test.prototype;2 3 import java.util.Date;4 5 /**6 * 浅复制7 */8 public class Sheep implements C…

ANSYS——自定义的梁截面中心(法线节点)的偏置,详细全面

目录 1、ANSYS梁的确定 2.关于梁截面的一些名词 总体坐标系 梁截面坐标系 梁单元的坐标系

Deepfacelab 小白教程

不小心入了AI换脸的坑,但是感觉AI换脸很有意思,第一次感觉科研使我快乐。 目录 一、AI换脸软件简介 二、Deepfacelab下载安装 三、Deepfacelab Demo实现 四、Deepfacelab 填坑 五、总结 一、AI换脸软件简介 这个没有具体使用过,目前我…

Underscore.js 的模板功能

Underscore是一个非常实用的JavaScript库,提供许多编程时需要的功能的支持,他在不扩展任何JavaScript的原生对象的情况下提供很多实用的功能。 无论你写一段小的js代码,还是写一个大型的HTML5应用,underscore都能帮上忙。目前&…

ANSYS——查看剖面图的应力分布云图以及工作平面的相关设置

剖面图和切片图其实差不多,只是切片图只有一个截面,而剖面图是切去一部分保留另一部分模型,不但可以看到截面处应力分布还可以看到剩余模型的应力分布 切片应力云图可见:https://blog.csdn.net/qq_45769063/article/details/106357700 1.剖面云图的查看 首先将工作平面的…

2016.8.2

高端内存映射方式 高端内存映射分为三种:永久映射、临时映射和非连续动态内存映射。高端内存一般是指896MB以上的页框,这段区间内核一般不能直接访问。 1.永久映射 永久内核映射允许内核建立高端页框到内核地址空间的长期映射。它们使用主内核页表中的一…

深度学习之pytorch(三) C++调用

玩深度学习,个人觉得基于anaconda的python适合开发与测试,C适合实际的工程部署!而pytorch官方有编译好的libtorch,特别方便,适合于我这样的伸手党和手残党(win10下编译tensorflow编译了好久都没通过,好忧伤…

ANSYS入门——模态分析步骤与实例详解

目录 一、ANSYS求解模态分析步骤 建模 施加载荷和求解

javascript库之Mustache库使用说明

一、简单示例 代码: 1 function show(t) { 2 $("#content").html(t); 3 } 4 5 var view { 6 title: YZF, 7 cacl: function () { 8 return …

Light OJ 1007

求区间欧拉函数平方和。。。 最后因为longlong 范围爆了WA 了&#xff0c; 0.0 #include<bits/stdc.h> using namespace std; const int maxn 5000000 131; typedef unsigned long long LL;bool Com[maxn]; LL Num[maxn], Prim[maxn / 3]; int Cnt;void INIT() {Num[1]…