用tensorflow實現minist手寫數字識別

代碼:

import pandas as pd
import tensorflow as tf
import numpy as np
#加載數據
train=pd.read_csv('../Desktop/DataSets/MINIST/train.csv')
test=pd.read_csv('../Desktop/DataSets/MINIST/test.csv')
X_train=train.iloc[:,1:].values
y_train=train.iloc[:,0].values
X_test=test.iloc[:,:].values
print(len(X_test))
print(len(y_train))
#數據預處理
X_train=X_train.astype(np.float)
X_train=np.multiply(X_train,1.0/225)
X_test=X_test.astype(np.float)
X_test=np.multiply(X_test,1.0/225)
image_size=X_train.shape[1]
#像正無窮大取整
image_width=image_height=np.ceil(np.sqrt(image_size)).astype(np.uint8)

#對結果進行處理
#y_count就是類別數 np.unique對列表去重並且從大到小排序
y_count=np.unique(y_train).shape[0]
print(y_count)
#進行one-hot編碼
def one_hot(labels,num_classes):
    nums_labels=labels.shape[0]#知道標籤數
    index=np.arange(nums_labels)*num_classes
    labels_one_hot=np.zeros((nums_labels,num_classes))#初始化標籤數*類別數的0矩陣
    #ravel()將多維數組降爲1維
    labels_one_hot.flat[index+labels.ravel()]=1
    return labels_one_hot
y_train=one_hot(y_train,y_count)
y_train=y_train.astype(np.uint8)
#對訓練集進行分批
batch_size=64
n_batch=int(len(X_train)/batch_size)
#創建一個簡單的神經網絡對圖片進行識別
x=tf.placeholder('float',shape=[None,image_size])
y=tf.placeholder('float',shape=[None,y_count])
weights=tf.Variable(tf.zeros([784,10]))
baises=tf.Variable(tf.zeros([10]))
result=tf.matmul(x,weights)+baises
predictions=tf.nn.softmax(result)

#創建損失函數
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=predictions))
train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss)
#初始化變量
init=tf.global_variables_initializer()
#計算預測值
with tf.Session() as sess:
    sess.run(init)
    #循環50輪
    for epoch in range(50):
        for batch in range(n_batch-1):
            batch_x=X_train[batch*batch_size:(batch+1)*batch_size]
            batch_y=y_train[batch*batch_size:(batch+1)*batch_size]
            #進行訓練
            sess.run(train_step,feed_dict={x:batch_x,y:batch_y})
        batch_x=X_train[n_batch*batch_size:]
        batch_y=y_train[n_batch*batch_size:]
        sess.run(train_step,feed_dict={x:batch_x,y:batch_y})
    myPrediction=sess.run(predictions,feed_dict={x:X_test})
y_test=np.argmax(myPrediction,axis=1)
#pd.DataFrame(y_test).to_csv('minist_cnn.csv')
submission=pd.DataFrame(({'ImageID':range(1,28001),'Label':np.int32(y_test)}))
submission.to_csv('cnn_minist.csv',index=False)#index=False表示不顯示行號

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章