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

时间:2023-03-09 05:21:16
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 = #训练次数
batch_size = #批次
display_step = #多少次显示一次结果 #设置网络参数
n_hidden_1 = #隐含层1 256节点
n_hidden_2 = #隐含层2 256节点
num_inputs = #输入一位向量28*
num_class = #-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,), tf.argmax(Y, ))
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(,num_step+):
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 == or step == :
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/ 以本地加载方式加载数据集

神经网络模型如下:

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

独热编码参考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