docker 中使用gpu

配置docker 支持GPU方法(Nvidia GPU) 系统版本:ubuntu 23.04。 阅读详情
AI摘要

该文详细描述了如何在Ubuntu系统中配置Docker,以便在容器内使用GPU和CUDA进行计算。首先,通过安装nvidia-container-toolkit解决GPU驱动问题,然后解决CUDNN库的软链接错误,最后成功运行了一个包含PyTorch的NVIDIADocker镜像,验证了GPU支持。

  • 普通docker的使用
  • 注:此方法实用于linux系统

docker gpu

  • 尝试运行docker run --gpus=all -it --net=host --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 nvcr.io/nvidia/pytorch:22.05-py3 bash

  • 如果没有正确配置会报错:could not select device driver “” with capabilities: [[gpu]].

  • 以下是配置的过程

  • apt-get install nvidia-container-runtime:nvidia-container-runtime 是 NVIDIA 提供的一个运行时环境,允许 Docker 容器访问 NVIDIA GPU。

  • distribution=$(. /etc/os-release;echo $ID$VERSION_ID):读取当前操作系统的版本信息,并存储到 distribution 变量中

  • curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -

  • Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
    OK

  • curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list

  • deb https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/KaTeX parse error: Expected 'EOF', got '#' at position 10: (ARCH) / #̲deb https://nvi…(ARCH) /
    deb https://nvidia.github.io/nvidia-container-runtime/stable/ubuntu18.04/KaTeX parse error: Expected 'EOF', got '#' at position 10: (ARCH) / #̲deb https://nvi…(ARCH) /
    deb https://nvidia.github.io/nvidia-docker/ubuntu18.04/$(ARCH) /

  • sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit

正在设置 libnvidia-container1:amd64 (1.12.0-1) ...
正在设置 libnvidia-container-tools (1.12.0-1) ...
正在设置 nvidia-container-toolkit (1.12.0-1) ...
正在处理用于 libc-bin (2.35-0ubuntu3.1) 的触发器 ...
/sbin/ldconfig.real: /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8 is not a symbolic link

/sbin/ldconfig.real: /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_train.so.8 is not a symbolic link

/sbin/ldconfig.real: /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8 is not a symbolic link

/sbin/ldconfig.real: /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8 is not a symbolic link

/sbin/ldconfig.real: /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn.so.8 is not a symbolic link

/sbin/ldconfig.real: /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_train.so.8 is not a symbolic link

/sbin/ldconfig.real: /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8 is not a symbolic link
  • 可能原因:/usr/local/cuda-11.4/lib64/ 和 /usr/local/cuda-11.4/targets/x86_64-linux/lib是链接关系,再使用copy方法安装cudnn时,部分软连接文件没有拷贝到cuda-11.4/lib64/文件目录

在这里插入图片描述

# 解决方案
$ sudo ln -sf  /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.8.0  /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8
$ sudo ln -sf /usr/local/cuda-11.2/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.8.0  /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_train.so.8
$ sudo ln -sf /usr/local/cuda-11.2/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.8.0  /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8
$ sudo ln -sf /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.8.0  /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8
$ sudo ln -sf /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn.so.8.8.0 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn.so.8
$ sudo ln -sf /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.8.0 /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_ops_train.so.8
$ sudo ln -sf /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.8.0  /usr/local/cuda-11.4/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8
  • sudo systemctl restart docker
(base) pdd@pdd-Dell-G15-5511:~$ sudo docker run --gpus=all --rm -it --net=host --ipc=host  --ulimit memlock=-1 --ulimit stack=67108864  nvcr.io/nvidia/pytorch:22.05-py3 bash

=============
== PyTorch ==
=============

NVIDIA Release 22.05 (build 37432893)
PyTorch Version 1.12.0a0+8a1a93a

Container image Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

Copyright (c) 2014-2022 Facebook Inc.
Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert)
Copyright (c) 2012-2014 Deepmind Technologies    (Koray Kavukcuoglu)
Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu)
Copyright (c) 2011-2013 NYU                      (Clement Farabet)
Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston)
Copyright (c) 2006      Idiap Research Institute (Samy Bengio)
Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz)
Copyright (c) 2015      Google Inc.
Copyright (c) 2015      Yangqing Jia
Copyright (c) 2013-2016 The Caffe contributors
All rights reserved.

