#将整数数组转换为one hot vector的数组 defto_onehotv(a): t = np.zeros((a.size,10)) for i,n inenumerate(a): t[i][n] = 1.0 return t
#分批次取出并转化mnist.train_images()与mnist.train_labels()中的数据 index = 0 defnext_batch(n): global index xs = np.float32(mnist.train_images()[index:index+n]).reshape((n,28**2))/255 ys = to_onehotv(mnist.train_labels()[index:index+n]) index += n return xs,ys
with tf.Session() as sess: sess.run(init) for i inrange(500): batch_xs,batch_ys = next_batch(50) sess.run(train_step,feed_dict={x:batch_xs,y_label:batch_ys}) if i%50 ==0: print(sess.run(accuracy,feed_dict={x:test_xs,y_label:test_ys})) print('training finished') print('final accuracy is',sess.run(accuracy,feed_dict={x:test_xs,y_label:test_ys}))