用Mnist数据集训练神经网络

用Mnist数据集训练神经网络

这篇博客是我在学习用tensorflow搭建神经网络时,所作的一些笔记。搭建的神经网络有两层隐藏层,和输入输出层。采用全连接的方式进行传输,优化算法采用自适用矩估计算法。
1.首先,导入tensorflow官方提供的库

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

2.设置输入:

x = tf.placeholder(tf.float32,[None,784],name='x_input')
y_ = tf.placeholder(tf.float32,[None,10],name='y_input')

mnist数据集中的图片为2828个像素点,并且每次训练都是小批量同时输入,所以输入的维度设置为[None,2828],同时可以用batch_size 代替None。

3.定义参数

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

为了提高代码的可读性和简洁性,定义了两个方法,分别来定义参数w和b。

4.运算

#第一层
net = tf.nn.relu(tf.matmul(x,w1)+b1)
#net = tf.nn.dropout(net,0.25)
#第二层
net = tf.matmul(net,w2)+b2
y = tf.nn.softmax(net)

对于激活函数采用relu,去线性。

5.损失函数和优化算法

loss = -tf.reduce_mean(tf.reduce_sum(y_*tf.log(y)))

optim = tf.train.AdamOptimizer(0.001).minimize(loss)

6.进行批量训练

with tf.Session() as sess:
        tf.global_variables_initializer().run()
        for i in range(10000):           
            xs,ys = mnist.train.next_batch(batch_size)
            _,l,accu=sess.run([optim,loss,accuracy],feed_dict={x:xs,y_:ys})
            if(i%100==0):
                print('after %d times,the loss is %g'%(i,l))
                print('the modle accuracy is %g '%(accu))

总结
这个代码量和理解并不难,主要用于练手,提高对tensorflow的掌握,熟悉tensorflow的函数。
附上代码

# -*- coding: utf-8 -*-
"""
Created on Tue Sep  3 16:09:17 2019

@author: ASUS
"""

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np

batch_size =100
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)

x = tf.placeholder(tf.float32,[None,784],name='x_input')
y_ = tf.placeholder(tf.float32,[None,10],name='y_input')

w1 = tf.get_variable('w1',shape=[784,300],initializer=tf.truncated_normal_initializer(stddev=0.1))
b1 = tf.Variable(tf.zeros([300]))

w2 = tf.get_variable('w2',shape=[300,10],initializer=tf.truncated_normal_initializer(stddev=0.1))
b2 = tf.Variable(tf.zeros([10]))


net = tf.nn.relu(tf.matmul(x,w1)+b1)
#net = tf.nn.dropout(net,0.25)

net = tf.matmul(net,w2)+b2
y = tf.nn.softmax(net)

loss = -tf.reduce_mean(tf.reduce_sum(y_*tf.log(y)))

optim = tf.train.AdamOptimizer(0.001).minimize(loss)
    
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

def train():
    with tf.Session() as sess:
        tf.global_variables_initializer().run()
        for i in range(10000):           
            xs,ys = mnist.train.next_batch(batch_size)
            _,l,accu=sess.run([optim,loss,accuracy],feed_dict={x:xs,y_:ys})
            if(i%100==0):
                print('after %d times,the loss is %g'%(i,l))
                print('the modle accuracy is %g '%(accu))
    
def main(argv=None):
    train()
    
if __name__ == '__main__':
    tf.app.run()
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

storm岚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值