github:https://github.com/MichaelBeechan
CSDN:https://blog.csdn.net/u011344545
涉及代碼:https://github.com/MichaelBeechan/Learning_TensorFlow-Kaggle_MNIST 歡迎Fork和Star
Learning_TensorFlow-Kaggle_MNIS
一步步帶你通過項目(MNIST手寫識別)學習入門TensorFlow以及神經網絡的知識
**
TF_Variable:TensorFlow入門
**
# -*- coding:utf-8 -*-
"""
Name: Michael Beechan
School: Chongqing University of Technology
Time: 2018.10.4
Description: tensorflow變量初始化
https://baike.baidu.com/item/TensorFlow/18828108?fr=aladdin
"""
import tensorflow as tf
# 變量定義
w = tf.Variable([[0.5, 1.0]])
x = tf.Variable([[2.0], [1.0]])
# 矩陣乘法
y = tf.matmul(w, x)
print(y)# 函數
norm = tf.random_normal([2, 3], mean = -1, stddev = 4)
c = tf.constant([[1, 2], [3, 4], [5, 6]])
shuff = tf.random_shuffle(c) # shuffle洗牌
sess = tf.Session()
print(sess.run(norm))
print(sess.run(shuff))
# 將numpy的一些數據轉換為tensorflow能用的類型
import numpy as np
a = np.zeros((3, 3))
ta = tf.convert_to_tensor(a)
print(sess.run(ta))# 創建一個變量 并用for循環對變量進行賦值操作
num =tf.Variable(0, name="count")
new_value = tf.add(num, 10)
op = tf.assign(num, new_value)
print(op)
# 初始化全局變量
init_op = tf.global_variables_initializer()
# 定義運行會話
with tf.Session() as sess:sess.run(init_op)print(sess.run(num))for i in range(5):sess.run(op)print(sess.run(num))# 通過feed設置placeholder的值
# 聲明變量是不賦值,計算時進行賦值 使用feed
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
value_new = tf.multiply(input1, input2)with tf.Session() as sess:print(sess.run(value_new, feed_dict={input1:23.0, input2:11.0}))
**
Kaggle_mnist
**
使用softMax作為激活函數,交叉熵做損失函數,梯度下降法優化的單層神經網絡學習識別
準確率:88%左右
#-*- coding:utf-8 -*-
"""
Name: Michael Beechan
School: Chongqing University of Technology
Time: 2018.10.4
Description: Kaggle MINIST 手寫圖片識別 Digit Recognizer
http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html
"""
"""
一、數據的準備
二、模型的設計
三、代碼實現
28*28 = 784 的二維數組
訓練數據和測試數據,都可以分別轉化為[42000,769]和[28000,768]的數組
模型建立:
1)使用一個最簡單的單層的神經網絡進行學習
2)用SoftMax來做為激活函數
3)用交叉熵來做損失函數
4)用梯度下降來做優化方式
"""#88.45% 識別正確率
import pandas as pd
import numpy as np
import tensorflow as tf#加載數據
train = pd.read_csv("train.csv")
images = train.iloc[:, 1:].values
#labels_flat = train[[0]].values.ravel()
labels_flat = train.iloc[:, 0].values.ravel()#輸入處理
images = images.astype(np.float)
images = np.multiply(images, 1.0 / 255.0)
print("輸入數據的數量:(%g, %g)" % images.shape)
images_size = images.shape[1]
images_width = images_height = np.ceil(np.sqrt(images_size)).astype(np.uint8)
print("圖片的長 = {0}\n圖片的高 = {1}".format(images_width, images_height))x = tf.placeholder('float', shape=[None, images_size])#結果處理
labels_count = np.unique(labels_flat).shape[0]
print('結果的種類 = {0}'.format(labels_count))
y = tf.placeholder('float', shape=[None, labels_count])#One-Hot編碼 :離散特征處理——獨熱編碼 scikit_learn有封裝了現成的編碼函數OneHotEncoder()
def dense_to_one_hot(labels_dense, num_calsses):num_labels = labels_dense.shape[0]index_offset = np.arange(num_labels) * num_calsseslabels_one_hot = np.zeros((num_labels, num_calsses))#flat返回的是一個迭代器labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1return labels_one_hotlabels = dense_to_one_hot(labels_flat, labels_count)
labels = labels.astype(np.uint8)
print('結果的數量:({0[0]}, {0[1]})'.format(labels.shape))#數據劃分
VALIDATION_SIZE = 2000validation_images = images[:VALIDATION_SIZE]
validation_labels = labels[:VALIDATION_SIZE]train_images = images[VALIDATION_SIZE:]
train_labels = labels[VALIDATION_SIZE:]batch_size = 100
n_batch = len(train_images)//batch_size#建立神經網絡
weight = tf.Variable(tf.zeros([784, 10]))
biases = tf.Variable(tf.zeros([10]))
result = tf.matmul(x, weight) + biases
prediction = tf.nn.softmax(result)loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=prediction))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)init = tf.global_variables_initializer()correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))with tf.Session() as sess:sess.run(init)for epoch in range(50):for batch in range(n_batch):batch_x = train_images[batch * batch_size:(batch+1) * batch_size]batch_y = train_labels[batch * batch_size:(batch+1) * batch_size]sess.run(train_step, feed_dict={x:batch_x, y:batch_y})accuracy_n = sess.run(accuracy, feed_dict={x:validation_images, y:validation_labels})print("第"+str(epoch+1)+"輪,準確度為:" + str(accuracy_n))```**
CNN_mnist
卷積神經網絡——卷積層1+池化層1+卷積層2+池化層2+全連接1+Dropout層+輸出層
準確率:訓練20 accuracy is 0.984
#-*- coding:utf-8 -*-
"""
Name: Michael Beechan
School: Chongqing University of Technology
Time: 2018.10.4
Description: MINIST Digit Recognizer CNN
https://www.zhihu.com/question/52668301
"""
#卷積層1+池化層1+卷積層2+池化層2+全連接1+Dropout層+輸出層
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plot
from tensorflow.examples.tutorials.mnist import input_data
import pandas as pd#Add data
train = pd.read_csv("train.csv")
test = pd.read_csv("test.csv")#Get data and deal data astype()轉換數據類型
x_train = train.iloc[:, 1:].values
x_train = x_train.astype(np.float)
x_train = np.multiply(x_train, 1.0 / 255.0)#Get image width and height
image_size = x_train.shape[1]
images_width = images_height = np.ceil(np.sqrt(image_size)).astype(np.uint8)print('數據樣本大小:(%g, %g)' % x_train.shape)
print('圖像的維度大小:{0}'.format(image_size))
print('圖像長度:{0}\n高度:{1}'.format(images_width, images_height))#Get data labels
labels_flat = train.iloc[:, 0].values.ravel()
#對于一維數組或者列表,unique函數去除其中重復的元素,并按元素由大到小返回一個新的無元素重復的元組或者列表
labels_count = np.unique(labels_flat).shape[0]#One-Hot function
def dense_to_one_hot(labels_dense, num_classes):num_labels = labels_dense.shape[0]index_offset = np.arange(num_labels) * num_classeslabels_one_hot = np.zeros((num_labels, num_classes))labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1return labels_one_hot#one-hot deal labels
labels = dense_to_one_hot(labels_flat, labels_count)
labels = labels.astype(np.uint8)print('標簽({0[0]}, {0[1]})'.format(labels.shape))
print('圖像標簽Example:[{0}] --> {1}'.format(25, labels[25]))#Divide train data to train and validation
VALIDATION_SIZE = 2000
train_images = x_train[VALIDATION_SIZE:]
train_labels = labels[VALIDATION_SIZE:]validation_images = x_train[:VALIDATION_SIZE]
validation_labels = labels[:VALIDATION_SIZE]#set batch size and get the sum total of batch
batch_size = 100
n_batch = len(train_images) // batch_size#define Empty variable (data)x: 784 (labels)y: 10
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])#define function to deal data
def weight_variable(shape):#initial weight --- normal distribution#一個截斷的產生正太分布的函數,就是說產生正太分布的值如果與均值的差值大于兩倍的標準差,那就重新生成initial = tf.truncated_normal(shape, stddev=0.1)return tf.Variable(initial)def bias_variable(shape):# initial bias -- nonzeroinitial = tf.constant(0.1, shape=shape)return tf.Variable(initial)#packaging TensorFlow 2D convolution
def conv2D(x, W):return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
#packaging Tensorflow Pooling layer
def max_pool_2x2(x):return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')#Transform input data to 4D tensor, 2 and 3 is width and height, 4 is color
x_image = tf.reshape(x, [-1, 28, 28, 1])#compute 32 features 3*3 patch
w_conv1 = weight_variable([3, 3, 1, 32])
b_conv1 = bias_variable([32])#28*28 images conv step-size is 1 2*2 max pool
#After pool [28/2, 28/2] = [14, 14] the second pool [14/2, 14/2] = [7, 7]
#conv data
h_conv1 = tf.nn.relu(conv2D(x_image, w_conv1) + b_conv1)
#pool result
h_pool1 = max_pool_2x2(h_conv1)#On the previous basis, generate 64 features
w_conv2 = weight_variable([6, 6, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2D(h_pool1, w_conv2) + b_conv2)#max_pool 2*2 --> [7, 7]
h_pool2 = max_pool_2x2(h_conv2)
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])#Fully connected neural network 1024 Neural
w_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)#Dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)#1024 to 10D output
w_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, w_fc2) + b_fc2#build loss function --> cross entropy
#tf.nn.softmax_cross_entropy_with_logits
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels = y, logits=y_conv))
#optimizing para
train_step_1 = tf.train.AdadeltaOptimizer(learning_rate=0.1).minimize(loss)#compute accuracy
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_conv, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))#Set the filename parameter to save the model
global_step = tf.Variable(0, name='globle_step', trainable=False)
saver =tf.train.Saver()#initial variable
init = tf.global_variables_initializer()#train
with tf.Session() as sess:sess.run(init)# saver.restore(sess, "model.ckpt-12")# iter 20for epoch in range(1, 20):for batch in range(n_batch):# each times get one data patch to trainbatch_x = train_images[(batch) * batch_size:(batch+1) * batch_size]batch_y = train_labels[(batch) * batch_size:(batch+1) * batch_size]# the most important step -->sess.run(train_step_1, feed_dict={x:batch_x, y:batch_y, keep_prob:0.5})# each period compute accuracyaccuracy_n = sess.run(accuracy, feed_dict={x:validation_images, y:validation_labels, keep_prob:1.0})print("The " + str(epoch+1) + "th, accuracy is " + str(accuracy_n))# save train model# global_step.assign(epoch).eval()# saver.save(sess, "model.ckpt", global_step=global_step)
接下來改進方案進一步提高準確率。。。。。使用大神的自歸一化神經網絡
總結
以上是生活随笔為你收集整理的TensorFlow | 使用Tensorflow带你实现MNIST手写字体识别的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。