Ejemplo n.º 1
0
def dataset_preprocess():
    #Loading MNIST dataset
    #    global train_images
    global train_labels
    #    global test_images
    global test_labels
    global train_images_preprocessed
    global test_images_preprocessed
    global vector_size

    train_images = load_train_images()
    train_labels = load_train_labels()
    test_images = load_test_images()
    test_labels = load_test_labels()
    ##pattern parameter
    input_threshold = 127  # the binary threshold of input pattern
    sample = preprocess(train_images[0], input_threshold)
    vector_size = size(sample)
    train_images_preprocessed = np.zeros((60000, vector_size), dtype=bool_)
    test_images_preprocessed = np.zeros((10000, vector_size), dtype=bool_)
    for n in range(0, 60000):
        train_images_preprocessed[n] = preprocess(train_images[n],
                                                  input_threshold)
    print 'preprocess train_image done'

    for n in range(0, 10000):
        test_images_preprocessed[n] = preprocess(test_images[n],
                                                 input_threshold)
    print 'preprocess test_image done'
    del train_images, test_images
Ejemplo n.º 2
0
def run():
    train_images = load_train_images()
    train_labels = load_train_labels()
    test_images = load_test_images()
    # test_labels = load_test_labels()

    Batch_num = 600
    Sample_num = 60000
    Epoch_N = 3

    Gw = mat(zeros((784, 10)))

    sequence_in = np.random.permutation(Sample_num)

    for Epoch in range(0, Epoch_N):
        print Epoch
        for n_batch in range(0, Sample_num / Batch_num - 1):
            for n_sample in range(n_batch * Batch_num,
                                  (n_batch + 1) * Batch_num - 1):
                input_sample = mat(train_images[sequence_in[n_sample]])
                input_label = int(mat(train_labels[sequence_in[n_sample]]))
                input_sample = test_images[1].reshape(784, order='c')
                input_sample = (input_sample > 0.5).astype(
                    np.int_)  # binaryzation
                output_neuron = input_sample * Gw
                fired_neuron = int(np.argmax(output_neuron))
                if fired_neuron != input_label:
                    input_sample = input_sample.reshape(-1, 1)
                    Gw[:,
                       input_label] = input_sample * 0.1 + Gw[:, input_label]
Ejemplo n.º 3
0
def dataset_preprocess():
    #Loading MNIST dataset
    #    global train_images
    #    global train_labels
    ##    global test_images
    #    global test_labels
    #    global train_images_preprocessed
    #    global test_images_preprocessed
    #    global vector_size

    train_images = load_train_images()
    train_labels = load_train_labels()
    test_images = load_test_images()
    test_labels = load_test_labels()
    ##pattern parameter
    input_threshold = 127  # the binary threshold of input pattern
    sample = preprocess(train_images[0], input_threshold)
    vector_size = size(sample)
    train_images_preprocessed = np.zeros((60000, vector_size))
    test_images_preprocessed = np.zeros((10000, vector_size))
    for n in range(0, 60):
        train_images_preprocessed[n] = preprocess(train_images[n],
                                                  input_threshold)
    print 'preprocess train_image done'

    for n in range(0, 10):
        test_images_preprocessed[n] = preprocess(test_images[n],
                                                 input_threshold)
    print 'preprocess test_image done'

    del train_images
    del test_images

    filename = 'train_images_preprocessed_8kernels.txt'
    f = open(filename, 'wb')
    pickle.dump(train_images_preprocessed, f)
    f.close()

    filename = 'test_images_preprocessed_8kernels.txt'
    f = open(filename, 'wb')
    pickle.dump(test_images_preprocessed, f)
    f.close()

    filename = 'vector_size.txt'
    f = open(filename, 'wb')
    pickle.dump(vector_size, f)
    f.close()

    print 'save done'

    del train_images_preprocessed
    del test_images_preprocessed
Ejemplo n.º 4
0
def dataset_preprocess():
    #Loading MNIST dataset
    #    global train_images
    #    global train_labels
    ##    global test_images
    #    global test_labels
    #    global train_images_preprocessed
    #    global test_images_preprocessed
    #    global vector_size

    train_images = load_train_images()
    train_labels = load_train_labels()
    test_images = load_test_images()
    test_labels = load_test_labels()
    ##pattern parameter
    input_threshold = 127  # the binary threshold of input pattern
    sample = preprocess(train_images[0], input_threshold)
    vector_size = size(sample)
    train_images_preprocessed = np.zeros((60000, vector_size))
    test_images_preprocessed = np.zeros((10000, vector_size))
    for n in range(0, 60):
        train_images_preprocessed[n] = preprocess(train_images[n],
                                                  input_threshold)
    print 'preprocess train_image done'

    for n in range(0, 10):
        test_images_preprocessed[n] = preprocess(test_images[n],
                                                 input_threshold)
    print 'preprocess test_image done'

    del train_images
    del test_images

    filename = 'processed_8kernels.db'
    f = shelve.open(filename)
    f['train'] = train_images_preprocessed
    f['train_label'] = train_labels
    f['test'] = test_images_preprocessed
    f['test_label'] = test_labels
    f['vector_size'] = vector_size
    f.close()
    print 'image save done'

    del train_images_preprocessed
    del test_images_preprocessed
