def test(): import mlbase.network as N import h5py network = N.Network() network.debug = True network.setInput(N.RawInput((1, 28, 28))) network.append(N.Conv2d(feature_map_multiplier=32)) network.append(ResLayer()) network.append(ResLayer()) network.append(ResLayer()) network.append(ResLayer(increase_dim=True)) network.append(ResLayer()) network.append(ResLayer()) network.append(ResLayer()) network.append(ResLayer(increase_dim=True)) network.append(ResLayer()) network.append(ResLayer()) network.append(ResLayer()) network.append(N.GlobalPooling()) network.append(N.FullConn(input_feature=128, output_feature=10)) network.append(N.SoftMax()) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:, :].reshape(-1, 1, 28, 28) teX = f['x_test'][:, :].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean( np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1)))
def test_binaryweight(): network = N.Network() network.debug = True network.setInput(N.RawInput((1, 28,28))) network.append(N.Conv2d(feature_map_multiplier=32)) network.append(N.Relu()) network.append(N.Pooling((2,2))) network.append(Binarize()) network.append(N.Conv2d(feature_map_multiplier=2)) network.append(N.Relu()) network.append(N.Pooling((2,2))) network.append(Binarize()) network.append(BinaryConv2d(feature_map_multiplier=2)) network.append(N.Relu()) network.append(N.Pooling((2,2))) network.append(N.Flatten()) network.append(N.FullConn(input_feature=1152, output_feature=1152*2)) network.append(N.Relu()) network.append(N.FullConn(input_feature=1152*2, output_feature=10)) network.append(N.SoftMax()) network.build() f = h5py.File('/hdd/home/yueguan/workspace/data/mnist/mnist.hdf5', 'r') trX = f['x_train'][:,:].reshape(-1, 1, 28, 28) teX = f['x_test'][:,:].reshape(-1, 1, 28, 28) trY = np.zeros((f['t_train'].shape[0], 10)) trY[np.arange(len(f['t_train'])), f['t_train']] = 1 teY = np.zeros((f['t_test'].shape[0], 10)) teY[np.arange(len(f['t_test'])), f['t_test']] = 1 for i in range(5000): print(i) network.train(trX, trY) print(1 - np.mean(np.argmax(teY, axis=1) == np.argmax(network.predict(teX), axis=1)))