Python中一些具体的项目,比如游戏开发,爬虫等,由于与数据挖掘关系不是特别密切,开始转战机器学习和TensorFlow
第一个TensorFlow项目,用神经网络进行手写数字的识别
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)
#每个批次的大小
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob=tf.placeholder(tf.float32)
#增加隐藏层
W1 = tf.Variable(tf.truncated_normal([784,2000],stddev=0.1))#生成标准差为0.1的正态分布数据
b1 = tf.Variable(tf.zeros([2000])+0.1)
L1 = tf.nn.relu(tf.matmul(x,W1)+b1)#激活函数的一种
L1_drop=tf.nn.dropout(L1,keep_prob)#防止过拟合
#创建一个简单的神经网络
W = tf.Variable(tf.truncated_normal([2000,10],stddev=0.1))
b = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(L1,W)+b)
#二次代价函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#使用梯度下降法
train_step = tf.train.MomentumOptimizer(0.2,0.9).minimize(loss)
#初始化变量
init = tf.global_variables_initializer()
#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
with tf.Session() as sess:
sess.run(init)
for epoch in range(21):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.1})
acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Iter " + str(epoch) + ",Testing Accuracy " + str(acc))
MNIST_data数据集需要提前下载完成放在该程序所在文件夹。
本程序的精度能够达到百分之九十八以上
Iter 0,Testing Accuracy 0.5796 Iter 1,Testing Accuracy 0.7614 Iter 2,Testing Accuracy 0.9502 Iter 3,Testing Accuracy 0.9634 Iter 4,Testing Accuracy 0.9719 Iter 5,Testing Accuracy 0.9736 Iter 6,Testing Accuracy 0.9769 Iter 7,Testing Accuracy 0.9781 Iter 8,Testing Accuracy 0.9784 Iter 9,Testing Accuracy 0.9796 Iter 10,Testing Accuracy 0.9804 Iter 11,Testing Accuracy 0.9809 Iter 12,Testing Accuracy 0.981 Iter 13,Testing Accuracy 0.9807 Iter 14,Testing Accuracy 0.9829 Iter 15,Testing Accuracy 0.9825 Iter 16,Testing Accuracy 0.982 Iter 17,Testing Accuracy 0.9833 Iter 18,Testing Accuracy 0.9824 Iter 19,Testing Accuracy 0.9828 Iter 20,Testing Accuracy 0.983