# input image dimensions img_rows, img_cols = 28, 28 chnls = 1 (x_train, y_train), (x_test, y_test) = dataset.load_data() x_train = x_train.reshape(x_train.shape[0], chnls, img_rows, img_cols) x_test = x_test.reshape(x_test.shape[0], chnls, img_rows, img_cols) y_train = to_categorical(y_train, nb_classes) y_test = to_categorical(y_test, nb_classes) x_train = x_train.astype('float32') x_test = x_test.astype('float32') y_train = y_train.astype('float32') y_test = y_test.astype('float32') x_train /= 255 x_test /= 255 print("Training...") for epoch in range(nb_epoch): print("Epoch: {}".format(epoch)) print("Training loss: {}".format( train_epoch(x_train, y_train, train_func, batch_size))) x_train, y_train = shuffle(x_train, y_train) val_err, val_loss = test_epoch(x_test, y_test, test_func, batch_size) print("Validation accuracy: {}".format((1 - val_err) * 100)) score = test_func(x_test, y_test) params = lasagne.layers.get_all_param_values(model) save_parameters(params, '{:2.2f}.h5'.format((1 - score[1]) * 100))
train_set.y = np.float32(np.eye(10)[train_set.y]) valid_set.y = np.float32(np.eye(10)[valid_set.y]) test_set.y = np.float32(np.eye(10)[test_set.y]) # for hinge loss train_set.y = 2 * train_set.y - 1. valid_set.y = 2 * valid_set.y - 1. test_set.y = 2 * test_set.y - 1. print('Building the CNN...') cnn, train_fn, val_fn = build_network() print('Training...') binary_net.train(train_fn, val_fn, cnn, batch_size, LR_start, LR_decay, num_epochs, train_set.X, train_set.y, valid_set.X, valid_set.y, test_set.X, test_set.y, shuffle_parts=shuffle_parts) W = lasagne.layers.get_all_layers(cnn)[1].W.get_value() import matplotlib.pyplot as plt plt.hist(W.flatten()) plt.title("Weight distribution of first hidden convolution layer") # Dump the network weights to a file filepath = 'binarynet.h5' params = lasagne.layers.get_all_param_values(cnn) save_parameters(params, filepath)
train_set.y = np.float32(np.eye(10)[train_set.y]) valid_set.y = np.float32(np.eye(10)[valid_set.y]) test_set.y = np.float32(np.eye(10)[test_set.y]) # for hinge loss train_set.y = 2 * train_set.y - 1. valid_set.y = 2 * valid_set.y - 1. test_set.y = 2 * test_set.y - 1. print('Building the CNN...') model, train_func, val_func = build_network() print('Training...') binary_connect.train(train_func, val_func, batch_size, LR_start, LR_decay, num_epochs, train_set.X, train_set.y, valid_set.X, valid_set.y, test_set.X, test_set.y) W = lasagne.layers.get_all_layers(model)[1].W.get_value() plt.hist(W.flatten()) plt.title("Weight distribution of first hidden convolution layer") # Dump the network weights to a file filepath = 'binaryconnect' parameters = lasagne.layers.get_all_param_values(model) save_parameters(parameters, filepath + '.h5') params_b = lasagne.layers.get_all_params(model, binary=True) save_parameters(parameters, filepath + '_binary.h5')