莫烦TensorFlow_09 MNIST例子
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例子的更多相关文章
- 莫烦TensorFlow_11 MNIST优化使用CNN
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #number 1 to 10 d ...
- 莫烦RL-01小例子
# Python 3.6.5 :: Anaconda, Inc. import numpy as np import pandas as pd import time np.random.seed(2 ...
- 稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记。
稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记. 还有 google 在 udacity 上的 CNN 教程. CNN(Convolutional Neural Networks) 卷积神经网络简单 ...
- tensorflow学习笔记-bili莫烦
bilibili莫烦tensorflow视频教程学习笔记 1.初次使用Tensorflow实现一元线性回归 # 屏蔽警告 import os os.environ[' import numpy as ...
- 莫烦pytorch学习笔记(八)——卷积神经网络(手写数字识别实现)
莫烦视频网址 这个代码实现了预测和可视化 import os # third-party library import torch import torch.nn as nn import torch ...
- 【莫烦Pytorch】【P1】人工神经网络VS. 生物神经网络
滴:转载引用请注明哦[握爪] https://www.cnblogs.com/zyrb/p/9700343.html 莫烦教程是一个免费的机器学习(不限于)的学习教程,幽默风俗的语言让我们这些刚刚起步 ...
- tensorflow 莫烦教程
1,感谢莫烦 2,第一个实例:用tf拟合线性函数 import tensorflow as tf import numpy as np # create data x_data = np.random ...
- 莫烦大大TensorFlow学习笔记(9)----可视化
一.Matplotlib[结果可视化] #import os #os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf i ...
- scikit-learn学习笔记-bili莫烦
bilibili莫烦scikit-learn视频学习笔记 1.使用KNN对iris数据分类 from sklearn import datasets from sklearn.model_select ...
随机推荐
- 《淘宝数据库OceanBase SQL编译器部分 源码阅读--解析SQL语法树》
淘宝数据库OceanBase SQL编译器部分 源码阅读--解析SQL语法树 曾经的学渣 2014-06-05 18:38:00 浏览1455 云数据库Oceanbase OceanBase是 ...
- ICCV
ICCV 简介 ICCV 的全称是 IEEE International Conference on Computer Vision,即国际计算机视觉大会,由IEEE主办,与计算机视觉模式识别会议(C ...
- 合并K个有序数组-Java
package com.rao.algorithm; import java.util.Arrays; /** * @author Srao * @className MergeK * @date 2 ...
- 小程序-promise封装小程序的请求方法
// 在utils-->base.js中,封装一个Base类,有一个axios方法 class Base{ constructor(){ } axios(method,url,data){ co ...
- Anaconda入门教程【快速掌握】
Anaconda 使用指南 概述 很多学习python的初学者甚至学了有一段时间的人接触到anaconda或者其他虚拟环境工具时觉得无从下手, 其主要原因就是不明白这些工具究竟有什么用, 是用来做什么 ...
- APP兼容性测试 (一) 机型选择概要
一.App兼容性问题有哪些 安装失败.启动失败.卸载失败,卸载不干净. 程序运行过程中闪退 部分控件显示不完整或者功能失效 屏幕显示异常 图片展示不全等 二.App兼容性测试的核心要点 测试软件是否能 ...
- DAX 第九篇:文本函数
DAX中用于处理文本的函数,和其他语言很相似. 一,文本连接 文本连接也可以使用操作符 & 来实现,也可以使用函数CONCATENATE来实现: CONCATENATE(<text1&g ...
- 2019-11-27-WPF-全屏透明窗口
原文:2019-11-27-WPF-全屏透明窗口 title author date CreateTime categories WPF 全屏透明窗口 lindexi 2019-11-27 09:22 ...
- js变量类型及检查
一.变量的类型 JavaScript 有六种数据类型.主要的类型有 Number.String.object 以及 Boolean 类型,其他两种类型为 null 和 undefined.var ob ...
- 3-Consul 使用手册
原文:http://www.liangxiansen.cn/2017/04/06/consul/ Consul包含多个组件,但是作为一个整体,为你的基础设施提供服务发现和服务配置的工具.他提供以下关键 ...