mlp = lasagne.layers.BatchNormLayer(mlp) test_output = lasagne.layers.get_output(mlp, deterministic=True) test_err = T.mean(T.neq(T.argmax(test_output, axis=1), target),dtype=theano.config.floatX) # Compile a second function computing the validation loss and accuracy: val_fn = theano.function([input, target], test_err) print("Loading the trained parameters and binarizing the weights...") # Load parameters with np.load('mnist_parameters.npz') as f: param_values = [f['arr_%d' % i] for i in range(len(f.files))] lasagne.layers.set_all_param_values(mlp, param_values) # Binarize the weights params = lasagne.layers.get_all_params(mlp) for param in params: # print param.name if param.name == "W": param.set_value(binary_ops.SignNumpy(param.get_value())) print('Running...') start_time = time.time() test_error = val_fn(test_set.X,test_set.y)*100. print "test_error = " + str(test_error) + "%" run_time = time.time() - start_time print("run_time = "+str(run_time)+"s")
print("Loading the trained parameters and binarizing the weights...") # Load parameters with np.load('mnist_parameters.npz') as f: param_values = [f['arr_%d' % i] for i in range(len(f.files))] lasagne.layers.set_all_param_values(mlp, param_values) # Binarize the weights weights_count = 0 params = lasagne.layers.get_all_params(mlp) for param in params: print(param.name) if param.name == "W": orig_weights = param.get_value() param.set_value(binary_ops.SignNumpy(orig_weights)) weights_count = weights_count + len(orig_weights.reshape(-1)) print(param.get_value()) # Print some stats total_params_count = lasagne.layers.count_params(mlp) weights_needed_bytes_count = weights_count / 8 non_weights_needed_bytes_count = total_params_count * 4 - weights_count / 8 print( 'The model has a total of %d parameters of which %d (the weights) are binary.' % (total_params_count, weights_count)) print('Space requirements:') print(' - %d bytes for the weights' % weights_needed_bytes_count) print(' - %d bytes for the other parameters (assumed 32bit floats each)' % non_weights_needed_bytes_count) print(' = %d bytes' %