(一)人工神经网络
什么是人工神经网络
神经网络的种类
神经网络的特点
神经网络的构成
(二)构建神经网络相关API
one-hot编码API
全连接
计算损失(交叉熵)
梯度下降
准确度计算
(三)Mnist数据集浅层网络分析案例
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_integer("is_train", 1, "指定程序是预测还是训练")
def full_connected():
# 获取真实的数据
mnist = input_data.read_data_sets("./data/mnist/input_data/", one_hot=True)
# 1、建立数据的占位符 x [None, 784] y_true [None, 10]
with tf.variable_scope("data"):
x = tf.placeholder(tf.float32, [None, 784])
y_true = tf.placeholder(tf.int32, [None, 10])
# 2、建立一个全连接层的神经网络 w [784, 10] b [10]
with tf.variable_scope("fc_model"):
# 随机初始化权重和偏置
weight = tf.Variable(tf.random_normal([784, 10], mean=0.0, stddev=1.0), name="w")
bias = tf.Variable(tf.constant(0.0, shape=[10]))
# 预测None个样本的输出结果matrix [None, 784]* [784, 10] + [10] = [None, 10]
y_predict = tf.matmul(x, weight) + bias
# 3、求出所有样本的损失,然后求平均值
with tf.variable_scope("soft_cross"):
# 求平均交叉熵损失
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict))
# 4、梯度下降求出损失
with tf.variable_scope("optimizer"):
train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss