標籤: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例子