pycaffe_draw

#!/usr/bin/env python
from pylab import *
import matplotlib.pyplot as plt
import caffe
caffe.set_device(0)
caffe.set_mode_gpu()
solver = caffe.SGDSolver('examples/mnist/lenet_auto_solver.prototxt')
niter = 10000
display_iter = 100
test_iter = 100
test_interval = 500
# train loss
train_loss = zeros(ceil(niter * 1.0 / display_iter))
# test loss
test_loss = zeros(ceil(niter * 1.0 / test_interval))
# test accuracy
test_acc = zeros(ceil(niter * 1.0 / test_interval))
solver.step(1)
_train_loss = 0; _test_loss = 0; _accuracy = 0
_, ax1 = plt.subplots()
ax2 = ax1.twinx()
for it in range(niter):
    solver.step(1)
    _train_loss += solver.net.blobs['loss'].data
    if it % display_iter == 0:
        train_loss[it // display_iter] = _train_loss / display_iter
        _train_loss = 0
    if it % test_interval == 0:
        for test_it in range(test_iter): 
            solver.test_nets[0].forward()         
            _test_loss += solver.test_nets[0].blobs['loss'].data

            _accuracy += solver.test_nets[0].blobs['accuracy'].data

        test_loss[it / test_interval] = _test_loss / test_iter
        test_acc[it / test_interval] = _accuracy / test_iter
        _test_loss = 0
        _accuracy = 0
    if it % display_iter == 0:
        print '\nplot the train loss and test accuracy\n'   
        #_, ax1 = plt.subplots()
        #ax2 = ax1.twinx()
        ax1.plot(display_iter * arange(len(train_loss)), train_loss, 'g')        
        ax1.plot(test_interval * arange(len(test_loss)), test_loss, 'y')
        #plt.plot(test_interval * arange(len(test_acc)), test_acc, 'r')
        ax1.set_xlabel('iteration')
        ax1.set_ylabel('loss')
        ax2.set_ylabel('accuracy')
        #plt.show()
        plt.pause(0.000001)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值