概述
分享朋友的机器学习应用案例:使用机器学习实现财富自由www.abuquant.com
Tesnroflow人工神经网络
import numpy as np
import tensorflow as tf
from tensorflow.examples.tutorials import mnist
from IPython.display import display, HTML
import matplotlib.pyplot as plt
读取数据集
mnist_data = mnist.input_data.read_data_sets('/data/mnist', one_hot=True) # one_hot 是 y是否one-hot表示
Extracting /data/mnist/train-images-idx3-ubyte.gz
Extracting /data/mnist/train-labels-idx1-ubyte.gz
Extracting /data/mnist/t10k-images-idx3-ubyte.gz
Extracting /data/mnist/t10k-labels-idx1-ubyte.gz
# 检查数据维度情况
display('train image shape:')
display(mnist_data.train.images.shape)
display('label y shape')
display(mnist_data.train.labels.shape)
'train image shape:'
(55000, 784)
'label y shape'
(55000, 10)
# 从上面可以看出一个image是 1*784的一维向量, label是10个分类中的一个
# 再来看看一个图像究竟是张的什么样子
def plot_mnist(image_array):
"""
根据手写识别的数组来进行输出最终的手写识别图片
:param image_array: 手写识别m*n数组
:return:
"""
fig = plt.figure()
plt.imshow(image_array, cmap='gray')
plt.show()
image_index = 1 # 取第一章图片看看
image = mnist_data.train.images[image_index]
image = image.reshape(28, 28)
plot_mnist(image)
# 看看label
display(mnist_data.train.labels[image_index])
array([ 0., 0., 0., 1., 0., 0., 0., 0., 0., 0.])
构建图
# 1 准备place holder用来传入数据使用
# x, y的shape都使用是None, 这个值就是一个可变的batch_size
X = tf.placeholder(tf.float32, shape=[None, 784], name='X_placeholder')
Y = tf.placeholder(tf.float32, shape=[None, 10], name='Y_placeholder')
# 2 设置变量,这里使用2个隐层+一个输出层来设置变量,所以就是3个W和3个bias
n_hidden_1 = 256 # 第1个隐层
n_hidden_2 = 256 # 第2个隐层
n_input = 784 # 输入
n_classes = 10 # 分类
weights = {
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1]), name='W1'),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2]), name='W2'),
'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]), name='W')
}
bias = {
'b1': tf.Variable(tf.zeros([n_hidden_1]), name='b1'),
'b2': tf.Variable(tf.zeros([n_hidden_2]), name='b2'),
'out': tf.Variable(tf.zeros([n_classes]), name='bias')
}
# 3 构建前向网络
def multilayer(x, weights, bias):
'''
定义前向网络函数
'''
layer1 = tf.add(tf.matmul(x, weights['h1']), bias['b1'], name='fc_1')
layer1 = tf.nn.relu(layer1, name='relu_1')
layer2 = tf.add(tf.matmul(layer1, weights['h2']), bias['b2'], name='fc_2')
layer2 = tf.nn.relu(layer2, name='relu_2')
out_layer = tf.add(tf.matmul(layer2, weights['out']), bias['out'], name='fc_out')
return out_layer
## 预测函数
y_pred = multilayer(X, weights, bias)
## 4 构建损失损失函数
loss_all = tf.nn.softmax_cross_entropy_with_logits(logits=y_pred, labels=Y, name='cross_entroyp_loss')
loss = tf.reduce_mean(loss_all)
learning_rate = 0.001
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
运行图
batch_size = 128
batch_n = (int)(mnist_data.train.num_examples / batch_size)
print 'batch_n: %d, examples num: %d, batch size: %d' % (batch_size, mnist_data.train.num_examples, batch_size)
with tf.Session() as sess:
writer = tf.summary.FileWriter('./graphs/dnn', sess.graph)
# 初始化所有变量
sess.run(tf.global_variables_initializer())
loss_avg = 0
for epoch in xrange(15): # 训练15轮
# 分batch训练
for i in xrange(batch_n):
# 获取batch数据
batch_x, batch_y = mnist_data.train.next_batch(batch_size)
_, l = sess.run([optimizer, loss], feed_dict={X: batch_x, Y: batch_y})
loss_avg += l
loss_avg = (loss_avg / batch_n)
print('epoch: %d, loss: %f' % (epoch, loss_avg))
print('train finished')
# 在测试集上评估
correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(Y, 1))
# 计算准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("Accuracy:", accuracy.eval({X: mnist_data.test.images, Y: mnist_data.test.labels}))
writer.close()
batch_n: 128, examples num: 55000, batch size: 128
epoch: 0, loss: 197.916901
epoch: 1, loss: 46.898612
epoch: 2, loss: 29.826258
epoch: 3, loss: 21.645072
epoch: 4, loss: 16.336597
epoch: 5, loss: 12.452627
epoch: 6, loss: 9.764067
epoch: 7, loss: 7.585990
epoch: 8, loss: 6.106027
epoch: 9, loss: 4.710732
epoch: 10, loss: 3.626024
epoch: 11, loss: 2.922296
epoch: 12, loss: 2.199572
epoch: 13, loss: 1.759399
epoch: 14, loss: 1.413137
train finished
('Accuracy:', 0.94349998)
最后
以上就是霸气茉莉为你收集整理的第三课 Tensorflow实现人工神经网络Tesnroflow人工神经网络的全部内容,希望文章能够帮你解决第三课 Tensorflow实现人工神经网络Tesnroflow人工神经网络所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复