ubuntu16 配置ROS使用Python3.7完整过程 + 踩坑记录

Carla 0.9.13 与ROS bridge安装记录 Carla 0.9.13 ros bridge在Ubuntu 20.04及ros noetic的安装与配置 阅读详情

因为想用python3 + ros,
所以按照此处介绍的方法,得知cv_bridge是python2.7才支持的,
所以要用python3编译cv_bridge.

python版本选择

之前选了python3.8后踩坑
什么坑呢
注意到cakin配置时要用到python3.xm, 但是在网上查的说python3.8已经没有m了。。(就没有试下去)

catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m 
-DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.socatkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m 
-DPYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so

然后换成3.7,从源码安装

sudo apt update
sudo apt install build-essential zlib1g-dev libbz2-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
tar -xzvf Python-3.7.4.tgz

cd Python-3.7.4
./configure --prefix=/usr/local/python3.7  # 此处有坑,看第6条
sudo make
sudo make install

sudo ln -s /usr/local/python3.7/bin/python3.7 /usr/bin/python3
sudo ln -s /usr/local/python3.7/bin/pip3.7 /usr/bin/pip3
sudo ln -s /usr/local/src/python37/bin/python3.7m /usr/bin/python3.7m

第2坑,没有python3.7-dev
这个直接跳过了

接着踩,编译

这几步都还好的

pip3 install rospkg
pip3 install catkin-tools
mkdir py3_ros_ws && cd py3_ros_ws
mkdir src && cd src
## 下载vision_opencv 库
git clone https://github.com/ros-perception/vision_opencv
git checkout kinetic  #选择自己的ros版本

开始编译

cd py3_ros_ws
source /opt/ros/kinetic/setup.bash
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/local/python3.7/include/python3.7m 
-DPYTHON_LIBRARY=/usr/local/python3.7/lib/libpython3.7m.a #此处有坑,后面会变成.so,看第6条
# 注意选择对应的python3版本
catkin config --install  #此处有坑,看第8条
# 编译
catkin build cv_bridge

然后就开始报错,以下是曾经遇到的坑(不分先后)

ImportError: No module named ‘_ctypes’ when using Value from module multiprocessing
解决方法:

sudo apt-get install libffi-dev

2.找不到libboost_python.so

# locate libboost_python
/usr/lib/x86_64-linux-gnu/libboost_python-py27.a
/usr/lib/x86_64-linux-gnu/libboost_python-py27.so
/usr/lib/x86_64-linux-gnu/libboost_python-py27.so.1.58.0
/usr/lib/x86_64-linux-gnu/libboost_python-py35.a
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so
/usr/lib/x86_64-linux-gnu/libboost_python-py35.so.1.58.0

确实是没有叫libboost_python.so的,所以要自己做一个link
但是发现没有,这个只有python3.5的libboost_python, python3.7的去哪了?

解决方法:
安装boost,下载地址
我这里下载的是1.76的版本

注意了!ubuntu16的gcc版本是5.4,默认是不用c++11编译的,
所以需要打开 c++11 的支持, gcc5.4 下, 只打开 --std=c++11 是不能编译fiber的, 使用 --std=c++14 或者 --std=c++17 可以成功
cxxflags一定要写c++14或c++17, 不然后面用的时候会报错,很多函数都没有
参考链接

tar -xzf boost_1_76_0.tar.gz
cd boost_1_76_0
./bootstrap.sh --with-python=/usr/bin/python3 --with-python-version=3.7 --with-python-root=/usr/local/python3.7
./b2 cflags='-fPIC' cxxflags='--std-c++14' --with-python include="/usr/local/python3.7/include/python3.7m"
./b2 install

这样就安装好了,在/usr/local/lib下,
不要copy到/usr/lib/x86_64-linux-gnu下,不然用的时候会报warning还是error

cd /usr/lib/x86_64-linux-gnu
ln -s /usr/local/lib/libboost_python-py37.so libboost_python.so
ln -s /usr/local/lib/libboost_python-py37.a libboost_python.a

3.Command ‘(‘lsb_release’, ‘-a’)’ returned non-zero exit status 1.
解决方法

sudo mv /usr/bin/lsb_release /usr/bin/lsb_release_bak

