windows10+Tensorflow配置

准备

Anaconda3 (python3.5版本的)
cudnn5.1
cuda 8.0

安装

新版的Anaconda3 的python版本是3.6,没办法pip tensorflow

  • 于是新建一个python3.5的虚拟环境
//创建环境,py3.5是环境的名字 
conda create -n py3.5 python=3.5
//激活环境
activate py3.5
//退出环境
deactivate py3.5
//为jupyter notebook 配置环境
conda install -n py27 ipykernel
//然后激活
python -m ipykernel install --user
//为spyder配置环境
进入Anaconda Navigator,在py3.5虚拟环境中安装spyder
#设置国内镜像源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
#打开C:\Users\king目录下的.condarc文件
删除-defualts的channel

这里写图片描述

  • 安装Tensorflow
pip install tensorflow-gpu
  • CUDA8.0和cuDNN安装
    官网下载后,将cuDNN的文件复制到相应文件夹
C:\cuda\bin\cudnn64_5.dll —> C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin
C:\cuda\include\cudnn.h —> C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\include
C:\cuda\lib\x64\cudnn.lib —> C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64
  • 测试
import tensorflow as tf

a = tf.random_normal((100, 100))
b = tf.random_normal((100, 500))
c = tf.matmul(a, b)
sess = tf.InteractiveSession()
sess.run(c)
  • 结果

这里写图片描述

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
#读取mnist文件

#设定参数
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)

#实际值
y_ = tf.placeholder("float", [None,10])

#定义cost
cross_entropy = -tf.reduce_sum(y_*tf.log(y))

#设定训练算法
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

#初始化变量
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})

#建立评估模型
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

#布尔值转换成浮点数,然后取平均值
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

#计算所学习到的模型在测试数据集上面的正确率
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

结果如下
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值