深度学习框架TensorFlow入门-windows10系统

1.环境:

  • windows10系统
  • python3.5
  • Nvidia显卡(cpu版本不需要)

(建议安装anaconda集成环境,conda create -n py35 python=3.5 anaconda——(安装python3.5版本,并起名为py35,activate py35激活python3.5)

CUDA:它所作用的对象是显卡,也就是GPU,有了它,程序开发人员就能够编码控制和使用GPU了,cuda8

CUDNN 是CUDA Deep Neural Network library 的缩写,也就是基于CUDA的神经网络软件包。可以类比JAVA中的JDK,也可以类比开源图像处理工具包openCV。cudnn6

2.安装

一键安装(CPU版):

pip install –upgrade –ignore-installed tensorflow

一键安装(GPU版):

pip install –upgrade –ignore-installed tensorflow-gpu

经过考虑到gpu不是很高级,所以就该为安装cpu版本:

《深度学习框架TensorFlow入门-windows10系统》 图片.png

成功:

《深度学习框架TensorFlow入门-windows10系统》 图片.png

3.使用tensorboard

TensorFlow自带的一个强大的可视化工具

3.1用法(实例)

打开python编辑器,新建文件命名:tensorboard-demo.py,输入下列代码:

# -*- coding: utf-8 -*-
"""
Created on Fri Jan 19 14:56:07 2018

@author: JayMo
"""

import tensorflow as tf
import numpy as np

#输入数据
x_data = np.linspace(-1,1,300)[:, np.newaxis]
noise = np.random.normal(0,0.05, x_data.shape)
y_data = np.square(x_data)-0.5+noise

#输入层
with tf.name_scope('input_layer'): #输入层。将这两个变量放到input_layer作用域下,tensorboard会把他们放在一个图形里面
    xs = tf.placeholder(tf.float32, [None, 1], name = 'x_input') # xs起名x_input,会在图形上显示
    ys = tf.placeholder(tf.float32, [None, 1], name = 'y_input') # ys起名y_input,会在图形上显示

#隐层
with tf.name_scope('hidden_layer'): #隐层。将隐层权重、偏置、净输入放在一起
    with tf.name_scope('weight'): #权重
        W1 = tf.Variable(tf.random_normal([1,10]))
        tf.summary.histogram('hidden_layer/weight', W1)
    with tf.name_scope('bias'): #偏置
        b1 = tf.Variable(tf.zeros([1,10])+0.1)
        tf.summary.histogram('hidden_layer/bias', b1)
    with tf.name_scope('Wx_plus_b'): #净输入
        Wx_plus_b1 = tf.matmul(xs,W1) + b1
        tf.summary.histogram('hidden_layer/Wx_plus_b',Wx_plus_b1)
output1 = tf.nn.relu(Wx_plus_b1)

#输出层
with tf.name_scope('output_layer'): #输出层。将输出层权重、偏置、净输入放在一起
    with tf.name_scope('weight'): #权重
        W2 = tf.Variable(tf.random_normal([10,1]))
        tf.summary.histogram('output_layer/weight', W2)
    with tf.name_scope('bias'): #偏置
        b2 = tf.Variable(tf.zeros([1,1])+0.1)
        tf.summary.histogram('output_layer/bias', b2)
    with tf.name_scope('Wx_plus_b'): #净输入
        Wx_plus_b2 = tf.matmul(output1,W2) + b2
        tf.summary.histogram('output_layer/Wx_plus_b',Wx_plus_b2)
output2 = Wx_plus_b2

#损失
with tf.name_scope('loss'): #损失
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-output2),reduction_indices=[1]))
    tf.summary.scalar('loss',loss)
with tf.name_scope('train'): #训练过程
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

#初始化
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
merged = tf.summary.merge_all() #将图形、训练过程等数据合并在一起
writer = tf.summary.FileWriter('logs',sess.graph) #将训练日志写入到logs文件夹下

#训练
for i in range(1000):
    sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
    if(i%50==0): #每50次写一次日志
        result = sess.run(merged,feed_dict={xs:x_data,ys:y_data}) #计算需要写入的日志数据
        writer.add_summary(result,i) #将日志数据写入文件

在文件路径打开命令行:输入

python tensorboard-demo.py

运行结果:

《深度学习框架TensorFlow入门-windows10系统》 图片.png

执行上述代码,会在“当前路径/logs”目录下生成一个events.out.tfevents.{time}.{machine-name}的文件:

《深度学习框架TensorFlow入门-windows10系统》 图片.png

启动tensorboard

tensorboard-demo.py文件路径下新打开一个命令行输入:

tensorboard –logdir=logs

《深度学习框架TensorFlow入门-windows10系统》 图片.png

在浏览器打开http://localhost:6006/,即可看到:

《深度学习框架TensorFlow入门-windows10系统》 图片.png

4. 基于tensorflow的MNIST手写数字识别

这个例子要先下载数据集,否则运行过程可能会链接失败,下载网址:https://github.com/zalandoresearch/fashion-mnist

需要下载四个文件:

  • train-images-idx3-ubyte.gz
  • train-labels-idx1-ubyte.gz
  • t10k-images-idx3-ubyte.gz
  • t10k-labels-idx1-ubyte.gz

下载好这四个文件后,不用解压。我下载是保存到F:\TensorFlow\MNIST_data目录。

运行的代码链接:
https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/tutorials/mnist

[图片上传失败…(image-681633-1516375599278)]

提示:

请不要用中文命名目录,中文目录中看不到任何图形。

    原文作者:可汗爷爷
    原文地址: https://www.jianshu.com/p/6bf97d903296
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