4.CMake Error at /usr/local/lib/cmake/Boost-1.76.0/BoostConfig.cmake:240 (if):
if given arguments:

"ALL" "IN_LIST" "Boost_FIND_COMPONENTS"

Unknown arguments specified
Call Stack (most recent call first):
/usr/share/cmake-3.5/Modules/FindBoost.cmake:245 (find_package)
CMakeLists.txt:9 (find_package)

解决方法:
参考链接

5./usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
解决方法:
修改vision_opencv下的CmakeLists.txt,添加c++11的支持

cd vision_opencv/
find ./ -name CMakeList*

在每一个(除了python)的CmakeLists.txt里添加SET(CMAKE_CXX_FLAGS "-std=c++0x")

6.`_Py_NotImplementedStruct’ can not be used when making a shared object; recompile with -fPIC

这个很坑
为什么,因为要重新编译python3.7!
刚刚编译python时,仅用了./configure --prefix=/usr/local/python3.7,并没有fPIC模式
重新编译吧

 ./configure --prefix=/usr/local/python3 --enable-shared CFLAGS=-fPIC --enable-optimizations
 make
 make install

make完之后你会发现/usr/local/python3.7/lib下多了libpython3.7m.so(之前只有.a)
重新配置

cd py3_ros_ws
source /opt/ros/kinetic/setup.bash
catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/local/python3.7/include/python3.7m 
-DPYTHON_LIBRARY=/usr/local/python3.7/lib/libpython3.7m.so 

7./usr/bin/python3: error while loading shared libraries: libpython3.7m.so.1.0: cannot open shared object file: No such file or directory

此坑是上一坑的延续版,
上一坑python重新编译后,再build就出现此问题
解决方法:
添加环境变量

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/python3.7/lib

8.error: option --install-layout not recognized

上一坑的延续
需要把catkin config --install改为

catkin config --install -DCMAKE_BUILD_TYPE=Release -DSETUPTOOLS_DEB_LAYOUT=OFF
catkin build cv_bridge

在这里插入图片描述
build成功。

使用

为避免还用之前python2.7的cv_bridge, 移除之前的(也可以不移除,这里为了测试python3的能不能用)

cd /opt/ros/kinetic/lib/python2.7/dist-packages
mv cv_bridge cv_bridge_BK

python3的cv_bridge库安装在py3_ros_ws的install文件夹,需要使用的时候将这个环境变量导入即可

source devel/setup.bash --extend

roslaunch以后提示No module named ‘cv_bridge’
找到使用cv_bridge的python文件,
第一行改为

!/usr/bin/env python3

指明使用python3

记住要添加环境变量,不然又找不到libpython3.7m.so.1.0

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/python3.7/lib

然后ros就可以使用python3啦(之前python2.7下安装的要在python3下重新安装)

参考链接:
https://medium.com/@beta_b0t/how-to-setup-ros-with-python-3-44a69ca36674
https://www.jianshu.com/p/2f86607c98d1

Ubuntu16.04安装ROS kinetic踩坑全记录(apt-get出现依赖问题、python3与kinetic不兼容、安装ROS后不能导入cv2等 Ubuntu16.04安装ROS kinetic踩坑全记录(apt-get出现依赖问题、python3与kinetic不兼容、安装ROS后不能导入cv2等)使用ROS官网的安装命令sudo apt-get install ros-kinetic-desktop-full出现依赖问题完成官网给出的安装过程后,在python环境出现不能import cv2的问题 最近需要在实验室的服务器上安装ROS调试机械手,因为服务器的ubuntu版本是16.04,所以只能装kinetic版的ROS,过程中出现了很多坑,记录 阅读详情

相关推荐

Ubuntu16.04中PL-SLAM的N次踩坑安装总结

Ubuntu16.04中PL-SLAM的N次踩坑安装总结 PL-SLAM安装Ubuntu16.04中PL-SLAM的N次踩坑安装总结1.安装思路参考链接2.ROS安装2.1 删除init文件2.2 update失败解决3.OpenCV安装3.1 opencv3.3.1安装过程3.2 安装所需要依赖3.3 gtk版本冲突问题3.4 opencv xfeatures2d模块缺失boostdesc_bgm.i文件3.5 ippicv下载问题3.6 opencv2/xfeatures2d/cuda.hpp: No

qq_39915898的博客 3663

【Linux】python多版本安装

源码安装Python3.7 首先在官网下载所需要的python版本,格式为tgz https://www.python.org/downloads/release/python-371/ 下载完成后在解压的后的跟目录下输入 ./configure --with-ssl make sudo make install 编译安装时需要提前安装好所需的依赖文件,否则会在make那一步报错。按照它提示的缺少信息安装即可 sudo apt-get install libbz2-dev libc6-dev libffi

uuuuur的博客 826

ubuntu16.04环境下解决ros工作空间编译出现python版本问题导致编译失败(附带自由切换python版本)

博主一直在ros环境下工作,由于在电脑上装了anaconda3和深度学习框架pytorch,再次到ros的工作空间下编译时发现python报错。虽然我都是基于c++开发的,但是在ros的工作空间下会发现有python2.7的一个文件夹,在/devel/lib/python2.7/dist-packages路径下会出现工作空间的功能包,会有功能包使用的msg的.py文件,具体是干什么用的,没有搞清楚...

EricLee23 3712

关于ubuntu16.04,python3.7,RTX2080,caffe最后一步的踩坑攻略

从0开始到安装openCV均可参考 https://blog.csdn.net/pengchengliu/article/details/90749483 这篇博客 本文主要对caffe编译,配置,以及python接口的成功调用做补充 一、安装Nvidia显卡驱动 1.到官网上下载自己GPU对应版本的显卡驱动。 下载地址:https://www.nvidia.cn/Download/index.a...

thomaswu1992的博客 1332

ubuntu16.04 + ros 各种坑

apt-get update出现错误 1)改了源 https://blog.csdn.net/Coderii/article/details/87738211 2)把其他网址都勾掉 2. 安装ros报错 ImportError: No module named ‘rosdep2’ 解决办法:https://blog.csdn.net/p942005405/article/details/8797...

DJ_HEY的博客 1031

Ubuntu16.04安装ROS kinetic过程记录及问题解决

官方安装教程:http://wiki.ros.org/kinetic/Installation/Ubuntu https://blog.51cto.com/laok8/2485703 https://blog.csdn.net/nanianwochengshui/article/details/105702188

weixin_44711603的博客 707

【Ubuntu & ROS】Ubuntu16.04使用Python3编译cv_bridge

一、前言 近期在做项目时,用到cv_bridge包和Python3的环境。cv_bridge是ROS库中用来将深度相机传感器图片数据类型转换成opencv的mat数据类型的功能包。在系统自带的ROS库中,cv_bridge编译用的是python2。使用过Python编程的小伙伴都知道,Python2和3存在很大差异,所以为满足项目需要,笔者使用Python3编译cv_bridge并进行记录。 二、配置过程 1.前期配置和创建工作区间并生成src子文件夹 前期配置 # 先将Python2的cv2.so环境

Up_Long的博客 1101

如何在Ubuntu系统里面运行python的代码

在Ubuntu系统中,你可以使用快捷键“Ctrl + Alt + T”来打开终端。这将运行你的Python代码。如果你的代码有任何输出,它们将会显示在终端中。按下"Ctrl + X"键,然后会出现一个提示,询问你是否要保存改动。确认文件名后,按下"Enter"键,即可保存并退出nano编辑器。如果你已经安装了Python,则会显示出Python的版本号。这会打开一个新文件,你可以在其中编写你的Python代码。如果你已经完成了代码编写,按下"Y"键来确认保存。然后,会出现另一个提示,询问你要保存的文件名。

weixin_47869094的博客 2万+

树莓派4B, ubuntu20.04, 安装Ros Noetic[踩坑记录]

树莓派4B ubuntu20.04 安装 ros noetic, 全面踩坑

bing_feilong的博客 1657

ROS和深度学习环境共存1---Ubuntu18.04下利用conda虚拟环境实现ROS Melodic编译python3的cv_bridge踩坑全记录

众所周知ros依赖python2.7环境,而深度学习最低要求是python3.x。因此在实现的过程中我遇到很多关于ros catkin_make失败,python路径找不到,以及OpenCV-python找不到版本,cv2不能import等等问题。我自认为是由于ros和深度学习的python版本或者其他包不兼容的事情。再说一下我想要实现的目标,以及为什么非要实现。第一:我想利用conda创建一个虚拟环境,并在虚拟环境中完成ROS Melodic编译python3的cv_bridge。

LZL2020LZL的博客 1905

关于三维全波电磁场仿真软件CST的安装和问题解决

百度网盘链接:https://pan.baidu.com/s/1RgWr8Qau1EzSOlh29SewFQ提取码:fzza这里是2023版本的CST,需要其他版本的uu们可以去官网或者其他人的网盘下载。

weixin_60728441的博客 3599

ubuntu16.04+ROS+tensorflow+object detection API配置

以下将是我心酸的安装历程,不保证都能顺利成功,毕竟每个人遇到的问题都不一样。但是,应该回给各位安装过程中出现错误提供一种安装思路。 安装前提,ubuntu16.04+ROS(kinetic) 一、安装tensorflow 安装pip并升级 sudo apt-get install python-pip python-dev sudo -H pip install --upgrade pip 这...

weixin_42058099的博客 1340

离合器KP点自学习:从原理到工程实践的全景解析

本文全面解析了离合器KP点自学习的原理与工程实践,详细介绍了KP点的物理本质、控制价值及自学习方法。通过扭矩增量式和转速变化式学习法,结合不同变速箱构型的适配策略,提升离合器控制的精度与效率。文章还探讨了液压系统迟滞补偿和摩擦材料老化应对方案,为工程实践提供可靠保障。

weixin_33717117的博客 286

Ubuntu16.04 配置ros+pytorch+cv2

Ubuntu16.04 配置ros+pytorch+cv2 最近安装这个环境,踩了很多坑,备份一下。 1、首先明确ros不一定完美支持python3,所以暂时先装在python2里。不过py2好像2021年停用。 2、如果想在python3搭环境,就确保pip3和所用的python3环境是否相同,可以通过pip3 -V查看。如果不相同的话,用软链接相连一下,这个网上有很多的教程,但注意要找到自己文件的位置。 3、要提前安装好驱动和cuda,N卡驱动可以在设置里面的其他驱动下载(需要重启),cuda我安装的是

Michelle_jx的博客 1248

YOLOv8中的C2f模块代码详解

在YOLOv8网络结构中,C2f模块(CSP Bottleneck with 2 Convolutions)是一个关键组件,用于实现跨阶段部分聚合

zzzyyy8的博客 3万+

PX4飞控CAN总线通信深度配置:从menuconfig到驱动加载的完整指南

本文详细解析了PX4飞控中CAN总线通信的深度配置流程,从menuconfig选项设置到驱动加载的完整指南。涵盖硬件抽象层、驱动层和应用层的配置细节,提供实际验证方法和故障排查技巧,帮助开发者高效实现CAN通信,特别适用于无人机和机器人底盘运动控制等场景。

weixin_30374009的博客 498

ubuntu16.04安装kitti2bag 详细过程

sudo yum -y instal python2-pip sudo apt install yum sudo wget https://bootstrap.pypa.io/get-pip.py chen@chen-QiTianM415-D070:~$ sudo python get-pip.py ERROR: This script does not work on Python 2.7 The minimum supported Python version is 3.7. Please use ht

AI Chen的博客 2670

使用 Unity Sentis 和 Compute Shader,det_10g.onnx 进行高效人脸五官定位

在计算机视觉领域,人脸五官定位是一个重要的任务。本文将介绍如何使用 Unity Sentis 和 Compute Shader,结合模型,实现高效的人脸五官定位。我们将详细讲解每一步骤,并提供完整的代码示例。

m0_55632444的博客 2392

ROS 从安装到机械臂的仿真与实验

ROS 从安装到机械臂的仿真与实验前言1. Ubuntu+ROS系统的搭建a) 在VMware虚拟机中安装Ubuntu 16.04;b) 在Ubuntu16.04LTS 中安装ROS(版本Kinetic):2. ROS工作原理与Node通讯机制:a) 首先,什么是ROS:b) ROS通过Package来管理所需的文件,通常一个ROS Package包含以下文件或文件夹:c) ROS nodes(节...

MuYang的博客 3万+

随机子空间集成方法原理与scikit-learn实践

集成学习通过组合多个基学习器的预测结果来提升模型性能,是机器学习中的核心方法之一。随机子空间方法作为集成学习的重要变体,其核心原理是通过对特征空间进行随机子采样来增加模型多样性。这种方法特别适合处理高维数据,能有效缓解维度灾难问题,常与决策树、K近邻等对特征选择敏感的基学习器配合使用。在scikit-learn中,可以通过BaggingClassifier和BaggingRegressor实现随机子空间集成,关键参数包括max_features和n_estimators。实际应用中,该方法在金融风控、医疗诊

weixin_33773084的博客 376

KiCad插件安装太慢?试试华秋版,再手把手教你装这5个实用插件

本文介绍了KiCad华秋发行版如何解决插件安装慢的问题,并详细推荐了5个实用插件及其安装方法。华秋版优化了国内用户的下载速度,预装了常用扩展包,包括BOM生成器、3D模型增强工具等,显著提升PCB设计效率。

weixin_29184075的博客 547

ROS那些坑

ROS那些坑 在此开一个专题,用来记录在使用ROS过程中遇到的问题(大坑)以及解决方法。 安装过程中的一些坑 我的是Ubuntu16.04,ROS安装对应版本kinetic。安装直接参考ROS官网:http://wiki.ros.org/kinetic/Installation 安装过程中出现的问题: 在进行sudo rosdep init的时候,出现了DistributionNotFound: The ‘rosdep==0.19.0’ distribution was not found and is r

图像处理、SLAM、PCL、OpenCV爱好者 1053

ABAP 7.40 新语法介绍系列之六:使用 FILTER 关键字对 ABAP 内表进行过滤

笔者之前写过的关于 ABAP 740 新语法的文章:本文介绍另一个新的关键字:FILTER.FILTER 是 ABAP 740 中引入的一个关键字,用于简化数据操作,它能够将内部表(Internal Table)中的数据按特定的条件进行过滤,并生成一个新的内部表。FILTER 的引入旨在使代码更加现代化和高效,尤其是在面对大量数据需要过滤的情况下。传统上,开发人员可能需要使用 LOOP AT 结合条件判断来实现,但使用 FILTER 关键字后,代码显得更加简洁且易于维护。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1167

ROS安装一路趟过的坑[Ubuntu 16.04 LTS]

系统:ubuntu 16.04 LTS ROS: kinetic 安装教程:官方教程 坑1:配置 Ubuntu 软件仓库时没有选择速度最快的源 下图中已经选择了自动测速时最优的源 坑2:没有使用推荐的国内或新加坡镜像 解决方法: 选择国内或新加坡镜像源替换下图框中的命令。(博主本人使用的新加坡镜像源) 坑3:执行sudo apt-get update时报错 安了好几遍,这一步报了很多次错误,但...

mingcheng2650的博客 415

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

【记录】在新电脑安装Ubuntu16.04系统并移植虚拟机中catkin_ws过程

由于实验室配置了新电脑,故将原来在虚拟机的catkin_ws内容移植到新电脑中ubuntu系统下,并安装驱动。过程中踩了很多坑,在此记录一下,便于日后有所参考。 一、windows10安装ubuntu16.04双系统(无U盘) 参考:(5条消息) WIN10下免U盘安装Ubuntu双系统_依依妖妖的林少的博客-优快云博客https://blog.csdn.net/Lin3119333/article/details/99630418我的电脑是单硬盘,但是操作类似。除了分出5G存储安装文件,我分出了约1

qq_41368544的博客 5051

深度学习工具包 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&#39;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(&#39;DeepLearnToolbox&#39;));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(&#39;state&#39;,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&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,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 = &#39;sigm&#39;;%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, &#39;Too big error&#39;);```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(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;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)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;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, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% 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&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 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, &#39;Too big error&#39;);```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(&#39;state&#39;,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, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,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, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,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, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % 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, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % 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, &#39;Too big error&#39;);%% 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(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % 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, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: leetcode 606. Construct String from Binary Tree(从二叉树建立字符串)
下一篇: Building wheel for opencv-python (PEP 517) 卡住
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值