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

from __future__ import print_function
from tensorflow.examples.tutorials.mnist import input_data
#加载数据集
mnist = input_data.read_data_sets(r"C:/Users/HPBY/tem/data/",one_hot=True)#加载本地数据 以独热编码形式
import tensorflow as tf
#设置超参
learning_rate = 0.01 #设置学习率
num_step = 500 #训练次数
batch_size = 128 #批次 
display_step =100 #多少次显示一次结果

#设置网络参数
n_hidden_1 = 256  #隐含层1 256节点
n_hidden_2 = 256  #隐含层2  256节点
num_inputs = 784  #输入一位向量28*28
num_class = 10    #0-9的数字一共10个分类

X = tf.placeholder("float",[None, num_inputs]#占位符784输入 10输出
Y = tf.placeholder("float",[None, num_class])
# 储存网络层权重和偏置值 
weights={#随机初始化并权重和偏置值
    'h1' : tf.Variable(tf.random_normal([num_inputs, n_hidden_1])),
    'h2' : tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out' : tf.Variable(tf.random_normal([n_hidden_2, num_class]))
}

biases = {
    'b1' : tf.Variable(tf.random_normal([n_hidden_1])),
    'b2' : tf.Variable(tf.random_normal([n_hidden_2])),
    'out' :tf.Variable(tf.random_normal([num_class]))
}
#创建模型
def neural_net(x):
    #全连接隐含层1,2隐含层256个节点
    layer_1 = tf.add(tf.matmul(x,weights['h1']), biases['b1'])#matmul是计算
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']),biases['b2'])
    out_layer = tf.matmul(layer_2, weights['out'])+biases['out']
    return out_layer
#构建模型
logits = neural_net(X)

#定义损失函数和优化器
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
    logits=logits, labels=Y))                         
opt = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = opt.minimize(loss_op)

#评价模型
correct_pred = tf.equal(tf.argmax(logits,1), tf.argmax(Y, 1))  
accuracy =  tf.reduce_mean(tf.cast(correct_pred, tf.float32))


#初始化变量
init = tf.global_variables_initializer()
#开始训练
with tf.Session() as sess:
    sess.run(init)
    
    for step in range(1,num_step+1):
        batch_x, batch_y =mnist.train.next_batch(batch_size)
        
        sess.run(train_op,feed_dict={X:batch_x, Y:batch_y})
        
        if step % display_step == 0 or step == 1:
            loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,
                                                                 Y: batch_y})
            print("Step " + str(step) + ", Minibatch Loss= " + \
                  "{:.4f}".format(loss) + ", Training Accuracy= " + \
                  "{:.3f}".format(acc))

    print("Optimization Finished!")

    # Calculate accuracy for MNIST test images
    print("Testing Accuracy:", \
        sess.run(accuracy, feed_dict={X: mnist.test.images,
                                      Y: mnist.test.labels}))

数据集来自 http://yann.lecun.com/exdb/mnist/ 以本地加载方式加载数据集

 

神经网络模型如下:

 

 

 独热编码参考https://www.cnblogs.com/zongfa/p/9305657.html

很简单的一种编码方式也经常用到

比如我们有“今天刀塔本子出了吗”这个形式的9个不同的词,那么我们独热编码就会形成一个九维的向量,

今是第1个词表示的向量为[1,0,0,0,0,0,0,0,0]

刀是第3个词表示的向量为[0,0,1,0,0,0,0,0,0]

神经网络原理与推导参考程序媛小姐姐的BP神经网络讲解,非常详细:http://www.cnblogs.com/charlotte77/p/5629865.html

转载于:https://www.cnblogs.com/laohejb/p/10724316.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值