机器学习入门之HelloWorld(Tensorflow)

作者:媒体转发 时间:2018-04-01 01:13

字号
沙龙活动 | 3月31日 京东、微博、华为实战专家与你共同探讨容器技术实践!

源码下载地址:https://share.weiyun.com/a0c1664d334c4c67ed51fc5e0ac5f2b2

初学机器学习,写篇文章mark一下,希望能为将入坑者解点惑。本文介绍一些机器学习的入门知识,从安装环境到跑通机器学习入门程序MNIST demo。

机器学习入门之HelloWorld(Tensorflow)

内容提纲:

环境搭建

了解Tensorflow运行机制

MNIST(手写数字识别 ) softmax性线回归

MNIST 深度卷积神经网络(CNN)

tools 工具类

CPU & GPU & multi GPU

学习资料

1 环境搭建 (Windows)

安装虚拟环境 Anaconda,方便python包管理和环境隔离。

Anaconda3 4.2 https://www.anaconda.com/downloads,自带python 3.5。

创建tensorflow隔离环境。打开Anaconda安装后的终端Anaconda Prompt,执行下面命令

conda create -n tensorflow python=3.5 #创建名为tensorflow,python版本为3.5的虚拟环境 

activate tensorflow #激活这个环境   

deactivate #退出当前虚拟环境。这个不用执行 

CPU 版本

pip install tensorflow #通过包管理来安装 

pip install whl-file #通过下载 whl 文件安装,tensorflow-cpu安装包:, cp35是指python3.5 

GPU 版本。我的笔记本是技持NVIDIA显卡的,可以安装cuda,GPU比CPU快很多,不过笔记本的显存不大,小模型还可以跑,大模型建议在本地用CPU跑通,到Tesla平台上训练。

注意点:选择正确的 CUDA 和 cuDNN 版本搭配,不要只安装最新版本,tensorflow可能不支持。

目前Tensorflow已支持到CUDA 9 & cuDNN 7,之前本人安装只支持CUDA 8 & cuDNN 6,所以用是的:

CUDA8.1 https://developer.nvidia.com/cuda-80-ga2-download-archive

cudnn 6 https://developer.nvidia.com/cudnn ,将cudnn包解压,把文件放到cuda安装的对应目录中,C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0,bin对应bin,include对应include,再添加bin目录到环境变量path中。

pip install tensorflow-gpu #通过包管理来安装 

pip install whl-file #  

一些python工具包安装。用到啥安啥就行, pip install,不行就找源码编译安装

(tensorflow) D:\> pip install opencv-python   #opencv, tensoflow 虚拟环境中 

(tensorflow) D:\> pip install scipy    #图片读取写入,scipy.misc.imread 

(tensorflow) D:\> pip install Pillow   #PIL/Pillow,这里有个坑,压缩过的PNG图,在1.x版本解析会出现透明通道质量下降,升级 

2 了解Tensorflow运行机制

上代码。注意注释说明

import tensorflow as tf 

hello_world = tf.constant('Hello World!', dtype=tf.string) #常量tensor 

print(hello_world) #这时hello_world是一个tensor,代表一个运算的输出 

#out: Tensor("Const:0", shape=(), dtype=string) 

hello = tf.placeholder(dtype=tf.string, shape=[None])#占位符tensor,在sess.run时赋值 

world = tf.placeholder(dtype=tf.string, shape=[None]) 

hello_world2 = hello+world #加法运算tensor 

print(hello_world2) 

#out: Tensor("add:0", shape=(?,), dtype=string) 

#math 

x = tf.Variable([1.0, 2.0]) #变量tensor,可变。 

y = tf.constant([3.0, 3.0]) 

mul = tf.multiply(x, y) #点乘运算tensor 

#logical 

rgb = tf.constant([[[255], [0], [126]]], dtype=tf.float32) 

logical = tf.logical_or(tf.greater(rgb,250.), tf.less(rgb, 5.))#逻辑运算,rgb中>250 or <5的位置被标为True,其它False 

责任编辑:CQITer新闻报料:400-888-8888   本站原创,未经授权不得转载
继续阅读
热新闻
推荐
关于我们联系我们免责声明隐私政策 友情链接