1、获取mnist数据集,得到正确的数据格式

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

2、定义网络大小:图片的大小是28*28,784个像素点,输入神经元为784个,输出0~9个数,输出神经元为10个

n_input =784
n_layer1 = 10
examples_to_show = 10 #显示的测试图像个数
x_data = tf.placeholder(tf.float32,[None,n_input])
y_data = tf.placeholder(tf.float32,[None,n_layer1])
 
3、添加层函数
inputsize:输入神经元的个数weights:权重biases:偏置值activation_function:激活函数
输出:该层的进行激活后的神经元,下一个层的输入
def addlayer(inputsize,weights,biases,activation_function=None):
    output = tf.add(tf.matmul(x_data,weights),biases)
    if activation_function == None:
        return tf.nn.sigmoid(output)
    else:
        return tf.nn.softmax(output)
 
4、构建网络
#预测输出
添加隐藏层
y_pre=addlayer(x_data,layer1_weights,layer1_biases,activation_function=tf.nn.softmax)
y_true=y_data
#反向
cross_entropy=-tf.reduce_sum(y_true * tf.log(y_pre))
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_data:batch_xs , y_data:batch_ys})
     if (i%50==0):
        print ("loss : ",sess.run(cross_entropy,feed_dict={x_data:batch_xs , y_data:batch_ys}))
        # 这个越大越好
        print ("prediction acc : ",compute_acc(mnist.test.images[:100], mnist.test.labels[:100]))
5、测试集计算模型预测准确率
def compute_acc(x_input_test,y_true_test):
    y_pre_test = sess.run(y_pre,feed_dict={x_data:x_input_test, y_data:y_true_test})
    correct_prediction=tf.equal(tf.argmax(y_pre_test,1),tf.argmax(y_true_test,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    result = sess.run(accuracy, feed_dict={x_data:x_input_test , y_data:y_true_test})
    return result
6、结果

最新文章

  1. JavaScript正则表达式,你真的知道?
  2. linux 报错 bash ‘/bin/sh: Syntax error: “(” unexpected
  3. VC++ MFC中如何将应用程序的配置信息保存到注册表中(二)
  4. ADT下开发环境的配置--个人配置啦 Eclipse Color Themes
  5. OrCAD Capture CIS与Allegro交互布局
  6. python的全局变量玩法还挺特别的
  7. C# 高精度求幂 poj1001
  8. Fedora 19+ 启动顺序调整
  9. PHP SimpleXML
  10. Nested Class Templates
  11. tensorflow-RNN和LSTM
  12. Debian图形界面与字符界面之间的切换
  13. 19.纯 CSS 创作一种有削铁如泥感觉的菜单导航特效
  14. The Win32 Rundll and Rundll32 Interface Related Topics
  15. Android训练课程(Android Training) - 构建你的第一个应用
  16. HihoCoder - 1789:阶乘问题 (简单数学)
  17. android studio 3.1.4下载安装配置(附旧版本下载地址)
  18. [活动已结束]《深入理解Android:Wi-Fi、NFC和GPS卷》CSDN社区活动
  19. javac的访问者模式
  20. gulp教程之gulp中文API

热门文章

  1. maomao的现在与未来
  2. (栈 注意格式)P1739 表达式括号匹配 洛谷
  3. (贪心和优先队列) POJ1862 Stripies
  4. Jqgrid pager 关于“local” dataType 动态加载数据分页的研究(没好用的研究结果)
  5. bzoj2819 DFS序 + LCA + 线段树
  6. PHP 连接 Memcached 服务
  7. angular中的服务
  8. ajax请求出现400错误
  9. redis使用问题一:Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool] with root cause
  10. springMVC怎么接受前台传过来的多种类型参数?(集合、实体、单个参数)