optimizer = tf.train.AdamOptimizer().minimize(cost)
今天将进行第一次实战,带大家体验机器学习界的“hello word”,mnist手写数字识别系统的实现。博主机器近期不知道发了什么疯,windows下的tensorflow再次无法运行,无奈之下只得屈服- -,用虚拟机装载linux系统,在linux系统下执行,具体安装方法很简单,详情见官方文档
https://www.tensorflow.org/versions/r0.12/get_started/index.html
这里不多做赘述。
第一次使用linux系统,按照教程下载了subline进行代码编写。
根据之前的神经网络介绍,下面开始吧~我将详述每行代码的作用,方便大家理解。
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
minist = input_data.read_data_sets("/tmp/data/",one_hot = True)
首先,加载我们的rensorflow库,库里面已经自带了mnist的数据,将数据下载并存入minist变量中(这里我不小心拼错了,懒得改了,见谅- -!)
这里one_hot表示独热编码,即一位有效编码,例如本次实验的输出为0-9十个数,分别表示为:
0 = [1,0,0,0,0,0,0,0,0,0]
1 = [0,1,0,0,0,0,0,0,0,0]
2 = [0,0,1,0,0,0,0,0,0,0]
。。。
读取完了数据,我们需要设置参数,这样写的好处就是方便调整网络参数,分别定义出各个各个隐藏层的神经元个数。
继续上我们这个老图,我们需要设置两个变量,一个权重weight,一个偏置biases
hidden_1layer = {'weights':tf.Variable(tf.random_normal([784,n_nodes_hdl1])),'biases':tf.Variable(tf.random_normal([n_nodes_hdl1]))}
定义隐藏层1有两个变量,分别取随机矩阵,维度行为前一层的输出个数,列为本层的输出个数,即神经元个数
设置有3个隐藏层,外加一个输出层,输出层代码如下:
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hdl3,n_classes])),'biases':tf.Variable(tf.random_normal([n_classes]))}
随机矩阵中行为第三个隐藏层的输出个数,列为总输出个数,这里n_class为10 即(0-9个数)
申明完了变量,接下来需要申明算法,由于python语言计算比较慢,直接用python计算需要消耗很多时间,利用tensorflow定义完计算方式通过c++来完成计算。
算法很简单,如上图所示,active_function(input * weight + biases) = output
这里激励函数选择relu函数
l2 = tf.add(tf.matmul(l1,hidden_2layer['weights']) , hidden_2layer['biases'])
l2 = tf.nn.relu(l2)
输出层计算代码如下
output = tf.matmul(l3,output_layer['weights']) + output_layer['biases']
第三个隐藏层的输出*输出层权重加上输出层偏置,返回最终输出
这是一个神经网络正向传递过程,接下来误差反向传递
用交叉熵函数定义代价函数,来表示输出与实际label的差别
softmax_cross_entropy_with_logits 这个函数把softmax与交叉熵函数结合在一起,softmax函数的作用相当于把每个输出量化为一个概率,这里返回一个向量,需要求均值得出实际输出与期望输出的损失。
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction,y))
之前博客中提到,为了使损失降到最小反向更新权重以及偏置,这里tensorflow已经有很多最优化函数,这里选择AdamOptimizer()
optimizer = tf.train.AdamOptimizer().minimize(cost)
全部定义完毕后,将训练数据分离,将变量,代价函数,最优控制器放入session中执行即可。
结果如下
总共执行
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
minist = input_data.read_data_sets("/tmp/data/",one_hot = True) #one_hot means put the input data transfor '0' or '1'
'''
input > weight >hidden layer 1 (activation function) > weights > hidden l 2..
cross entropy(how not fit of the moddel)
optimization function to minmize the cost
BP NN
'''
# 10 class ,0-9
'''
one_hot means
0 = [1,0,0,0,0,0,0,0,0,0]
1 = [0,1,0,0,0,0,0,0,0,0]
'''
# first build the number of neural
n_nodes_hdl1 = 500
n_nodes_hdl2 = 500
n_nodes_hdl3 = 500
#each hidden layer has 500 neural nodes
n_classes = 10
batch_size = 100 #to fit the weight about 100 times ???
x = tf.placeholder('float')#placeholder to define the pre-input data ,just give the type and shape,the 2nd paremeter is to leave something thire
y = tf.placeholder('float')
def neural_network_model(data):
hidden_1layer = {'weights':tf.Variable(tf.random_normal([784,n_nodes_hdl1])),'biases':tf.Variable(tf.random_normal([n_nodes_hdl1]))}
hidden_2layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hdl1,n_nodes_hdl2])),'biases':tf.Variable(tf.random_normal([n_nodes_hdl2]))}
hidden_3layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hdl2,n_nodes_hdl3])),'biases':tf.Variable(tf.random_normal([n_nodes_hdl3]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hdl3,n_classes])),'biases':tf.Variable(tf.random_normal([n_classes]))}
l1 = tf.add(tf.matmul(data,hidden_1layer['weights']) , hidden_1layer['biases'])
l1 = tf.nn.relu(l1) #active function reli function
l2 = tf.add(tf.matmul(l1,hidden_2layer['weights']) , hidden_2layer['biases'])
l2 = tf.nn.relu(l2)
l3 = tf.add(tf.matmul(l2,hidden_3layer['weights']) , hidden_3layer['biases'])
l3 = tf.nn.relu(l3)
output = tf.matmul(l3,output_layer['weights']) + output_layer['biases']
return output
def train_neural_network(x):
prediction = neural_network_model(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction,y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
hm_epochs = 10
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for epoch in range(hm_epochs):
epoch_loss = 0
for _ in range(int(minist.train.num_examples/batch_size)):
epoch_x,epoch_y = minist.train.next_batch(batch_size)
_,c = sess.run([optimizer,cost], feed_dict = {x: epoch_x,y: epoch_y})
epoch_loss += c
print('Epoch',epoch,'completed out of',hm_epochs,'loss',epoch_loss)
correct = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct,'float'))
print('accuracy',accuracy.eval({x:minist.test.images, y:minist.test.labels}))
train_neural_network(x)
短短70行就能实现一个手写数字识别系统,可见python的强大!