使用caffe提供的python介面訓練mnist例子

來源:互聯網
上載者:User

標籤:min   例子   figure   loss   lib   cccccc   app   並且   cond   

1 首先肯定是安裝caffe,並且編譯python介面,如果是在windows上,最好把編譯出來的python檔案夾的caffe檔案夾拷貝到anaconda檔案夾下面去,這樣就有代碼自動提示功能,如下:

本文中使用的ide為anaconda安裝中內建的spyder,,將根目錄設定為caffe的根目錄。

import caffecaffe.set_mode_cpu()solver = caffe.SGDSolver(‘examples/mnist/lenet_solver.prototxt‘)solver.solve()

以上為一次全部迭代,如果想自己控制,可使用如下代碼:

import caffecaffe.set_mode_cpu()solver = caffe.SGDSolver(‘examples/mnist/lenet_solver.prototxt‘)#solver.solve()iter = solver.iterwhile iter<10000:    solver.step(1)    iter = solver.iter    input_data = solver.net.blobs[‘data‘].data      loss = solver.net.blobs[‘loss‘].data    accuracy = solver.test_nets[0].blobs[‘accuracy‘].data    print ‘iter:‘, iter, ‘loss:‘, loss,‘accuracy:‘,accuracy
import caffeimport matplotlib.pyplot as plt     import numpy as npdef vis_square(data):    """Take an array of shape (n, height, width) or (n, height, width, 3)       and visualize each (height, width) thing in a grid of size approx. sqrt(n) by sqrt(n)"""        # normalize data for display    data = (data - data.min()) / (data.max() - data.min())        # force the number of filters to be square    n = int(np.ceil(np.sqrt(data.shape[0])))    padding = (((0, n ** 2 - data.shape[0]),               (0, 1), (0, 1))                 # add some space between filters               + ((0, 0),) * (data.ndim - 3))  # don‘t pad the last dimension (if there is one)    data = np.pad(data, padding, mode=‘constant‘, constant_values=1)  # pad with ones (white)        # tile the filters into an image    data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))    data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])    if data.shape[2] == 1:        data = data[:,:,0]    plt.imshow(data); plt.axis(‘off‘)if __name__ == ‘__main__‘:    caffe.set_mode_cpu()    solver = caffe.SGDSolver(‘examples/mnist/lenet_solver.prototxt‘)    solver.step(1)    input_data = solver.net.blobs[‘data‘].data      plt.figure(0)    vis_square(input_data.transpose(0, 2, 3, 1))      filters = solver.net.params[‘conv1‘][0].data    plt.figure(1)    vis_square(filters.transpose(0, 2, 3, 1))

    特徵圖:
   
    權值圖

使用caffe提供的python介面訓練mnist例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.