Esempio n. 1
0
def compute_performance(mnist, weights, biases, layer_types):
    nn = BasicLenetModel()
    nn.create_network(weights, biases, layer_types)
    nn.create_initializer()

    nn.initialize()
    signals = nn.get_fw_signals(mnist.train.images)

    # sparsity and accuracy
    acc = nn.compute_accuracy(mnist.test.images, mnist.test.labels)
    nnz = [np.count_nonzero(np.abs(w) > 1e-6) for w in weights]

    return acc, nnz, signals[-2]
Esempio n. 2
0
def parallel_nettrim(mnist, epsilon_gain, original_weights, original_biases,
                     layer_types):
    nn = BasicLenetModel()
    nn.create_network(original_weights, original_biases, layer_types)
    nn.create_initializer()

    nn.initialize()

    # use all training samples
    num_samples = mnist.train.images.shape[0]
    samples_x, _ = mnist.train.next_batch(num_samples)

    orig_Weights, orig_Biases = nn.get_weights()
    layer_types = nn.get_layer_types()
    signals = nn.get_fw_signals(samples_x)

    num_layers = len(orig_Weights)

    # pruning algorithm on all layers
    unroll_number = 200
    num_iterations = 10
    nt = nt_tf.NetTrimSolver(unroll_number=unroll_number)

    pruned_weights = copy.deepcopy(orig_Weights)
    pruned_biases = copy.deepcopy(orig_Biases)

    for layer in range(num_layers):
        print(' Pruning layer ', layer)
        if layer_types[layer] == 'conv':
            print('Convolutional layer: skipping.')
            continue

        X = np.concatenate(
            [signals[layer].transpose(),
             np.ones((1, num_samples))])
        Y = signals[layer + 1].transpose()

        if layer < num_layers - 1:
            # ReLU layer, use net-trim
            V = np.zeros(Y.shape)
        else:
            # use sparse least-squares (for softmax, ignore the activation function)
            V = None

        norm_Y = np.linalg.norm(Y)
        epsilon = epsilon_gain * norm_Y

        start = time.time()
        W_nt = nt.run(X, Y, V, epsilon, rho=100, num_iterations=num_iterations)
        elapsed = time.time() - start

        print('Elapsed time: {0:5.3f}'.format(elapsed))
        Y_nt = np.matmul(W_nt.transpose(), X)
        if layer < num_layers - 1:
            Y_nt = np.maximum(Y_nt, 0)

        rec_error = np.linalg.norm(Y - Y_nt)
        nz_count = np.count_nonzero(W_nt > 1e-6)
        print('non-zeros= {0}, epsilon= {1:.3f}, rec. error= {2:.3f}'.format(
            nz_count, epsilon, rec_error))
        pruned_weights[layer] = W_nt[:-1, :]
        pruned_biases[layer] = W_nt[-1, :]

    return pruned_weights, pruned_biases
Esempio n. 3
0
    nn.train(x, y, keep_prob)

    if k % 500 == 0:
        acc = nn.compute_accuracy(mnist.validation.images, mnist.validation.labels)
        print('{0:2d}: learning rate={1:5.4f}, accuracy={2:2.3f} '.format(k // 500, nn.learning_rate(), acc))

org_acc = nn.compute_accuracy(mnist.test.images, mnist.test.labels)

#
# Net-Trim:
# change num_samples to a number, say 10000, if you want the Net-Trim retraining with only that many samples
num_samples = mnist.train.images.shape[0]
samples_x, _ = mnist.train.next_batch(num_samples)

orig_Weights, orig_Biases = nn.get_weights()
signals = nn.get_fw_signals(samples_x)
#
num_layers = len(orig_Weights)
#
# pruning algorithm on all layers


nt = nt_tf.NetTrimSolver(unroll_number=unroll_number)


layer_types = nn.get_layer_types()

pruned_weights = copy.deepcopy(orig_Weights)
pruned_biases = copy.deepcopy(orig_Biases)

for layer in range(num_layers):