Various files include modifications (c) NVIDIA CORPORATION & AFFILIATES.  All rights reserved.

This container image and its contents are governed by the NVIDIA Deep Learning Container License.
By pulling and using the container, you accept the terms and conditions of this license:
https://developer.nvidia.com/ngc/nvidia-deep-learning-container-license

WARNING: CUDA Minor Version Compatibility mode ENABLED.
  Using driver version 470.161.03 which has support for CUDA 11.4.  This container
  was built with CUDA 11.7 and will be run in Minor Version Compatibility mode.
  CUDA Forward Compatibility is preferred over Minor Version Compatibility for use
  with this container but was unavailable:
  [[Forward compatibility was attempted on non supported HW (CUDA_ERROR_COMPAT_NOT_SUPPORTED_ON_DEVICE) cuInit()=804]]
  See https://docs.nvidia.com/deploy/cuda-compatibility/ for details.

root@pdd-Dell-G15-5511:/workspace# nvidia-smi
Sat Mar 11 05:45:06 2023       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.161.03   Driver Version: 470.161.03   CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |

CG

  • https://stackoverflow.com/questions/25185405/using-gpu-from-a-docker-container
  • 在这里插入图片描述
Docker 入门到精通+调用GPU docker 封装 阅读详情

相关推荐

Docker中调用NVIDIA显卡的完整避坑指南:从--gpus参数到NVIDIA_DRIVER_CAPABILITIES

本文是一份在Docker容器中调用NVIDIA显卡的完整避坑指南。文章深度解析了从基础的`--gpus`参数使用到关键的`NVIDIA_DRIVER_CAPABILITIES`环境变量配置,系统性地梳理了GPU加速的配置路径与常见问题解决方案,帮助开发者和运维人员高效部署AI与高性能计算应用。

weixin_28826521的博客 540

docker中使用gpu

本机想要启用gpu加速计算,需要由一张多余的nVidia显卡。需要提前禁用nouveau:lsmod | grep nouveau没有输出即禁用了 需要安装1、显卡驱动、2、cuda库(安装cuda会自动安装显卡驱动)3、cudnn(深度神经网络的GPU加速库,需要神经网络则安否则可以不安) 安装完成后,可以运行nvidia-smi查看GPU设备的状态。

qq_42152032的博客 1万+

如何在docker容器中使用GPU以及使用pytorch

最近刚接触docker,如有不当,请指出。 1. 首先要解决在docker中可以使用GPU的问题。 官方给出了解决办法:https://github.com/NVIDIA/nvidia-docker 具体操作如下: step 1:确认本机可以检测到NVIDIA卡,通过下列命令看自己GPU卡的型号。 lspci -vv | grep -i nvidia step 2:新建nvidia-container-runtime-script.sh文件 touch nvidia-container-ru

六十英里的专栏 1万+

OpenCV与AI深度学习 | 如何在 Docker 容器中使用 GPU

在计算机上配置 GPU 可能非常困难。配置步骤会根据计算机的操作系统和计算机所具有的 NVIDIA GPU 类型而变化。更难的是,当 Docker 启动容器时,它几乎需要从头开始。在本文中,我们将介绍在 Docker 容器内访问机器 GPU 所需的步骤。

csdn_xmj的博客 1521

k8s调用gpu,nvidia-docker使用gpu

简单介绍了nvidia-docker,介绍了k8s如何调用gpu

flyingbird 1万+

window下的docker内使用gpu

Windows 上使用 Docker GPU需要进行一系列的配置和步骤。这是因为 Docker 在 Windows 上的运行环境与 Linux 有所不同,需要借助 WSL 2(Windows Subsystem for Linux 2)和 NVIDIA Container Toolkit 来实现 GPU 的支持。

俞兆鹏的博客 2534

docker使用GPU总结

docker使用GPU总结      (注:本文只讨论docker19使用gpu,低于19的请移步其他博客,或更新19后再参考本文) 背景及基本介绍   感觉docker已是toB公司的必备吧,有了docker再也不用担心因客户环境问题导致程序各种bug,也大大省去了配置客户服务器的过程。   现由于项目要使用GPU,所以需要docker支持GPU,在docker19以前都需要单独下载nvidia...

