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. 树莓派进阶之路 (028) - 树莓派SQLite3的安装

    MySQL占用内存太大,而SQLite是一款轻量级零配置数据库,非常适合在树莓派和其他嵌入式系统中使用.SQLite文档详细资料丰富,本文不会详细解释SQLite数据库操作的方方面面,只能结合具体场景 ...

  2. The method getServletContext() is undefined for the type HttpServletRequest

    request.getServletContext().getRealPath("/") 已经加入了 sun runtime library但是还是提示错误 是因为 写法过时了改成 ...

  3. 【struts2】struts2的execAndWait拦截器使用

    使用execAndWait拦截器可以在等待较长时间的后台处理中增加等待页面.实现如下图所示的效果: 1)struts.xml主要部分 <action name="test" ...

  4. golang学习笔记 ---命令行参数

    os 包以跨平台的方式,提供了一些与操作系统交互的函数和变量.程序的命令行参数可从os包的Args变量获取:os包外部使用os.Args访问该变量. os.Args变量是一个字符串(string)的切 ...

  5. /proc 目录详细说明

    /proc路径详细: Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...

  6. systemd的程序自启动脚本编写

    以FreeSWITCH的自启动脚本为例. 一. 编写freeswitch.service文件 [Unit] Description=FreeSWITCH After=syslog.target net ...

  7. rdlc 分页操作和分页统计

    1. 工具箱中拖一个列表过来,设置 列表-->行组-->组属性常规-->组表达式=Int((RowNumber(Nothing)-1)/10)分页符-->勾选在组的结尾. 2. ...

  8. 使用equals方法时,要注意

    这是我在项目中犯的一个低级错误: 使用equals方法时,要注意这个方法是boolean java.lang.String.equals(Object anObject)传递的是Object,所以传任 ...

  9. Maven 使用国内镜像

    1 修改maven 的配置文件 settings.xml,添加阿里云的一个中央仓库. 2 找到maven 的配置文件,一般在 maven 安装目录 apache-maven-3.5.0\conf 文件 ...

  10. 修改 Semantic UI 中对 Google 字体的引用

    在第一次尝试 Semantic UI 后,发现其 css 中第一行,就引用了 fonts.googleapis.com 中的字体. 不知道为什么要这么做,也许在国外,google 的服务已经是一种互联 ...