import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data', one_hot = True) #
# add layer
#
def add_layer(inputs, in_size, out_size, activation_function = None): Weights = tf.Variable(tf.random_normal([in_size, out_size])) # hang lie
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
Wx_plus_b = tf.matmul(inputs, Weights) + biases if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b) return outputs def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs:v_xs})
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))#返回最大值的索引号
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={xs:v_xs, ys:v_ys})
return result #
# define placeholder for inputs to network
#
xs = tf.placeholder(tf.float32, [None, 784]) # 28x28, 784 dimention / sample
ys = tf.placeholder(tf.float32, [None, 10]) #
# add output layer
#
prediction = add_layer(xs, 784, 10, activation_function = tf.nn.softmax) #
# the error between prediction and real data
#
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
reduction_indices=[1])) #loss
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) sess = tf.Session()
sess.run(tf.global_variables_initializer()) for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs:batch_xs, ys:batch_ys})
if i % 50 == 0:
print(compute_accuracy(
mnist.test.images, mnist.test.labels))

  

解释 compute_accuracy 的计算原理:

来自:https://blog.csdn.net/cy_tec/article/details/52046806

莫烦TensorFlow_09 MNIST例子的更多相关文章

  1. 莫烦TensorFlow_11 MNIST优化使用CNN

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #number 1 to 10 d ...

  2. 莫烦RL-01小例子

    # Python 3.6.5 :: Anaconda, Inc. import numpy as np import pandas as pd import time np.random.seed(2 ...

  3. 稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记。

    稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记. 还有 google 在 udacity 上的 CNN 教程. CNN(Convolutional Neural Networks) 卷积神经网络简单 ...

  4. tensorflow学习笔记-bili莫烦

    bilibili莫烦tensorflow视频教程学习笔记 1.初次使用Tensorflow实现一元线性回归 # 屏蔽警告 import os os.environ[' import numpy as ...

  5. 莫烦pytorch学习笔记(八)——卷积神经网络(手写数字识别实现)

    莫烦视频网址 这个代码实现了预测和可视化 import os # third-party library import torch import torch.nn as nn import torch ...

  6. 【莫烦Pytorch】【P1】人工神经网络VS. 生物神经网络

    滴:转载引用请注明哦[握爪] https://www.cnblogs.com/zyrb/p/9700343.html 莫烦教程是一个免费的机器学习(不限于)的学习教程,幽默风俗的语言让我们这些刚刚起步 ...

  7. tensorflow 莫烦教程

    1,感谢莫烦 2,第一个实例:用tf拟合线性函数 import tensorflow as tf import numpy as np # create data x_data = np.random ...

  8. 莫烦大大TensorFlow学习笔记(9)----可视化

      一.Matplotlib[结果可视化] #import os #os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf i ...

  9. scikit-learn学习笔记-bili莫烦

    bilibili莫烦scikit-learn视频学习笔记 1.使用KNN对iris数据分类 from sklearn import datasets from sklearn.model_select ...

随机推荐

  1. Python 对象比较(is & ==)

    Python 对象有 3 要素 id type value id 对象在内存中的地址 可以通过 id() 获取 比较 只有同一个对象 id 才会相同 id 通过 is 比较 示例: a = list( ...

  2. Python ord & chr

    ord & chr ord: 返回单个 Unicode 字符编码的整数 chr: 给一个 Unicode 编码,返回一个字符 (0 ~ 1,114,111) (ASCII 与 Unicode ...

  3. 【day06】PHP

    一.字符串函数库 1.安装 2.   (1)strlen:获得字符串的字符长度   (2)substr:字符串截取       格式: string substr(string $var,      ...

  4. ASP.NET开发实战——(九)ASP.NET MVC 与数据库之ORM

    之前的文章中介绍了如何在ASP.NET中通过ADO.NET操作SQL Server和My SQL数据库,数据库的操作是通过SQL语句的执行来完成的,在ASP.NET中还有一个简便的方式来使用数据库,那 ...

  5. PyQt5发布技巧:指定插件(plugins)路径

    一般来说,发布后的应用程序要能正常使用必须设置插件路径的环境变量: cmd脚本: wmic ENVIRONMENT create name="QT_QPA_PLATFORM_PLUGIN_P ...

  6. springboot+shrio简易登录登出和用户权限认证。

    源码:https://github.com/huangshengz/myJavaDemo本例子参考:https://www.cnblogs.com/HowieYuan/p/9259638.html 本 ...

  7. HDU 6148 (数位DP)

    ### HDU 6148 题目链接 ### 题目大意: 众所周知,度度熊非常喜欢数字. 它最近发明了一种新的数字:Valley Number,像山谷一样的数字. 当一个数字,从左到右依次看过去数字没有 ...

  8. 计算机基础 ---- 编码(er)

    1.字符集与编码 字符集:字符组成的集合,汉字,字母,符号被收录到标准的字符集合 编码:规定每个字符使用一个字节还是多个字节存储,那些字节来存储的规定 术语:字符编码(character encodi ...

  9. 【大数据】SparkSql 连接查询中的谓词下推处理 (二)

    本文首发于 vivo互联网技术 微信公众号 https://mp.weixin.qq.com/s/II48YxGfoursKVvdAXYbVg作者:李勇 目录:1.左表 join 后条件下推2.左表j ...

  10. python学习笔记 - socket通信

    socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...