正在学习的Lee的博客 4万+

Docker中GPU的使用指南

在Docker中使用GPU可以大大提升计算密集型任务的性能,特别是在深度学习和高性能计算领域。通过正确安装NVIDIA Container Toolkit并合理配置Docker容器,你可以充分利用GPU的计算能力。希望本文能够帮助你更好地在Docker中使用GPU,加速你的开发和部署过程!

俞兆鹏的博客 1681

docker中使用gpu_docker gpu

为了使docker image能很便利的使用Nvidia GPU,从而产生了nvidia-docker,由它来制作nvidia driver的image。nvidia-docker是一个可以使用GPU的docker,nvidia-docker是在docker上做了一层封装,通过nvidia-docker-plugin,然后调用到docker。需要安装:1、docker 2、显卡驱动(nvidia driver) 和cuda(安装cuda时会自动安装显卡驱动)。

2401_87287700的博客 1435

CGNS快速入门到实战(二):VS配置调用CGNS静态链接库

CGNS快速入门到实战这一系列分成三篇文章展开叙述,具体有:(一)CGNS静态链接库编译;(二)VS配置调用CGNS静态链接库;(三)CGNS文件及案例展示。本篇文章重点讲述VS配置调用CGNS静态链接库。

ahang_99的博客 2620

STM32F103中GPIO_Remap1_CAN1与GPIO_Remap2_CAN1的配置差异与应用场景解析

本文深入解析了STM32F103中GPIO_Remap1_CAN1与GPIO_Remap2_CAN1的配置差异与应用场景。通过剖析AFIO_MAPR寄存器,明确了两者在引脚映射(PB8/PB9 vs PD0/PD1)和适用封装上的区别,并提供了详细的配置步骤、常见陷阱排查及基于项目需求的选型指南,帮助开发者高效完成CAN引脚重映射。

u6v7w8x的博客 398

Docker使用GPU

一、docker使用宿主机硬件设备的三种方式 使用--privileged=true选项,以特权模式开启容器 使用--device选项 使用容器卷挂载-v选项 二、docker使用gpu方式演变 docker使用宿主机的gpu设备,本质是把宿主机使用gpu时调用的设备文件全部挂载到docker上。nvidia提供了三种方式的演变,如下是官网的一些介绍 来自 <Enabling GPUs in the Container Runtime Ecosystem | ..

weixin_38420154的博客 1万+

.NET+Vue企业级RBAC权限平台:开箱即用的生产就绪方案

RBAC(基于角色的访问控制)是企业系统安全治理的基础模型,其核心在于将用户、角色与权限解耦,通过动态能力组合实现细粒度管控。在真实工程中,RBAC需突破静态角色限制,支持时间维度授权、前后端双重鉴权、行级数据范围控制等关键能力。本文聚焦于一套已落地金融、政务、制造等多行业的生产级实现——基于.NET Minimal API与Vue 3的前后端分离架构,提供菜单/按钮/接口/数据四层权限控制,内置滑动过期JWT、Redis权限缓存、动态路由加载与权限指令等开箱即用能力,显著降低权限配置与审计成本。

weixin_33326218的博客 285

docker中使用gpu_docker gpu(1)

