基于anaconda在Windows安装TensorFlow

来源:互联网 发布:巨灵数据库账号 编辑:程序博客网 时间:2024/05/16 09:48

引言—CUDA与版本选择

本人系统:Windows8.1;

硬件:TensorFlow有支持CPU,GPU版本,支持GPU版本的TensorFlow对硬件(显卡)要求更高。

如果显卡是nvdia(看电脑贴签)可以通过nvdia CUDA支持GPU型号,查看自己电脑时候支持CUDA,如果支持,就可以装GPU版本。(GPU版本功能更强)

关于CUDA的理解博文

一、安装步骤

1.anaconda 安装

1.1先下好anaconda。anaconda下载链接;

1.2按照这篇文章一步步配置好环境。
anaconda安装配置详细步骤;

注意要点:1.Windows系统只支持python3.5版本;2.安装anaconda过程中,如果有部分文件下载不成功,可以使用conda install ****指令,例如需要安装wheel文件,直接conda install wheel。至于需要安装什么文件,可以conda list。

2. tensorflow安装

好,anaconda如果安装配置成功的话,下面就好办了。接下来,只需要在anaconda的框架下进行下载TensorFlow和配置。

2.1 TensorFlow安装
按照这篇文章一步步来就好。TensorFlow安装配置教程 .

注意要点:        1.如果启动anaconda navigator闪退,可以试试在anaconda prompt命令行,`conda update anaconda-navigator`

3.关于TensorFlow GPU版本的安装
原来安装了CPU版本,但CPU版本局限性太大了,根本满足不了科研的要求,悲剧地重新安装GPU版本。

安装这篇文章,一步步来:简书_TensorFlowGPU版本在Windows下的安装

根据个人经历,大概写一下雷区:

1.首先,我查看自己电脑是GeForce 720M,满足cuda的条件。然后按照用GPU加速深度学习: Windows安装CUDA+TensorFlow教程 这篇文章去按照CUDA Toolkit 8.0,和Cudnn v6.0。

2.我分享的专栏说需要vs2015,我电脑原来是vs2017,后来我省略了vs调试cuda生成的文件,TensorFlow也能正常运行;

3.安装好cuda,以及解压cudnn。按道理基本完事。

4.然后,我使用cmd运行python或者使用notepad++,import TensorFlow进行各种运算,正常;但是我使用anaconda 加载spider,运行,import TensorFlow 却出错ImportError: No module named 'tensorflow' on windows!!
郁闷啊。
一开始,我参考GitHub-ImportError: No module named’tensorflow’,重装TensorFlow-GPU版本;
在cmd命令行窗口,输入

>>>pip install --upgrade -I setuptools>>>pip install --ignore-installed --upgrade https://mirrors.tuna.tsinghua.edu.cn/tensorflow/windows/gpu/tensorflow_gpu-1.3.0rc0-cp35-cp35m-win_amd64.whl#这里使用了Tsinghua的mirror链接安装

重装了之后,依然解决不了问题。
再然后,我在anaconda prompt按照代码重试了一次,才解决问题,所以,当时没有安装简书的那篇文章一步步走,确实是碰了苦头。


4、关于TensorFlow使用过程升级问题
装的GPU版本是tensorflow (1.3.0rc0),但是运行tensorboard的时候,没有出现scalar,然后试了升级TensorFlow版本,成功解决问题。
anaconda prompt运行

pip install --ignore-installed --upgrade tensorflow-gpu

二、TensorFlow测试代码

安装过程中肯定会出现bug,但能跳过的先跳过,直到安装到最后一步,然后,我们进行测试,看看我们是否安装成功。

2.1 使用python进行测试
打开cmd,输入python,进入python环境。详细如下:

C:\Users\RoFun>pythonPython 3.5.2 |Anaconda custom (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import tensorflow as tf>>> a=tf.constant([1.0,2.0],name='a')>>> b=tf.constant([5.0,10.0],name='b')>>> result=a+b>>> sess=tf.Session()>>> sess.run(result)array([  6.,  12.], dtype=float32)

解释一下,这是实现了在TensorFlow框架下,两个向量的相加,session()是为了生成TensorFlow的会话,从而计算结果并输出。

2.2 使用anaconda navigator测试
启动anaconda navigator(直接在Windows搜索框搜),使用里边TensorFlow的Spyder(是一种python的编译器),粘贴测试代码。

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at##     http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# =============================================================================="""A very simple MNIST classifier.See extensive documentation athttps://www.tensorflow.org/get_started/mnist/beginners"""from __future__ import absolute_importfrom __future__ import divisionfrom __future__ import print_functionimport argparseimport sysfrom tensorflow.examples.tutorials.mnist import input_dataimport tensorflow as tfFLAGS = Nonedef main(_):  # Import data  mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)  # Create the model  x = tf.placeholder(tf.float32, [None, 784])  W = tf.Variable(tf.zeros([784, 10]))  b = tf.Variable(tf.zeros([10]))  y = tf.matmul(x, W) + b  # Define loss and optimizer  y_ = tf.placeholder(tf.float32, [None, 10])  # The raw formulation of cross-entropy,  #  #   tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),  #                                 reduction_indices=[1]))  #  # can be numerically unstable.  #  # So here we use tf.nn.softmax_cross_entropy_with_logits on the raw  # outputs of 'y', and then average across the batch.  cross_entropy = tf.reduce_mean(      tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))  train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)  sess = tf.InteractiveSession()  tf.global_variables_initializer().run()  # Train  for _ in range(1000):    batch_xs, batch_ys = mnist.train.next_batch(100)    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})  # Test trained model  correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))  print(sess.run(accuracy, feed_dict={x: mnist.test.images,                                      y_: mnist.test.labels}))if __name__ == '__main__':  parser = argparse.ArgumentParser()  parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data',                      help='Directory for storing input data')  FLAGS, unparsed = parser.parse_known_args()  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

测试结果应该是90%多。

来源:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/mnist/mnist_softmax.py

三、安装心得

装了一天半,其实,使用Windows安装TensorFlow还是比较容易出问题,倒不如先装Ubuntu系统,在Ubuntu安装TensorFlow。
其次,使用pip方式安装文件,下载很慢,可以尝试使用镜像下载,

# 添加Anaconda的TUNA镜像conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/# TUNA的help中镜像地址加有引号,需要去掉# 设置搜索时显示通道地址conda config --set show_channel_urls yes
采用清华的镜像网站,下载的速度会更快。

总的来说,安装TensorFlow的方式有很多,比如,基于Docker,基于pip,基于anaconda,基于源代码编译安装等方式。
其中,基于anaconda,基于Docker较为省心,基于源代码较为麻烦,在这就不一一展开。


参考:
1.安装GPU版本TensorFlow
2.CSDN TensorFlow_GPU版安装
3.在Windows上安装GPU版Tensorflow

原创粉丝点击