您的位置:首页 > 理论基础 > 计算机网络

基于Tensorflow的机器学习(5) -- 全连接神经网络

2017-10-22 22:17 671 查看
这篇博客将实现的主要神经网络如下所示:



以下是相关代码的实现步骤:

简单化的实现

导入必要内容

# Import MNIST data
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)


参数初始化

# Parameters
learning_rate = 0.1
num_steps = 500
batch_size = 128
display_step = 100

# Network Parameters
n_hidden_1 = 256 # 1st layer number of neurons
n_hidden_2 = 256 # 2nd layer number of neurons
num_input = 784 # MNIST data input (img shape: 28*28)
num_classes = 10 # MNIST total classes (0-9 digits)

# tf Graph Input
X = tf.placeholder("float", [None, num_input])
Y = tf.placeholder("float", [None, num_classes])


此处每个隐藏层都有256个神经元,输入是通过图片转换而来的784维数组,一共将所有数据分为0-9这10个类。

存储weights 和 bias

# Store layers weight & bias
weights = {
'h1' : tf.Variable(tf.random_normal([num_input, 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_classes]))
}
# 为何我们要将上述的两个变量以矩阵的形式进行传输
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_classes]))
}


模型创建

# Create model
def neural_net(x):
# Hidden fully connnected layer with 256 neurons
layer_1 = tf.add(tf.matmul(x, weights['h1']) , biases['b1'])
# Hidden fully connnected layer with 256 neurons
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']) , biases['b2'])
# Output fully connected layer with a neuron for each class
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return out_layer


模型构建

# Create model
def neural_net(x):
# Hidden fully connnected layer with 256 neurons
layer_1 = tf.add(tf.matmul(x, weights['h1']) , biases['b1'])
# Hidden fully connnected layer with 256 neurons
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']) , biases['b2'])
# Output fully connected layer with a neuron for each class
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return out_layer


模型训练

# Start training
with tf.Session() as sess:
# Run the initilizer
sess.run(init)

for step in range(1, num_steps+1):
batch_x, batch_y = mnist.train.next_batch(batch_size)
# Run optimization op (backporp)
sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})
if step % display_step == 0 or step == 1:
# Calculate batch loss and accuracy
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("Testiing Accuracy:", \
sess.run(accuracy, feed_dict={X: mnist.test.images, Y: mnist.test.labels}))


结果输出:

Step 1, Minibatch Loss= 10718.8223, Training Accuracy= 0.289
Step 100, Minibatch Loss= 254.0072, Training Accuracy= 0.852
Step 200, Minibatch Loss= 91.2099, Training Accuracy= 0.859
Step 300, Minibatch Loss= 65.1114, Training Accuracy= 0.859
Step 400, Minibatch Loss= 48.7690, Training Accuracy= 0.891
Step 500, Minibatch Loss= 13.4156, Training Accuracy= 0.922
Optimization Finished!
('Testiing Accuracy:', 0.8648001)


全连接网络高级实现

以下实例将使用tensorflow的 ‘layers’ 和 ‘estimator’的API去构建上述的全连接网络。

具体实现步骤如下:

导入必要内容

# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data", one_hot=False)

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np


参数初始化

# Parameters
learning_rate = 0.01
num_steps = 1000
batch_size = 128
display_step = 100

# Network Parameters
n_hidden_1 = 256 # 1st layer number of neurons
n_hidden_2 = 256 # 2nd layer number of neurons
num_input = 784 # MNIST data input( img shape: 28*28)
num_classes = 10 # MNIST total classes (0-9 digits)


定义输入函数

# Define the input function for training
input_fn = tf.estimator.inputs.numpy_input_fn(
x={'images': mnist.train.images}, y=mnist.train.labels,
batch_size=batch_size, num_epochs=None, shuffle=True)


稍后对estimator的API进行具体分析,此处只需要记住相关用法即可

定义神经网络

# Define the neural network
def neural_net(x_dict):
# TF Estimator input is a dict, in case of multiple inputs
x = x_dict['images']
# Hidden fully connected layer with 256 neurons
layer_1 = tf.layers.dense(x, n_hidden_1)
# Hidden fully connected layer with 256 neurons
layer_2 = tf.layers.dense(layer_1, n_hidden_2)
# Output fully connected layer with a neuron for each class
out_layer = tf.layers.dense(layer_2, num_classes)
return out_layer


定义模型函数

# Define the model function (following TF Estimator Template)
def model_fn(features, labels, mode):

# Build the neural network
logits = neural_net(features) # features 是个啥

# Predictions
pred_classes = tf.argmax(logits, axis=1)
pred_probas = tf.nn.softmax(logits)

# If prediction mode, early return
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)

# Define loss and optimizer
loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits, labels=tf.cast(labels, dtype=tf.int32)))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op, global_step=tf.train.get_global_step())
# 什么是global step??

# Evaluate the accuracy of the model
acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)

# TF Estimators requires to return a EstimatorSpec, that specify
# the different ops for training, evaluating
estim_specs = tf.estimator.EstimatorSpec(
mode=mode,
predictions=pred_classes,
loss=loss_op,
train_op=train_op,
eval_metric_ops={'accuracy': acc_op})

return estim_specs


建立estimator

# Build the Estimator
model = tf.estimator.Estimator(model_fn)


模型训练

# Train the Model
model.train(input_fn, steps=num_steps)


模型评估

# Evaluate the Model
# Define the input function for evaluating
input_fn = tf.estimator.inputs.numpy_input_fn(
x={'images': mnist.test.images}, y=mnist.test.labels,
batch_size=batch_size, shuffle=False)
# Use the Estimator 'evaluate' method
model.evaluate(input_fn)


输出结果为:

{'accuracy': 0.9091, 'global_step': 2000, 'loss': 0.31571656}


单图片预测

# Predict single images
n_images = 5
# Get images from test set
test_images = mnist.test.images[:n_images]
# Prepare the input data
input_fn = tf.estimator.inputs.numpy_input_fn(
x={'images': test_images}, shuffle=False)
# Use the model to predict the images class
preds = list(model.predict(input_fn))

# Display
for i in range(n_images):
plt.imshow(np.reshape(test_images[i], [28,28]), cmap='gray')
plt.show()
print("Model prediction: ", preds[i])


以上便是基于tensorflow的全连接网络的理论及其应用的全部内容。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