用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表示不显示行号

 

 

你可能感兴趣的:(机器学习)