MNIST
MNIST是机器学习领域的一个经典问题。并且在tensorflow中有数据实例,不需要自己去整理数据。为了更好的理解神经网络,自己按照书上写了做了一遍实验。
数据介绍
每一个MNIST数据单元均由两部分组成:一张包含手写数字的图片(xs)和一个对应的标签(ys)。
训练集:60000行(mnist.train.image and label) 测试集:10000行(mnist.test.image and label)
数据处理
数字图片采用28×28的像素矩阵存储的,我们只需要保证,每个像素矩阵的读取方式是相同的即可。
代码实现
下面用 TensorFlow 实现一个含有两个隐藏的神经网络来进行训练。
顺便提一句,我用的是Anaconda3.7 ,TensorFlow安装,采用语句conda install tensorflow
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# 初始化权重参数
def init_weights(shape):
return tf.Variable(tf.random_normal(shape,stddev=0.01))
# 定义神经网络模型
def model(X,w_h,w_h2,w_o,p_keep_input,p_keep_hidden):
# 隐藏层1
X = tf.nn.dropout(X,p_keep_input)
h = tf.nn.relu(tf.matmul(X