當前位置: 首頁>>代碼示例>>Python>>正文


Python MLP.test_patterns方法代碼示例

本文整理匯總了Python中mlp.MLP.test_patterns方法的典型用法代碼示例。如果您正苦於以下問題:Python MLP.test_patterns方法的具體用法?Python MLP.test_patterns怎麽用?Python MLP.test_patterns使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在mlp.MLP的用法示例。


在下文中一共展示了MLP.test_patterns方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: main

# 需要導入模塊: from mlp import MLP [as 別名]
# 或者: from mlp.MLP import test_patterns [as 別名]
def main():
    imres = MLP()
    num_points = 400
    imres.add_layer(Layer(num_points))
    imres.add_layer(Layer(20))
    imres.add_layer(Layer(10))

    imres.add_bias()
    imres.init_network()

    imres.step = 0.01
    imres.moment = imres.step / 10
    imres.verbose = True
    target_error = 0.01

    imres.patterns = []
    imres._patterns = []
    imres.test_patterns = []
    imres._test_patterns = []

    def norm(inp):
        def fn(x):
            return x/255
        return map(fn, inp)

    sample_dirs = set(os.listdir('font_samples'))
    train = set(random.sample(sample_dirs, len(sample_dirs)-1))
    test = sample_dirs - train
    for j in train:
        for i in range(0, 10):
            gim = Image.open('font_samples/%s/%d.png' % (j, i)).convert('L')
            imdata = norm(list(gim.getdata()))
            outvect = [0]*10
            outvect[i] = 1
            imres.patterns.append((imdata, outvect))
            imres._patterns.append((imdata, i, outvect))

    for j in test:
        for i in range(0, 10):
            gim = Image.open('font_samples/%s/%d.png' % (j, i)).convert('L')
            imdata = norm(list(gim.getdata()))
            outvect = [0]*10
            outvect[i] = 1
            imres.test_patterns.append((imdata, outvect))
            imres._test_patterns.append((imdata, i, outvect))


    print 'Training samples: %d (%s)' %  (len(imres.patterns),
        ' '.join(train))
    print 'Testing samples: %d (%s)' %  (len(imres.test_patterns),
        ' '.join(test))
    print 'Target error: %.4f' % target_error

    final_err, steps = imres.train_target(imres.patterns,
        target_error=target_error)

    print 'Training done in %d steps with final error of %.6f' % (steps,
        final_err)


    print '----- Detailed test output -----'
    total_tests = len(imres._test_patterns)
    total_fails = 0
    for inp, num, target in imres._test_patterns:
        computed = imres.run(inp)
        error = abs(computed[0] - target[0])
        computed = map(lambda x: round(x, 4), computed)
        maxn = computed[0]
        pos = 0
        for i in range(len(computed)):
            if computed[i] > maxn:
                maxn = computed[i]
                pos = i

        if num != pos:
            total_fails += 1
        print 'in: %d, out: %d' % (num, pos)
        print 'target: %s \noutput: %s' % (target, computed)

    print '-----'
    print 'Testing done - %d of %d samples classified incorrectly' % (
        total_fails, total_tests)
開發者ID:sorki,項目名稱:mlp,代碼行數:84,代碼來源:img.py


注:本文中的mlp.MLP.test_patterns方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。