aistarter助手程序未运行_师傅领进门之6步教你跑通一个AI程序!

欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~

源码下载地址请点击原文查看。

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

内容提纲:

环境搭建

了解Tensorflow运行机制

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

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

tools 工具类

CPU & GPU & multi GPU

学习资料

1 环境搭建 (Windows)

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

创建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安装包:http://mirrors.oa.com/tensorflow/windows/cpu/tensorflow-1.2.1-cp35-cp35m-win_amd64.whl, cp35是指python3.5

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

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

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

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

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

pip install whl-file #http://mirrors.oa.com/tensorflow/windows/gpu/tensorflow_gpu-1.2.1-cp35-cp35m-win_amd64.whl

一些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

where = tf.where(logical, tf.fill(tf.shape(rgb),1.), tf.fill(tf.shape(rgb),5.))#True的位置赋值1,False位置赋值5

# 启动默认图.

# sess = tf.Session()

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())#变量初始化

result = sess.run(hello_world) #Fetch, 获取tensor运算结果

print(result, result.decode(), hello_world.eval())#`t.eval()` is a shortcut for calling `tf.get_default_session().run(t)`.

#out: b'Hello World!' Hello World! b'Hello World!' #前辍'b'表示bytestring格式,decode解成string格式

print(sess.run(hello, feed_dict={hello: ['Hello']}))

#out: ['Hello']

print(sess.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值