注意:本机想要启用gpu加速计算,需要由一张多余的nVidia显卡。需要提前需要安装1、显卡驱动、2、cuda库(安装cuda会自动安装显卡驱动)3、cudnn(安装完成后,可以在本机运行nvidia-smi查看GPU设备的状态。

2401_87287480的博客 1454

Fluent动网格+滑移网格+重叠网格对比介绍(以震荡水翼flapping foil为例)

初学fluent笔记,本文主要包含: Flapping foil在fluent内的几种实现方式 在哪设置动网格、滑移、重叠网格 flapping的UDF 示例 写在前面:Fluent在预览网格运动/提交计算之前,注意保存一个cas版本。 -------------------------Flapping foil 网格几种组合实现方式:---------------------- 直接重叠网格,overset, 需要建立两个几何part,part要重合。水翼局部区域(component),背景网格(ba

深海民工CFDer 2万+

10X Visium空间转录组学入门:从芯片结构到数据分析全流程解析(附避坑指南)

本文全面解析10X Visium空间转录组学技术,从芯片物理结构、探针化学原理到湿实验全流程操作与避坑指南,并详细介绍了从FASTQ数据到空间可视化的核心分析流程。文章旨在帮助科研人员跨越从理论到实操的鸿沟,掌握从样本准备、文库构建到使用Space Ranger和Seurat进行数据分析与可视化的完整路径,为组织生物学研究提供清晰的实践指引。

weixin_29306011的博客 171

Docker 使用GPU

配置GPU版本Docker 一、安装Docker 更新apt 包的索引 : $ sudo apt-get update 把Docker仓库加进到apt里: $ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common 添加Docker的官方GPG key: $ curl -fsSL https

婷婷 1391

网络设备心跳线带宽高导致虚拟化平台不稳定的问题处理

本文记录了网络设备心跳线带宽高导致虚拟化平台不稳定的问题处理一例。

forestqq的博客 2232

win10 Docker Desktop使用GPU

文章目录前言一、下载Docker以及安装二、安装WSL21.wsl22.安装Linux子系统。3.MobaXterm(可选,我只是感觉操作更方便)4.docker配置ubuntu20.04 LTS三、GPU的使用1.显卡驱动下载2. CUDA For WSL总结 前言 本文主要记录win10系统上Docker使用GPU的全部过程。对于原理不做解释,只是实操。 1、下载Docker Desktop 2、配置驱动 3、WSL2 4、使用GPU测试 一、下载Docker以及安装 Docker下载地址:dock

clownwai的博客 2万+

手把手教你用TLP350驱动IGBT:光耦隔离驱动电路完整设计教程

本文详细介绍了如何使用TLP350光耦隔离驱动电路设计IGBT驱动方案。从TLP350芯片特性分析到驱动电路架构设计,包括信号调理、自举电源和栅极控制网络,再到PCB布局优化和保护电路设计,提供了完整的实战指南。通过实测波形分析和故障排查,帮助工程师快速掌握IGBT驱动电路的设计要点和进阶技巧。

weixin_30566063的博客 849

YOLOv11改进 | 细节涨点篇 | DySample一种最新的轻量化动态上采样算子(效果完爆CARAFE)

本文给大家带来的最新改进机制是一种号称超轻量级且有效的动态上采样器——DySample。与传统的基于内核的动态上采样器相比,DySample采用了一种基于点采样的方法,相比于以前的基于内核的动态上采样器,DySample具有更少的参数、浮点运算次数、GPU内存和延迟。此外,DySample在包括语义分割、目标检测、实例分割、全景分割和单目深度估计在内的五个预测任务中,性能均优于其他上采样器(截至目前最有效的上采样算子),

Snu77的博客 4225

快速搭建GPU环境 | docker、k8s中使用gpu

device-plugin 以 DaemonSet 方式运行到集群各个节点,以感知节点上的 GPU 设备,从而让 k8s 能够对节点上的 GPU 设备进行管理。nvidia-container-runtime 在中间拦截了容器 spec,就可以把 gpu 相关配置添加进去,再传给 runC 的 spec 里面就包含 gpu 信息了。调用链从 containerd --> runC 变成 containerd --> nvidia-container-runtime --> runC。

weixin_48711696的博客 4817

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

在docker容器中使用GPU

在docker容器中使用GPU

zhaoyi40233的博客 7986

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Docker Desktop 中使用GPU

Docker Desktop 中使用GPU 2021-06-12 端午放假,没有回家,配置了一下环境,实现了在Windows 系统下 Docker Desktop 容器中使用GPU。记录一下过程。 据说在docker 容器内使用GPU属于显卡透传。 环境需求和软件版本 我的环境如下: win10 版本21390 WSL2 5.4.72 Docker Desktop 20.10.7 CUDA driver for WSL2 470.76,同时 CUDA Version: 11

chenxizhan1995的博客 4万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: docker 常用命令
下一篇: leetcode876.链表的中间结点 快慢指针
FakeOccupational
博客等级 码龄6年 2万+粉丝 1729原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址