Ejemplo n.º 5
0
    m = size(image_pool_1) + size(image_pool_2) + size(image_pool_3)
    n = size(image_pool_1) + size(image_pool_2) + size(image_pool_3) + size(
        image_pool_4)
    image_vector[m:n] = image_pool_4.reshape(size(image_pool_4), order='c')

    return image_vector


#%%
def run():
    input_threshold = 127  # the binary threshold of input pattern
    a = preprocess(train_images[0], input_threshold)
    print a


#%%
if __name__ == '__main__':
    #%%
    #Loading MNIST dataset
    global train_images
    global train_labels
    global test_images
    global test_labels

    train_images = load_train_images()
    train_labels = load_train_labels()
    test_images = load_test_images()
    test_labels = load_test_labels()

    #%%
    run()
Ejemplo n.º 6
0
def run():
    train_images = load_train_images()
    train_labels = load_train_labels()
    test_images = load_test_images()
    test_labels = load_test_labels()
    '''Simulation parameter setting'''
    Batch_num = 600
    Sample_num = 60000
    Epoch_N = 4
    Batch_N = Sample_num / Batch_num * Epoch_N

    '''Synapses array initialization '''
    Gw_L1 = np.random.uniform(0.14, 0.16, [784, 100])  # initializing synapses of the first layer
    Gw_L2 = zeros((100, 10))  # initializing synapses of the second layer

    '''Neurons initialization'''
    vth_neuron_l1 = 15*ones(100)
    vth_min = 0.1
    vth_max = 100

    Acc = zeros(Batch_N)

    '''Data pre-processing and Training'''
    for Epoch in range(0, Epoch_N):
        sequence_in = np.random.permutation(Sample_num) # Generate the random sequence of trainning pattern input
        if Epoch == 1:
            print Epoch

        # training in a batch
        for Batch in range(0, Sample_num/Batch_num):
            # trainning
            for n in range(Batch*Batch_num, (Batch+1)*Batch_num):
                input_sample = train_images[sequence_in[n]]/255
                input_label = train_labels[sequence_in[n]]

                '''sort'''
                input_sample = input_sample.reshape(784, order='c')  # reshape the data array from 28*28 to 784*1
                sort_input_temp = np.argsort(-input_sample, 0)
                threshold_input_num = sort_input_temp[100]
                threshold_input = input_sample[threshold_input_num] # find the threshold of the pixel value as active


                '''Binaryzation'''
                # input_sample = (input_sample > 0.5) * 1 + (input_sample <= 0.5) * (-0.1)  # binaryzation
                input_sample = (input_sample > threshold_input) * 1 + (input_sample <= threshold_input) * (-0.1)  # binaryzation

                '''Dot production'''
                neuron_L1_signal = np.dot(input_sample, Gw_L1)  # calculation
                neuron_fire_l1 = (neuron_L1_signal >= vth_neuron_l1) * 1 + (neuron_L1_signal < vth_neuron_l1) * (-0.1)

                # choose fired neurons of layer 1, as the input pattern of the 2nd layer
                for num_of_neuron in range(0, 100):# change the threshold of neurons
                    temp_vth_neuron_l1 = vth_neuron_l1[num_of_neuron]
                    if neuron_fire_l1[num_of_neuron] != 1:
                        temp_vth_neuron_l1 *= 0.9
                        if temp_vth_neuron_l1 > vth_max:
                            temp_vth_neuron_l1 = 1
                    else:
                        temp_vth_neuron_l1 *= 1.1
                        if temp_vth_neuron_l1 < vth_min:
                            temp_vth_neuron_l1 = 0.1
                    vth_neuron_l1[num_of_neuron] = temp_vth_neuron_l1

                '''Weight change'''
                for label_input in range(0, 784):
                    if input_sample[label_input] == 1:
                        for label_output in range(0, 100):
                            if neuron_fire_l1[label_output] == 1:
                                W0 = Gw_L1[label_input, label_output]
                                # weight_change = ap*exp(-bp*W0)
                                weight_change = 0.1
                                W1 = W0 + weight_change
                                if W1 > 1:
                                    W1 = 1
                                Gw_L1[label_input, label_output] = W1

                'forgetting during a sample'
                Gw_L1 *= 0.99999999
        
                '''Layer-2'''
                input_L2 = neuron_fire_l1
                neuron_L2_signal = np.dot(input_L2, Gw_L2)
                neuron_L2_sort = np.argsort(-neuron_L2_signal, 0)
                neuron_num_L2 = neuron_L2_sort[0]
                '''weight change'''
                if neuron_num_L2 != input_label:
                    for label_input_l2 in range(0, 100):
                        if input_L2[label_input_l2] == 1:
                            W0 = Gw_L2[int(label_input_l2), int(input_label)]
                            # weight_change = ap*exp(-bp*W0)
                            weight_change = 0.1
                            W1 = W0 + weight_change
                            if W1 > 1:
                                W1 = 1
                            Gw_L2[int(label_input_l2), int(input_label)] = W1
                Gw_L2 *= 0.99999999
            '''Accuracy Rate Test'''
            correct = snn_2layer_test(test_images, test_labels, Gw_L1, Gw_L2, vth_neuron_l1)
            print correct
            # Acc[Batch + Epoch * (Sample_num / Batch_num)] = snn_2layer_test(test_images, test_labels, Gw_L1_A, Gw_L1_B, Gw_L1_C, Gw_L1_D, Gw_L1_E, Gw_L2)
        Gw_L1 *= 0.999
        Gw_L2 *= 0.999