单隐藏层全连接神经网络实现

单个神经元模型

在这里插入图片描述

常见激活函数

  • sigmoid函数
    σ ( x ) = 1 1 + e − x \sigma(x) = \frac{1}{1 + e^{-x}} σ(x)=1+ex1
    在这里插入图片描述
  • 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实践

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值