Exemplo n.º 1
0
def classification(neuro_obj = None, epoch = 100000, num_class = 10):

    print 'initialize Neural Network.'
    if neuro_obj: nn_obj = neuro_obj
    else        : nn_obj = mln.Mln().make_neuralnet([28*28, 1000, num_class], ['sigmoid', 'softmax'], 0.01) # mnist classification
#    else        : nn_obj = mln.Mln().make_neuralnet([28*28, 1000, num_class], ['sigmoid', 'softmax'], 0.15) # mnist classification

    print "dump obj..."
    dp.obj_dump(nn_obj, './default-classification.pkl')

    print 'read training data and label.'
    training_data = dp.obj_load_gzip('../../mnist/mnist-training_all.pkl.gz')

    print 'data      size : ', len(training_data)
    print 'label     size : ', len(training_data)

    data_num = len(training_data)

    print '--start--'
    print '@@ Learn Character Recognition @@'
    for j in range(0, epoch):
        prg.show_progressxxx(j+1, epoch)
    
        i = np.random.randint(data_num)
        nn_obj.learn(training_data[i][0], training_data[i][1])

    prg.end_progress()

    print "dump obj..."
    dp.obj_dump(nn_obj, './learn-classification.pkl')
    
    return nn_obj
Exemplo n.º 2
0
def classification(neuro_obj=None, epoch=100000, minibatch_size=1):
    num_class = 10

    print "initialize Neural Network."
    if neuro_obj:
        nn_obj = neuro_obj
    else:
        nn_obj = mln.Mln().make_neuralnet(
            [28 * 28, 1000, num_class], ["sigmoid", "softmax"], 0.15
        )  # mnist classification
    #    else        : nn_obj = mln.Mln().make_neuralnet([28*28, 1000, num_class], ['sigmoid', 'softmax'], 0.01) # mnist classification

    # use weight decay.
    #    nn_obj.use_weight_decay(0.01)   # unlearnable
    #    nn_obj.use_weight_decay(0.001)  # learnable
    nn_obj.use_weight_decay(0.0001)  # learnable
    #    nn_obj.unuse_weight_decay()
    # use momentum.
    #    nn_obj.use_momentum(0.1)
    nn_obj.use_momentum(0.5)
    #    nn_obj.use_momentum(0.9)
    #    nn_obj.unuse_momentum()

    print "dump obj..."
    dp.obj_dump(nn_obj, "./default-classification-batch.pkl")

    print "read training data and label."
    training_data = dp.obj_load_gzip("../../mnist/mnist-training_all.pkl.gz")

    print "data      size : ", len(training_data)
    print "label     size : ", len(training_data)
    print "minibatch size : ", minibatch_size

    data_num = len(training_data)

    print "--start--"
    print "@@ Learn Character Recognition @@"
    mb_size = minibatch_size
    for i in range(0, epoch):
        prg.show_progressxxx(i + 1, epoch)

        xxx = random.sample(training_data, data_num)
        x = []
        d = []
        for i in range(0, mb_size):
            x.append(xxx[i][0])
            d.append(xxx[i][1])

        nn_obj.batch_learn(x[0:mb_size], d[0:mb_size], mb_size)
    prg.end_progress()

    print "dump obj..."
    dp.obj_dump(nn_obj, "./learn-classification-batch.pkl")

    return nn_obj
Exemplo n.º 3
0
def test_classification():
    num_class = 10

    print 'initialize Neural Network.'
    nn_obj = dp.obj_load('./learn-classification-batch.pkl')

    print 'read test data.'
    test_data  = dp.obj_load_gzip('../../mnist/mnist-test_data.pkl.gz')

    print 'read test label.'
    test_label = dp.obj_load_gzip('../../mnist/mnist-test_label.pkl.gz')
 
    print 'data  size : ', len(test_data)
    print 'label size : ', len(test_label)

    data_num = len(test_data)
    prediction_error = []
    prediction_recog = []

    print '--start--'
    print '@@ Test Character Recognition @@'
    for j in range(0, data_num):
        prg.show_progressxxx(j+1, data_num)
    
        num_recog, list_recog = nn_obj.test(test_data[j], test_label[j])

        if test_label[j][num_recog] == 0:
            prediction_error.append((test_label[j], num_recog))

    prg.end_progress()

    count = len(prediction_error)

    for item in prediction_error:
        print "Truth:", np.argmax(item[0]), ", --> But predict:", item[1]

    print ''
    print 'error : ', count, '/', data_num, '(', count * 1.0 / data_num * 100.0, '%)'

    return
