单个神经元模型
常见激活函数
- sigmoid函数
σ ( x ) = 1 1 + e − x \sigma(x) = \frac{1}{1 + e^{-x}} σ(x)=1+e−x1
- tanh 双曲正切函数
t a n h ( x ) tanh(x) tanh(x)
- ReLu 修正线性单元函数
m a x ( 0 , x ) max(0, x) max(0,x)
多层神经元
全连接单隐藏层神经网路
全连接双隐藏层神经网络
全连接单隐藏层神经网络建模实现
载入数据
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("/Users/liuqi/Desktop/data/MNIST_data/", one_hot=True)
建立模型
构建输入层
定义标签数据占位符
# x为样本输入的占位符 y为标签值的占位符
x = tf.placeholder(tf.float32, [None, 784], name = "X")
y = tf.placeholder(tf.float32, [None, 10], name = "Y")
构建隐藏层
# 隐藏层神经元数量
H1_NN = 256
W1 = tf.Variable(tf.random_normal([784, H1_NN]))
b1 = tf.Variable(tf.zeros([H1_NN]))
Y1 = tf.nn.relu(tf.matmul(x, W1) + b1)
构建输出层
W2 = tf.Variable(tf.random_normal([H1_NN, 10]))
b2 = tf.Variable(tf.zeros([10]))
forward = tf.matmul(Y1, W2) + b2
pred = tf.nn.softmax(forward)
训练模型
定义损失函数
# # 交叉熵损失函数
# loss_function = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred), reduction_indices = 1))
# 以上的损失函数由于log(0)引起的数据的不稳定,造成数据结果不正确
# TensorFlow提供了结合Softmax的交叉熵损失函数定义方法
# TensorFlow提供了softmax_cross_entropy_with_logits
# 用于避免因为log(0)值为Nan造成的数据不稳定
loss_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=forward, labels = y))
设置训练参数
train_epochs = 40
batch_size = 50
total_batch = int(mnist.train.num_examples / batch_size)
display_step = 1
learning_rate = 0.01
选择优化器
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss_function)
定义准确率
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(pred, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
训练模型
# 记录开始训练的时间
from time import time
startTime = time()
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for epoch in range(train_epochs):
for batch in range(total_batch):
xs, ys = mnist.train.next_batch(batch_size)
sess.run(optimizer, feed_dict={x: xs, y: ys})
# 一轮训练完后 使用验证数据集计算误差和准确率
loss, acc = sess.run([loss_function, accuracy], feed_dict={x: mnist.validation.images, y: mnist.validation.labels})
if (epoch + 1) % display_step == 0:
print("Train Epoch:", "%02d" % (epoch + 1), "Loss=", "{:.9f}".format(loss), "Accuracy=", "{:.4f}".format(acc))
# 显示运行总时间
duration = time() - startTime
print("Trian Finished takes:", "{:.2f}".format(duration))
评估模型
accu_test = sess.run(accuracy, feed_dict={x: mnist.test.images, y:mnist.test.labels})
print("Test Accuracy:", accu_test)
进行预测
# 由于pred预测结果是one-hot编码格式,所以需要转换为0-9的数字
prediction_result = sess.run(tf.argmax(pred, 1), feed_dict={x:mnist.test.images})
# 查看预测结果中的前十项
prediction_result[0:10]
找出预测错误的结果
import numpy as np
def print_predict_errs(labels, prediction):
count = 0
compare_list = prediction_result == np.argmax(mnist.test.labels, 1)
error_list = [i for i in range(len(compare_list)) if compare_list[i] == False]
for x in error_list:
print("index = " + str(x) + "标签值 = ", np.argmax(labels[x]), "预测值 = ", prediction[x])
print_predict_errs(labels=mnist.test.labels, prediction=prediction_result)
参考视频:深度学习应用开发TensorFlow实践