import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
def add_layer(inputs,in_size,out_size,activation_function=None):
W=tf.Variable(tf.random_normal([in_size,out_size]))
b=tf.Variable(tf.zeros([1,out_size])+0.01)
Z=tf.matmul(inputs,W)+b
if activation_function is None:
out_puts=Z
else:
out_puts=activation_function(Z)
return out_puts
if __name__=="__main__":
MINST=input_data.read_data_sets("./",one_hot=True)
learning_rate=0.05
batch_size=128
n_epochs=10
X=tf.placeholder(tf.float32,[batch_size,784])
Y=tf.placeholder(tf.float32,[batch_size,10])
L1=add_layer(X,784,1000,tf.nn.relu)
prediction=add_layer(L1,1000,10)
entropy=tf.nn.softmax_cross_entropy_with_logits(labels=Y,logits=prediction)
loss=tf.reduce_mean(entropy)
optimizer=tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
init=tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
n_batches=int(MINST.train.num_examples/batch_size)
for i in range(n_epochs):
for j in range(n_batches):
X_batch,Y_batch=MINST.train.next_batch(batch_size=batch_size)
_,loss_=sess.run([optimizer,loss],feed_dict={
X:X_batch,
Y:Y_batch
})
if j == 0:
print("Loss of epochs[{0}] batch[{1}]: {2}".format(i, j, loss_)) # test the model
n_batches = int(MINST.test.num_examples / batch_size)
total_correct_preds = 0
for i in range(n_batches):
X_batch, Y_batch = MINST.test.next_batch(batch_size)
preds = sess.run(prediction, feed_dict={X: X_batch, Y: Y_batch})
correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(Y_batch, 1))
accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32)) total_correct_preds += sess.run(accuracy) print("Accuracy {0}".format(total_correct_preds / MINST.test.num_examples))

我们不做卷积。直接将x输入到网络中去。最后用softmax作为激活函数

大概结构,我这里没法上传,等我回去在传。

使用一层神经网络训练mnist数据集的更多相关文章

  1. TensorFlow初探之简单神经网络训练mnist数据集(TensorFlow2.0代码)

    from __future__ import print_function from tensorflow.examples.tutorials.mnist import input_data #加载 ...

  2. mxnet卷积神经网络训练MNIST数据集测试

    mxnet框架下超全手写字体识别—从数据预处理到网络的训练—模型及日志的保存 import numpy as np import mxnet as mx import logging logging. ...

  3. 使用caffe训练mnist数据集 - caffe教程实战(一)

    个人认为学习一个陌生的框架,最好从例子开始,所以我们也从一个例子开始. 学习本教程之前,你需要首先对卷积神经网络算法原理有些了解,而且安装好了caffe 卷积神经网络原理参考:http://cs231 ...

  4. 实践详细篇-Windows下使用VS2015编译的Caffe训练mnist数据集

    上一篇记录的是学习caffe前的环境准备以及如何创建好自己需要的caffe版本.这一篇记录的是如何使用编译好的caffe做训练mnist数据集,步骤编号延用上一篇 <实践详细篇-Windows下 ...

  5. Python实现bp神经网络识别MNIST数据集

    title: "Python实现bp神经网络识别MNIST数据集" date: 2018-06-18T14:01:49+08:00 tags: [""] cat ...

  6. TensorFlow——CNN卷积神经网络处理Mnist数据集

    CNN卷积神经网络处理Mnist数据集 CNN模型结构: 输入层:Mnist数据集(28*28) 第一层卷积:感受视野5*5,步长为1,卷积核:32个 第一层池化:池化视野2*2,步长为2 第二层卷积 ...

  7. deep_learning_LSTM长短期记忆神经网络处理Mnist数据集

    1.RNN(Recurrent Neural Network)循环神经网络模型 详见RNN循环神经网络:https://www.cnblogs.com/pinard/p/6509630.html 2. ...

  8. TensorFlow——LSTM长短期记忆神经网络处理Mnist数据集

    1.RNN(Recurrent Neural Network)循环神经网络模型 详见RNN循环神经网络:https://www.cnblogs.com/pinard/p/6509630.html 2. ...

  9. TensorFlow 训练MNIST数据集(2)—— 多层神经网络

    在我的上一篇随笔中,采用了单层神经网络来对MNIST进行训练,在测试集中只有约90%的正确率.这次换一种神经网络(多层神经网络)来进行训练和测试. 1.获取MNIST数据 MNIST数据集只要一行代码 ...

随机推荐

  1. zabbix v3.0安装部署

    这篇文章没有写明init的部分要注意 zabbix v3.0安装部署 摘要: 本文的安装过程摘自http://www.ttlsa.com/以及http://b.lifec-inc.com ,和站长凉白 ...

  2. IIS的应用程序池优化方法

    IIS应用程序池优化方案 服务器经常产生“应用程序池 'DefaultAppPool' 提供服务的进程关闭时间超过了限制.进程 ID 是 '3504'.”的错误,导致iis处于假死状态,经了解是IIS ...

  3. java.lang.IllegalStateException: No typehandler found for mapping XXX

    前言:今天遇到了这个问题,刚开始觉得很容易解决的,毕竟能定位到出问题的文件和对应的字段,根据以往的经验也可以判断出是字段映射类型不匹配的问题,后来找了半天还是没找到问题的根源,从网上百度,也没看到令人 ...

  4. Eclipse中Ant的配置与测试

    在Eclipse中使用Ant Ant是Java平台下非常棒的批处理命令执行程序,能非常方便地自动完成编译,测试,打包,部署等等一系列任务,大大提高开发效率.如果你现在还没有开始使用Ant,那就要赶快开 ...

  5. span的赋值与取值

      1.<span id="span_id">span的文本</span>的取值. js取<span>的值并不是用document.getEle ...

  6. 一个酷绚的linux 桌面程序 GLX-DOCK (cario-dock)

    记录一个酷绚的linux  桌面程序  GLX-DOCK (cario-dock),支持多种风格的桌面主题. http://glx-dock.org/ 优势: 多个workspaces 方便自由切换 ...

  7. VC下加载JPG/GIF/PNG图片的两种方法

    转载自:http://blog.sina.com.cn/s/blog_6582aa410100huil.html 仅管VC有提供相应的API和类来操作bmp位图.图标和(增强)元文件,但却不支持jpg ...

  8. 【转载并整理】mysql 1293错误 建表两个timestamp

    http://www.jb51.net/article/50878.htm 这里要使用到mysql的触发器

  9. Python 爬虫实例(15) 爬取 百度百聘(微信公众号)

    今天闲的无聊,爬取了一个网站,百度百聘,仅供学习参考 直接上代码: #-*-coding:utf-8-*- from common.contest import * def spider(): hea ...

  10. 收集一些有意思的ASCII程序注释(持续收集中,希望大家踊跃贡献)

      /** * * created by Mr.Simple, Aug 21, 20141:51:40 PM. * Copyright (c) 2014, hehonghui@umeng.com Al ...