Python 神经网络调教程序

01import random
02import math
03 
04from pyneurgen.neuralnet import NeuralNet
05from pyneurgen.nodes import BiasNode, Connection
06 
07pop_len = 360
08factor = 1.0 / float(pop_len)
09population = [
10    (i, math.sin(float(i) * factor )) for i in range(pop_len)
11]
12 
13all_inputs = []
14all_targets = []
15 
16def population_gen(population):
17    pop_sort = [item for item in population]
18    random.shuffle(pop_sort)
19    for item in pop_sort:
20        yield item
21 
22#   Build the inputs
23for position, target in population_gen(population):
24    pos = float(position)
25    all_inputs.append([random.random(), pos * factor])
26    all_targets.append([target])
27 
28net = NeuralNet()
29net.init_layers(2, [10], 1)
30net.randomize_network()
31net.learnrate = .20
32 
33net.randomize_network()
34net.set_all_inputs(all_inputs)
35net.set_all_targets(all_targets)
36length = len(all_inputs)
37 
38learn_end_point = int(length * .8)
39net.set_learn_range(0, learn_end_point)
40net.set_test_range(learn_end_point + 1, length - 1)
41net.layers[1].set_activation_type('tanh')
42net.learn(epochs=125, show_epoch_results=True,random_testing=False)
43mse = net.test()
44 
45import matplotlib
46from pylab import plot, legend, subplot, grid
47from pylab import xlabel, ylabel, show, title
48 
49test_positions = [item[0][1] * 1000.0 for item in net.get_test_data()]
50 
51all_targets1 = [item[0][0] for item in net.test_actuals_targets]
52allactuals = [item[1][0] for item in net.test_actuals_targets]
53 
54#   This is quick and dirty, but it will show the results
55subplot(3, 1, 1)
56plot([i[1] for i in population])
57title("Population")
58grid(True)
59 
60subplot(3, 1, 2)
61plot(test_positions, all_targets1, 'bo', label='targets')
62plot(test_positions, allactuals, 'ro', label='actuals')
63grid(True)
64legend(loc='lower left', numpoints=1)
65title("Test Target Points vs Actual Points")
66 
67subplot(3, 1, 3)
68plot(range(1, len(net.accum_mse) + 1, 1), net.accum_mse)
69xlabel('epochs')
70ylabel('mean squared error')
71grid(True)
72title("Mean Squared Error by Epoch")
73 
74show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值