Exemplo n.º 4
0
def classification(neuro_obj=None, epoch=100000, num_class=10):

    print "initialize Neural Network."
    if neuro_obj:
        nn_obj = neuro_obj
    else:
        nn_obj = mln.Mln().make_neuralnet(
            [28 * 28, 1000, num_class], ["sigmoid", "softmax"], 0.01
        )  # mnist classification
    #    else        : nn_obj = mln.Mln().make_neuralnet([28*28, 1000, num_class], ['sigmoid', 'softmax'], 0.15) # mnist classification

    # use weight decay.
    #    nn_obj.use_weight_decay(0.01)   #
    #    nn_obj.use_weight_decay(0.001)  #
    #    nn_obj.use_weight_decay(0.0001) # 20.02% error, with momentum 0.5.
    nn_obj.use_weight_decay(0.00001)  #  8.45% error, with momentum 0.5.
    #    nn_obj.unuse_weight_decay()
    # use momentum.
    #    nn_obj.use_momentum(0.1)
    nn_obj.use_momentum(0.5)
    #    nn_obj.use_momentum(0.9)
    #    nn_obj.unuse_momentum()

    print "dump obj..."
    dp.obj_dump(nn_obj, "./default-classification.pkl")

    print "read training data and label."
    training_data = dp.obj_load_gzip("../../mnist/mnist-training_all.pkl.gz")

    print "data      size : ", len(training_data)
    print "label     size : ", len(training_data)

    data_num = len(training_data)

    print "--start--"
    print "@@ Learn Character Recognition @@"
    for j in range(0, epoch):
        prg.show_progressxxx(j + 1, epoch)

        i = np.random.randint(data_num)
        nn_obj.learn(training_data[i][0], training_data[i][1])

    prg.end_progress()

    print "dump obj..."
    dp.obj_dump(nn_obj, "./learn-classification.pkl")

    return nn_obj
Exemplo n.º 5
0
def classification(neuro_obj = None, epoch = 100000, minibatch_size = 1):
    num_class = 10

    print 'initialize Neural Network.'
    if neuro_obj: nn_obj = neuro_obj
    else        : nn_obj = mln.Mln().make_neuralnet([28*28, 1000, num_class], ['sigmoid', 'softmax'], 0.15) # mnist classification
#    else        : nn_obj = mln.Mln().make_neuralnet([28*28, 1000, num_class], ['sigmoid', 'softmax'], 0.01) # mnist classification

    print "dump obj..."
    dp.obj_dump(nn_obj, './default-classification-batch.pkl')

    print 'read training data and label.'
    training_data = dp.obj_load_gzip('../../mnist/mnist-training_all.pkl.gz')

    print 'data      size : ', len(training_data)
    print 'label     size : ', len(training_data)
    print "minibatch size : ", minibatch_size

    data_num = len(training_data)

    print '--start--'
    print '@@ Learn Character Recognition @@'
    mb_size = minibatch_size
    for i in range(0, epoch):
        prg.show_progressxxx(i+1, epoch)

        xxx = random.sample(training_data, data_num)
        x = []
        d = []
        for i in range(0, mb_size):
            x.append(xxx[i][0])
            d.append(xxx[i][1])

        nn_obj.batch_learn(x[0:mb_size], d[0:mb_size], mb_size)
    prg.end_progress()

    print "dump obj..."
    dp.obj_dump(nn_obj, './learn-classification-batch.pkl')
    
    return nn_obj