当前位置: 首页>>代码示例>>Python>>正文


Python MLP.add_bias方法代码示例

本文整理汇总了Python中mlp.MLP.add_bias方法的典型用法代码示例。如果您正苦于以下问题:Python MLP.add_bias方法的具体用法?Python MLP.add_bias怎么用?Python MLP.add_bias使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mlp.MLP的用法示例。


在下文中一共展示了MLP.add_bias方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from mlp import MLP [as 别名]
# 或者: from mlp.MLP import add_bias [as 别名]
def main():
    xor = MLP()
    cnf = lambda: 0
    xor.add_layer(Layer(2))
    xor.add_layer(Layer(2, cnf))
    xor.add_layer(Layer(1))

    xor.add_bias()
    xor.init_network()

    xor.patterns = [
        ([0, 0], [0]),
        ([0, 1], [1]),
        ([1, 0], [1]),
        ([1, 1], [0]),
    ]

    print xor.train(xor.patterns)
    for inp, target in xor.patterns:
        tolerance = 0.1
        computed = xor.forward(inp)
        error = abs(computed[0] - target[0])
        print 'input: %s target: %s, output: %s, error: %.4f' % (inp,
            target, computed, error)
开发者ID:RiweiChen,项目名称:ML_Code_Practice,代码行数:26,代码来源:xor_bias.py

示例2: main

# 需要导入模块: from mlp import MLP [as 别名]
# 或者: from mlp.MLP import add_bias [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.add_bias方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。