tensorflow +mnist

根据youtube上sendex大神视频所敲写的代码:

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist=input_data.read_data_sets("/tmp/data",one_hot=True)

n_nodes_hl1=500
n_nodes_hl2=500
n_nodes_hl3=500

n_classes=10
batch_size=100

x=tf.placeholder('float',[None,784])#flat out the data
y=tf.placeholder('float')

def neural_network_model(data):
    hidden_1_layer={'weights':tf.Variable(tf.random_normal([784,n_nodes_hl1])),
                    'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
    hidden_2_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])),
                    'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
    hidden_3_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl2,n_nodes_hl3])),
                    'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}
    output_layer={'weights':tf.Variable(tf.random_normal([n_nodes_hl3,n_classes])),
                    'biases':tf.Variable(tf.random_normal([n_classes]))}
                    
    l1=tf.add(tf.matmul(data,hidden_1_layer['weights']),hidden_1_layer['biases'])
    l1=tf.nn.relu(l1)
    
    l2=tf.add(tf.matmul(l1,hidden_2_layer['weights']),hidden_2_layer['biases'])
    l2=tf.nn.relu(l2)
    
    l3=tf.add(tf.matmul(l2,hidden_3_layer['weights']),hidden_3_layer['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(logits=prediction,labels=y) )
    optimizer=tf.train.AdamOptimizer().minimize(cost)#也可以选择梯度下降法
    
    hm_epochs=10
    #所有样本处理一遍算一个epoch
    
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        
        for epoch in range(hm_epochs):
            epoch_loss=0
            for _ in range(int(mnist.train.num_examples/batch_size)):
                epoch_x,epoch_y=mnist.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:mnist.test.images,y:mnist.test.labels}))


train_neural_network(x)

运行结果:

Epoch 0 completed out of 10 loss 1804656.12052
Epoch 1 completed out of 10 loss 401965.048153
Epoch 2 completed out of 10 loss 217808.414017
Epoch 3 completed out of 10 loss 130326.765904
Epoch 4 completed out of 10 loss 75227.516495
Epoch 5 completed out of 10 loss 46899.7264338
Epoch 6 completed out of 10 loss 32204.3892914
Epoch 7 completed out of 10 loss 23254.3602934
Epoch 8 completed out of 10 loss 18647.4183468
Epoch 9 completed out of 10 loss 17600.1437412
Accuracy: 0.9511

另外,使用KNN准确率为0.9664,。

文中某些参数参考:

http://blog.sina.com.cn/s/blog_155d29f010102wqj6.html

http://blog.csdn.net/mao_xiao_feng/article/details/53382790

http://blog.csdn.net/freedom098/article/details/51911473

mnist数据可视化的代码参考:

https://github.com/saradhix/mnist_visual



再来一个卷积神经训练的代码:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

import tensorflow as tf
import pandas as pd
from PIL import Image 
import numpy as np

sess = tf.InteractiveSession()

X = tf.placeholder(tf.float32, [None, 28*28])#用于CNN样本定义 160*60
Y = tf.placeholder(tf.float32, [None, 10])#用于CNN训练类别 5*63
keep_prob = tf.placeholder(tf.float32) # dropout 用于CNN类别输出防止过拟合时的过滤器

# 定义CNN
def crack_captcha_cnn(w_alpha=0.01, b_alpha=0.1):
	x = tf.reshape(X, shape=[-1, 28, 28, 1])#四维矩阵

	w_c1 = tf.Variable(w_alpha*tf.random_normal([3, 3, 1, 32]))#每次输入三张图,3*1,32个通道单元
	b_c1 = tf.Variable(b_alpha*tf.random_normal([32]))
	conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(x, w_c1, strides=[1, 1, 1, 1], padding='SAME'), b_c1))#same允许有边缘,因为只有两维,通常strides取[1,stride,stride,1],步长为1,
	conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')#max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似,两个1是atch和channel,2*2是池化窗口的大小
	conv1 = tf.nn.dropout(conv1, keep_prob)

	w_c2 = tf.Variable(w_alpha*tf.random_normal([3, 3, 32, 64]))
	b_c2 = tf.Variable(b_alpha*tf.random_normal([64]))
	conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(conv1, w_c2, strides=[1, 1, 1, 1], padding='SAME'), b_c2))
	conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
	conv2 = tf.nn.dropout(conv2, keep_prob)

	w_d = tf.Variable(w_alpha*tf.random_normal([7*7*64, 1024]))
	b_d = tf.Variable(b_alpha*tf.random_normal([1024]))
	dense = tf.reshape(conv2, [-1, w_d.get_shape().as_list()[0]])
	dense = tf.nn.relu(tf.add(tf.matmul(dense, w_d), b_d))
	dense = tf.nn.dropout(dense, keep_prob)

	w_out = tf.Variable(w_alpha*tf.random_normal([1024, 10]))
	b_out = tf.Variable(b_alpha*tf.random_normal([10]))
	out = tf.add(tf.matmul(dense, w_out), b_out)
	out = tf.nn.softmax(out)
	return out

# 训练
output = crack_captcha_cnn()
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(output, Y))
train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
predict = tf.reshape(output, [-1, 1, 10])
max_idx_p = tf.argmax(predict, 2)
max_idx_l = tf.argmax(tf.reshape(Y, [-1, 1, 10]), 2)
correct_pred = tf.equal(max_idx_p, max_idx_l)
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

sess.run(tf.initialize_all_variables())
for i in range(10000):
    batch = mnist.train.next_batch(50)
    if i%100 == 0:
        #验证的时候dropout=1.0,训练时=0.5
        train_accuracy = accuracy.eval(feed_dict={X:batch[0], Y: batch[1], keep_prob: 1.0})
        print ("step %d, training accuracy %g"%(i, train_accuracy))
    train_step.run(feed_dict={X: batch[0], Y: batch[1], keep_prob: 0.5})
print ("test accuracy %g"%accuracy.eval(feed_dict={
X: mnist.test.images, Y: mnist.test.labels, keep_prob: 1.0}))

batch=mnist.test.next_batch(50)
predict = tf.argmax(tf.reshape(output, [-1, 1, 10]), 2)
text_list = sess.run(predict, feed_dict={X:batch[0], keep_prob: 1})
print("正确: {}  预测: {}".format(text_list, batch[1]))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值