• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

nunca

但行好事 莫问前程
  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

DL: 初试 tensorflow

 

一、TensorFlow安装

win 7 下安装 TensorFlow并不困难,cmd下 pip install TensorFlow即可

二、对于TensorFlow初学者,MNIST数据集试验就像是初学编程的“Hello,world”,第一次尝试并不困难,网上教程挺多。

参考博客: http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html

                   http://blog.csdn.net/sysstc/article/details/73468532

                  http://blog.csdn.net/sysstc/article/details/74910918

                  http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_pros.html

#coding:utf-8
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import gzip
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True)
x = tf.placeholder(tf.float32,[None,784])
y_ = tf.placeholder(tf.float32,[None,10])
W = tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W)+b)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_,logits=tf.log(y)))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.global_variables_initializer()
sess = tf.InteractiveSession()
sess.run(init)
for i in range(1000):
    batch_x,batch_y = mnist.train.next_batch(100)
    sess.run(train_step,feed_dict={x:batch_x,y_:batch_y})
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}))
sess.close()

三、报错调试

可能会出现如下图所示错误

出现如图所示错误其实不要紧,因为程序已经正确运行,答案已经出来了,但是对于强迫症来说还是很难受,尤其是看到红色的bug,解决方法很简单,在头部加两句代码:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2' 
既然无论如何时间都会过去,为什么不选择做些有意义的事情呢

posted on 2018-03-16 21:49  乐晓东随笔  阅读(187)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3