Caffe Python Interface Learning (7): Drawing loss and accuracy curves

Source: Internet
Author: User
Tags jupyter notebook

The main reason for using the Python interface to run the Caffe program is that Python is very easy to visualize. So it is not recommended to run Python programs under the command line. If you want to run under the command line, you might as well just use C + + instead.

It is recommended to use tools such as Jupyter Notebook,spyder to run Python code so that it is perfectly combined with its visualization.

Because I am using Anaconda to install a series of Python third-party libraries, so I use the Spyder, a MATLAB interface similar to an editor, in the run process, you can view the values of the variables, easy to understand, such as:

As long as the installation of Anaconda, the operation is also very convenient, directly in the terminal to enter the Spyder command.

In the course of Caffe training, if we want to know the loss value and accuracy value of a certain stage and draw it with the graph, we can use the Python interface.

#-*-coding:utf-8-*-"""Created on Tue Jul 16:22:22 2016@author:root"""ImportMatplotlib.pyplot as PltImportCaffe Caffe.set_device (0) Caffe.set_mode_gpu ()#using Sgdsolver, a random gradient descent algorithmSolver = Caffe. Sgdsolver ('/home/xxx/mnist/solver.prototxt')    #equivalent to the Max_iter in the Solver file, which is the maximum number of solutionsNiter = 9380#Collect data every 100 timesDisplay= 100#100 calculation per test, 10000/100Test_iter = 100#one test per 500 training sessions (100 solutions), 60000/64Test_interval =938#InitializeTrain_loss = Zeros (ceil (Niter * 1.0/display)) Test_loss= Zeros (Ceil (Niter * 1.0/test_interval)) TEST_ACC= Zeros (Ceil (Niter * 1.0/test_interval)) #Iteration 0, not countedSolver.step (1)    #Auxiliary Variables_train_loss = 0; _test_loss = 0; _accuracy =0#to solve the calculation forItinchRange (niter):#to perform a single calculationSolver.step (1)      #every iteration, training batch_size picture_train_loss + = solver.net.blobs['SoftmaxWithLoss1'].dataifIt% display = =0:#calculate average train lossTrain_loss[it//Display] = _train_loss/Display _train_loss=0ifIt% Test_interval = =0: forTest_itinchRange (test_iter):#Conduct a testSolver.test_nets[0].forward ()#Calculate Test Loss_test_loss + = solver.test_nets[0].blobs['SoftmaxWithLoss1'].data#Calculate test Accuracy_accuracy + = solver.test_nets[0].blobs['Accuracy1'].data#calculate average Test lossTest_loss[it/test_interval] = _test_loss/Test_iter#calculate average test accuracyTest_acc[it/test_interval] = _accuracy/Test_iter _test_loss=0 _accuracy=0#Draw train loss, test loss, and accuracy curvesPrint '\nplot the train loss and test accuracy\n'_, Ax1=plt.subplots () ax2=Ax1.twinx ()#train loss, greenAx1.plot (Display * Arange (len (Train_loss)), Train_loss,'g')  #Test Loss YellowAx1.plot (Test_interval * Arange (len (Test_loss)), Test_loss,'y')  #test accuracy, redAx2.plot (Test_interval * Arange (len (TEST_ACC)), TEST_ACC,'R') Ax1.set_xlabel ('Iteration') Ax1.set_ylabel ('Loss') Ax2.set_ylabel ('accuracy') plt.show ()

The resulting chart has been shown in the.

Caffe Python Interface Learning (7): Drawing loss and accuracy curves

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.