importpandasaspdimportmatplotlib.pyplotaspltimportnumpyasnpimporttensorflowastffromtensorflow.contrib.rnn.python.opsimportrnn_cell,rnn# Import MNIST data http://yann.lecun.com/exdb/mnist/fromtensorflow.examples.tutorials.mnistimportinput_data
mnist=input_data.read_data_sets("MNIST_data/",one_hot=True)x_train=mnist.train.images# Check that the dataset contains 55,000 rows and 784 columnsN,D=x_train.shape
tf.reset_default_graph()sess=tf.InteractiveSession()x=tf.placeholder("float",[None,n_steps,n_input])y_true=tf.placeholder("float",[None,n_classes])keep_prob=tf.placeholder(tf.float32,shape=[])learning_rate=tf.placeholder(tf.float32,shape=[])#[............Build the RNN graph model.............]sess.run(tf.global_variables_initializer())# Because I am using my GPU for the training, I avoid allocating the whole# mnist.validation set because of memory error, so I gragment it to# small batches (100)x_validation_bin,y_validation_bin=mnist.validation.next_batch(batch_size)x_validation_bin=binarize(x_validation_bin,threshold=0.1)x_validation_bin=x_validation_bin.reshape((-1,n_steps,n_input))forkinrange(epochs):steps=0foriinrange(training_iters):#Stochastic descentbatch_x,batch_y=mnist.train.next_batch(batch_size)batch_x=binarize(batch_x,threshold=0.1)batch_x=batch_x.reshape((-1,n_steps,n_input))sess.run(train_step,feed_dict={x:batch_x,y_true:batch_y,keep_prob:keep_prob,eta:learning_rate})ifdo_report_err==1:ifsteps%display_step==0:# Calculate batch accuracyacc=sess.run(accuracy,feed_dict={x:batch_x,y_true:batch_y,keep_prob:1.0})# Calculate batch lossloss=sess.run(total_loss,feed_dict={x:batch_x,y_true:batch_y,keep_prob:1.0})print("Iter "+str(i)+", Minibatch Loss= "+"{:.6f}".format(loss)+", Training Accuracy = "+"{:.5f}".format(acc))steps+=1# Validation Accuracy and Costvalidation_accuracy=sess.run(accuracy,feed_dict={x:x_validation_bin,y_true:y_validation_bin,keep_prob:1.0})validation_cost=sess.run(total_loss,feed_dict={x:x_validation_bin,y_true:y_validation_bin,keep_prob:1.0})validation_loss_array.append(final_validation_cost)validation_accuracy_array.append(final_validation_accuracy)saver.save(sess,savefilename)total_epochs=total_epochs+1np.savez(datasavefilename,epochs_saved=total_epochs,learning_rate_saved=learning_rate,keep_prob_saved=best_keep_prob,validation_loss_array_saved=validation_loss_array,validation_accuracy_array_saved=validation_accuracy_array,modelsavefilename=savefilename)