01 |
import random |
02 |
import math |
03 |
04 |
from pyneurgen.neuralnet import NeuralNet |
05 |
from pyneurgen.nodes import BiasNode, Connection |
06 |
07 |
pop_len = 360 |
08 |
factor = 1.0 / float (pop_len) |
09 |
population = [ |
10 |
(i, math.sin( float (i) * factor )) for i in range (pop_len) |
11 |
] |
12 |
13 |
all_inputs = [] |
14 |
all_targets = [] |
15 |
16 |
def 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 |
23 |
for position, target in population_gen(population): |
24 |
pos = float (position) |
25 |
all_inputs.append([random.random(), pos * factor]) |
26 |
all_targets.append([target]) |
27 |
28 |
net = NeuralNet() |
29 |
net.init_layers( 2 , [ 10 ], 1 ) |
30 |
net.randomize_network() |
31 |
net.learnrate = . 20 |
32 |
33 |
net.randomize_network() |
34 |
net.set_all_inputs(all_inputs) |
35 |
net.set_all_targets(all_targets) |
36 |
length = len (all_inputs) |
37 |
38 |
learn_end_point = int (length * . 8 ) |
39 |
net.set_learn_range( 0 , learn_end_point) |
40 |
net.set_test_range(learn_end_point + 1 , length - 1 ) |
41 |
net.layers[ 1 ].set_activation_type( 'tanh' ) |
42 |
net.learn(epochs = 125 , show_epoch_results = True ,random_testing = False ) |
43 |
mse = net.test() |
44 |
45 |
import matplotlib |
46 |
from pylab import plot, legend, subplot, grid |
47 |
from pylab import xlabel, ylabel, show, title |
48 |
49 |
test_positions = [item[ 0 ][ 1 ] * 1000.0 for item in net.get_test_data()] |
50 |
51 |
all_targets1 = [item[ 0 ][ 0 ] for item in net.test_actuals_targets] |
52 |
allactuals = [item[ 1 ][ 0 ] for item in net.test_actuals_targets] |
53 |
54 |
# This is quick and dirty, but it will show the results |
55 |
subplot( 3 , 1 , 1 ) |
56 |
plot([i[ 1 ] for i in population]) |
57 |
title( "Population" ) |
58 |
grid( True ) |
59 |
60 |
subplot( 3 , 1 , 2 ) |
61 |
plot(test_positions, all_targets1, 'bo' , label = 'targets' ) |
62 |
plot(test_positions, allactuals, 'ro' , label = 'actuals' ) |
63 |
grid( True ) |
64 |
legend(loc = 'lower left' , numpoints = 1 ) |
65 |
title( "Test Target Points vs Actual Points" ) |
66 |
67 |
subplot( 3 , 1 , 3 ) |
68 |
plot( range ( 1 , len (net.accum_mse) + 1 , 1 ), net.accum_mse) |
69 |
xlabel( 'epochs' ) |
70 |
ylabel( 'mean squared error' ) |
71 |
grid( True ) |
72 |
title( "Mean Squared Error by Epoch" ) |
73 |
74 |
show() |
相关推荐
PyTorch python 神经网络最新代码PyTorch python 神经网络最新代码PyTorch python 神经网络最新代码PyTorch python 神经网络最新代码PyTorch python 神经网络最新代码PyTorch python 神经网络最新代码PyTorch python...
Python神经网络编程,想学习神经网络可以参考学习
python神经网络编程,是入门深度学习必备书,附上该书的源代码
一个最简单的神经网络的PY程序实现。有测试数据集和PY实现。
python实现实 BP神经网络回归预测模型 神 主要介绍了python实现BP神经网络回归预测模型,文中通过示例代码介绍的非常详细,对大家的学习或者工作 具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
介绍神经网络的相关知识。读后你将对神经网络有个大概了解,它是如何工作的?如何创建神经网络?
对Python神经网络编程中的demo进行练习,简单的三层神经网络,是Python神经网络编程学习入门的很好的体验。
数字图像识别的Python神经网络小程序
收集一些Python 神经网络相关的练习源码,注释丰富,是一些学习Python时候自己完成的一个神经网络相关代码,或许对你有参考作用。可完成的功能有:简单神经网络实现自定义损失函数(利润最大化)、计算一个5层神经...
python卷积神经网络案例Alexnet的有关代码,有利于初学者借鉴并学习
一个神经网络就是一个模仿人类大脑激发学习过程的框架。神经网络被用于从数据中识别隐藏的模式。正如所有的学习算法,神经网络处理的是数字。因此,如果想要实现处理现实世界中任何包含图像、文字、传感器等的任务,...
Python神经网络编程笔记,主要实现了使用Python制作神经网络。
Python神经网络学习从鸢尾花数据集开始
纯手写卷积神经网络,未使用任何神经网络框架,使用numpy...阅读建议:对于想学习python的同学,可通过此小项目一边研习python代码语法,一边学习卷积神经网络算法,可以很快入门python,并掌握基础的卷积神经网络算法。
BP神经网络实例及代码分析(python+tensorflow)
python神经网络算法,代码注释详细,实用易懂
神经网络风格迁移Python程序
使用python自己实现神经网络操纵小车,使用TensorFlow框架实现神经网络操纵小车,神经网络入门.