基于Tensorflow的机器学习(5) -- 全连接神经网络
来源:互联网 发布:社交网络人力资源 编辑:程序博客网 时间:2024/05/29 11:41
这篇博客将实现的主要神经网络如下所示:
以下是相关代码的实现步骤:
简单化的实现
导入必要内容
# Import MNIST dataimport tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
参数初始化
# Parameterslearning_rate = 0.1num_steps = 500batch_size = 128display_step = 100# Network Parametersn_hidden_1 = 256 # 1st layer number of neuronsn_hidden_2 = 256 # 2nd layer number of neuronsnum_input = 784 # MNIST data input (img shape: 28*28)num_classes = 10 # MNIST total classes (0-9 digits)# tf Graph InputX = tf.placeholder("float", [None, num_input])Y = tf.placeholder("float", [None, num_classes])
此处每个隐藏层都有256个神经元,输入是通过图片转换而来的784维数组,一共将所有数据分为0-9这10个类。
存储weights 和 bias
# Store layers weight & biasweights = { '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 modeldef 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 modeldef 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 trainingwith 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.289Step 100, Minibatch Loss= 254.0072, Training Accuracy= 0.852Step 200, Minibatch Loss= 91.2099, Training Accuracy= 0.859Step 300, Minibatch Loss= 65.1114, Training Accuracy= 0.859Step 400, Minibatch Loss= 48.7690, Training Accuracy= 0.891Step 500, Minibatch Loss= 13.4156, Training Accuracy= 0.922Optimization Finished!('Testiing Accuracy:', 0.8648001)
全连接网络高级实现
以下实例将使用tensorflow的 ‘layers’ 和 ‘estimator’的API去构建上述的全连接网络。
具体实现步骤如下:
导入必要内容
# Import MNIST datafrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("/tmp/data", one_hot=False)import tensorflow as tfimport matplotlib.pyplot as pltimport numpy as np
参数初始化
# Parameterslearning_rate = 0.01num_steps = 1000batch_size = 128display_step = 100# Network Parametersn_hidden_1 = 256 # 1st layer number of neuronsn_hidden_2 = 256 # 2nd layer number of neuronsnum_input = 784 # MNIST data input( img shape: 28*28)num_classes = 10 # MNIST total classes (0-9 digits)
定义输入函数
# Define the input function for traininginput_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 networkdef 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 Estimatormodel = tf.estimator.Estimator(model_fn)
模型训练
# Train the Modelmodel.train(input_fn, steps=num_steps)
模型评估
# Evaluate the Model# Define the input function for evaluatinginput_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' methodmodel.evaluate(input_fn)
输出结果为:
{'accuracy': 0.9091, 'global_step': 2000, 'loss': 0.31571656}
单图片预测
# Predict single imagesn_images = 5# Get images from test settest_images = mnist.test.images[:n_images]# Prepare the input datainput_fn = tf.estimator.inputs.numpy_input_fn( x={'images': test_images}, shuffle=False)# Use the model to predict the images classpreds = list(model.predict(input_fn))# Displayfor i in range(n_images): plt.imshow(np.reshape(test_images[i], [28,28]), cmap='gray') plt.show() print("Model prediction: ", preds[i])
以上便是基于tensorflow的全连接网络的理论及其应用的全部内容。
阅读全文
0 0
- 基于Tensorflow的机器学习(5) -- 全连接神经网络
- 基于Tensorflow的机器学习(6) -- 卷积神经网络
- TensorFlow学习笔记(5)——训练全连接神经网络
- TensorFlow学习_(2)基于TensorFlow的神经网络
- 【机器学习】动手写一个全连接神经网络(一)
- 基于神经网络的机器学习基础
- 基于Tensorflow的MNIST机器学习
- 机器学习的大局:用神经网络和TensorFlow分类文本
- TensorFlow学习记录--3.MNIST从低级到高级(从全连接网络到卷积神经网络的解释)
- 【opencv机器学习】基于SVM和神经网络的车牌识别
- 机器学习实验---人工神经网络MLP基于sklearn的实现
- 使用tensorflow实现全连接神经网络的简单示例,含源码
- 全连接的BP神经网络
- tensorflow实现基于深度学习的图像补全
- TensorFlow实现基于深度学习的图像补全
- 【机器学习】动手写一个全连接神经网络(二):线性回归
- 【机器学习】动手写一个全连接神经网络(三):分类
- 机器学习笔记8:基于TensorFlow的数据预测
- 快乐编程吧之2048小游戏
- 一步一步写算法(关于malloc函数)
- java:<显示两个字符串的相同前缀>
- 二叉树遍历
- 互联网背景下为什么会出现NoSQL?
- 基于Tensorflow的机器学习(5) -- 全连接神经网络
- 【数据结构】使用栈解决火车硬席(H)和软席(S)的调度问题
- 深入分析synchronized
- JAVA程序通过freemarker生成静态HTML页面
- 鬼谷子的钱袋
- JavaScript中数组的相关方法
- 171022 C++Tips
- virtualenv(python虚拟环境)
- LightOJ