本文整理汇总了Python中mlp.MLP.init_network方法的典型用法代码示例。如果您正苦于以下问题:Python MLP.init_network方法的具体用法?Python MLP.init_network怎么用?Python MLP.init_network使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mlp.MLP
的用法示例。
在下文中一共展示了MLP.init_network方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from mlp import MLP [as 别名]
# 或者: from mlp.MLP import init_network [as 别名]
def setUp(self):
xor = MLP()
xor.add_layer(Layer(2))
xor.add_layer(Layer(2))
xor.add_layer(Layer(1))
xor.init_network()
xor.patterns = [([0, 0], [0]), ([0, 1], [1]), ([1, 0], [1]), ([1, 1], [0])]
self.xor = xor
示例2: test_activate
# 需要导入模块: from mlp import MLP [as 别名]
# 或者: from mlp.MLP import init_network [as 别名]
def test_activate(self):
a = MLP()
a.add_layer(Layer(3))
a.add_layer(Layer(2))
a.init_network()
a.layers[0].values = [1, 1, 1]
a.layers[0].weights[0][0] = 1
a.layers[0].weights[1][0] = -1
a.layers[0].weights[2][0] = 1
a.layers[0].weights[0][1] = -0.1
a.layers[0].weights[1][1] = -0.5
a.layers[0].weights[2][1] = 1
a._activate()
self.assertGreater(a.layers[1].values[0], 0.5)
self.assertLess(a.layers[1].values[1], 0.5)
示例3: test_init_network
# 需要导入模块: from mlp import MLP [as 别名]
# 或者: from mlp.MLP import init_network [as 别名]
def test_init_network(self):
a = MLP()
a.add_layer(Layer(1))
a.add_layer(Layer(2))
a.add_layer(Layer(3))
a.init_network()
self.assertIsNone(a.layers[0].prev)
self.assertIsNotNone(a.layers[0].weights)
self.assertIsNotNone(a.layers[0].next)
self.assertIsNotNone(a.layers[1].prev)
self.assertIsNotNone(a.layers[1].weights)
self.assertIsNotNone(a.layers[1].next)
self.assertIsNotNone(a.layers[2].prev)
self.assertIsNone(a.layers[2].weights)
self.assertIsNone(a.layers[2].next)
示例4: test_xor
# 需要导入模块: from mlp import MLP [as 别名]
# 或者: from mlp.MLP import init_network [as 别名]
def test_xor(self):
xor = MLP()
xor.add_layer(Layer(2))
xor.add_layer(Layer(2))
xor.add_layer(Layer(1))
xor.init_network()
xor_patterns = [
([0, 0], [0]),
([0, 1], [1]),
([1, 0], [1]),
([1, 1], [0]),
]
xor.train(xor_patterns)
for inp, outp in xor_patterns:
self.assertEqual(xor.run(inp), outp)
示例5: main
# 需要导入模块: from mlp import MLP [as 别名]
# 或者: from mlp.MLP import init_network [as 别名]
def main():
xor = MLP()
xor.add_layer(Layer(2))
xor.add_layer(Layer(2))
xor.add_layer(Layer(1))
xor.init_network()
xor.patterns = [
([0, 0], [0]),
([0, 1], [1]),
([1, 0], [1]),
([1, 1], [0]),
]
print xor.train_target(xor.patterns)
for inp, target in xor.patterns:
tolerance = 0.1
computed = xor.run(inp)
error = abs(computed[0] - target[0])
print 'input: %s target: %s, output: %s, error: %.4f' % (inp,
target, computed, error)
示例6: test_init_empty_network
# 需要导入模块: from mlp import MLP [as 别名]
# 或者: from mlp.MLP import init_network [as 别名]
def test_init_empty_network(self):
a = MLP()
a.init_network()
示例7: main
# 需要导入模块: from mlp import MLP [as 别名]
# 或者: from mlp.MLP import init_network [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)