使用win8.1安装tensorflow CPU一次成功

1.本机电脑配置

电脑概览
电脑型号 联想 ThinkCentre M6500t-N000
操作系统 Microsoft Windows 8.1 Pro (64位)
CPU (英特尔)Intel(R) Core(TM) i3-4160 CPU @ 3.60GHz(3600 MHz)
显卡 Intel(R) HD Graphics 4400 (1024 MB)

2.python版本3.5以上(一定要3.x版本的Python,别问我是怎么知道的,前辈踩的坑)
本人使用的是anaconda

Microsoft Windows [版本 6.3.9600]
(c) 2013 Microsoft Corporation。保留所有权利。

C:\Users\test>python
Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

下载地址:anaconda官网

3.修改pip源
安装完Python之后,Python3.x以上版本里面自带有pip,Python3.x以上是pip3,pip下载速度很慢,看着网上各种资料,修改了下pip源。
使用国内镜像加速pip安装,做如下修改:
WIndows 8.1 在“C:\Users\你的用户名\AppData\Local\pip”文件夹下,新建一个文件,命名为“pip.ini“,添加内容:

[global]  
index-url = https://pypi.tuna.tsinghua.edu.cn/simple  
[install]  
trusted-host=mirrors.aliyun.com  

4.安装Tensorflow CPU版本
TensorFlow的安装有GPU和CPU两个不同版本,我的GPU不行,所以我安装的是CPU的版本。
采用的输入Shell指令进行安装,
CPU版:

pip install tensorflow  

CPU版:

pip install tensorflow-gpu  

但是,这样做是及其慢的,你可以自己感受下,后来各种百度,发现还是得先提前下载好,制定路径本地安装是最快的,路径是这样的

https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc0-cp35-cp35m-win_amd64.whl

类似这种,(网上找的,要改成你自己的whl包),我的包名叫做 tensorflow-1.3.0-cp36-cp36m-win_amd64.whl,所以应该怎么写

https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.3.0-cp36-cp36m-win_amd64.whl

下载到你本地路径,再安装,一次成功:

这里写图片描述

5.验证安装是否成功
先跑一个mnist的代码

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)


x = tf.placeholder(tf.float32, [None, 784])


W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))


y = tf.nn.softmax(tf.matmul(x, W) + b)


# Training
y_ = tf.placeholder(tf.float32, [None, 10])


cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))


train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)


# run
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})


# evaluate
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}))


运行结果:

runfile('C:/Users/test/.spyder-py3/temp.py', wdir='C:/Users/test/.spyder-py3')
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
0.9197

成功截图:
这里写图片描述

6.安装遇到的某一些警告
类似这样的

2017-10-25 09:44:24.637141: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\p
latform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but
 these are available on your machine and could speed up CPU computations.
2017-10-25 09:44:24.654800: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\p
latform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.

后来再网上查了下,这是由于下载的tensorflow的包是官方预编译好的,并没有针对该计算机的CPU进行优化,所以导致了这一警告,上stackoverflow上的解决办法是源码安装,但是我赖的弄了,反正处于学习阶段,重要的是算法的掌握和运用,这些小问题以后有时间再弄。
下面附一个带有GPU测试的mnist代码

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
import time
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

start = time.time()

mnist = input_data.read_data_sets('MNIST_data',one_hot=True)

# print mnist.train.images.shape,mnist.train.labels.shape
# (55000, 784) (55000, 10)
# 784 = 28*28
# print mnist.test.images.shape,mnist.test.labels.shape\
# (10000, 784) (10000, 10)
# print mnist.validation.images.shape,mnist.validation.labels.shape
# (5000, 784) (5000, 10)

def Weight_value(shape):
    init = tf.random_normal(shape, stddev=0.1)
    return tf.Variable(init, name="weight")
def bias_value(shape):
    init = tf.constant(0.1, shape=shape)
    return tf.Variable(init)
def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME")
def pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")

xs = tf.placeholder(tf.float32, [None, 784])
ys = tf.placeholder(tf.float32, [None, 10])

x_image = tf.reshape(xs, [-1, 28, 28, 1])

# layer1 conv1  [-1, 28, 28, 32]
W_conv1 = Weight_value([5, 5, 1, 32])
b_conv1 = bias_value([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1)+b_conv1)
# layer2 pool1 [-1, 14, 14, 32]
h_pool1 = pool_2x2(h_conv1)
# layer3 conv2 [-1, 14, 14, 64]
W_conv2 = Weight_value([5, 5, 32, 64])
b_conv2 = bias_value([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2)+b_conv2)
# layer4 pool2 [-1,7,7,64]
h_pool2 = pool_2x2(h_conv2)
# layer5 fc1 [-1,1024]
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
W_fc1 = Weight_value([7*7*64, 1024])
b_fc1 = bias_value([1024])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1)+b_fc1)
#layer6 dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# layer7 fc2 [-1,10]
W_fc2 = Weight_value([1024, 10])
b_fc2 = bias_value([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2)+b_fc2)

# cross_entropy
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(y_conv), reduction_indices=[1]))
# optimizer
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# accuracy
correct_prediction = tf.equal(tf.argmax(ys, 1), tf.argmax(y_conv, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# init
init = tf.global_variables_initializer()
# sess
config = tf.ConfigProto()
config.gpu_options.allow_growth = True

with tf.Session(config=config) as sess:
    sess.run(init)
    for i in range(1001):
        x_batch, y_batch = mnist.train.next_batch(50)
        sess.run(train_step, feed_dict={xs:x_batch, ys:y_batch, keep_prob:0.5})
        if i%100 == 0:
            x_test, y_test = mnist.test.next_batch(50)
            print(i, ' step train ', sess.run(accuracy, feed_dict={xs: x_batch, ys: y_batch, keep_prob: 1}))
            print(i, ' step test', sess.run(accuracy, feed_dict={xs:x_test, ys:y_test, keep_prob: 1}))

end = time.time()
print("function time is : ", end-start)

# 0  step train  0.08
# 0  step test 0.14
# 100  step train  0.82
# 100  step test 0.82
# 200  step train  0.84
# 200  step test 0.96
# 300  step train  0.92
# 300  step test 0.9
# 400  step train  0.96
# 400  step test 0.92
# 500  step train  0.9
# 500  step test 0.96
# 600  step train  0.94
# 600  step test 1.0
# 700  step train  0.96
# 700  step test 0.96
# 800  step train  0.96
# 800  step test 1.0
# 900  step train  0.96
# 900  step test 0.96
# 1000  step train  0.94
# 1000  step test 0.96
# function time is :  25.16685461997986
# 使用cpu function time is :  269.778738022
# 速度提高了10倍

参考博客
1.http://blog.csdn.net/hongzhen91/article/details/62888660
2.http://blog.csdn.net/sinat_28731575/article/details/74633476
3.http://blog.csdn.net/zd_nupt/article/details/77507688

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

deywós

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

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

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

打赏作者

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

抵扣说明:

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

余额充值