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() |
相关推荐
Python神经网络编程不仅仅是关于编写代码,它还涉及到理解神经网络的基本理论、学习如何构建和训练模型、以及如何将这些模型应用于实际问题。 神经网络的基本组成单元是神经元,它们通过权重相互连接并进行信息的...
PyTorch python 神经网络最新代码PyTorch python 神经网络最新代码PyTorch python 神经网络最新代码PyTorch python 神经网络最新代码PyTorch python 神经网络最新代码PyTorch python 神经网络最新代码PyTorch python...
神经网络是一种模拟人脑神经元处理信息的计算模型,它是由大量节点(或称神经元)通过互相连接构成的网络。这些神经元是高度简化的人脑神经元的数学模型,它们通过权重来模拟生物神经元之间的突触连接强度。神经网络...
python实例_神经网络实现手写字符识别系统python实例_神经网络实现手写字符识别系统python实例_python实例_神经网络实现手写字符识别系统神经网络实现手写字符识别系统python实例_神经网络实现手写字符识别系统...
python实现实 BP神经网络回归预测模型 神 主要介绍了python实现BP神经网络回归预测模型,文中通过示例代码介绍的非常详细,对大家的学习或者工作 具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...
数字图像识别的Python神经网络小程序
收集一些Python 神经网络相关的练习源码,注释丰富,是一些学习Python时候自己完成的一个神经网络相关代码,或许对你有参考作用。可完成的功能有:简单神经网络实现自定义损失函数(利润最大化)、计算一个5层神经...
用python实现神经网络算法用python实现神经网络算法用python实现神经网络算法用python实现神经网络算法用python实现神经网络算法用python实现神经网络算法用python实现神经网络算法用python实现神经网络算法用python...
python 用GA算法优化BP神经网络python 用GA算法优化BP神经网络 python 用GA算法优化BP神经网络python 用GA算法优化BP神经网络 python 用GA算法优化BP神经网络python 用GA算法优化BP神经网络 python 用GA算法优化BP...
在Python中,神经网络的实现通常依赖于各种库,如TensorFlow、Keras、PyTorch等。这些库提供了丰富的功能,可以方便地构建、训练和优化神经网络模型。 在上述代码中,虽然没有直接涉及神经网络的构建,但可以看到它...
Python神经网络学习从鸢尾花数据集开始
【作品名称】:基于python 神经网络实现 latex公式识别 【神经网络课设】 【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【项目...
神经网络 时间序列预测 python语言 code LSTM 深度学习
纯手写卷积神经网络,未使用任何神经网络框架,使用numpy...阅读建议:对于想学习python的同学,可通过此小项目一边研习python代码语法,一边学习卷积神经网络算法,可以很快入门python,并掌握基础的卷积神经网络算法。
《Python神经网络编程高清版.pdf》这本书则专注于使用Python进行神经网络编程。Python是数据科学和机器学习领域广泛使用的语言,其简洁的语法和丰富的库使得构建神经网络变得相对容易。这本书可能详细介绍了Python中...
Python作为目前最流行的编程语言之一,拥有丰富的库支持神经网络的实现,如TensorFlow、Keras、PyTorch等。 本项目专注于使用Python实现BP神经网络,旨在预测未知数据。在CSDN博主的文章中,你可以找到详细的步骤和...
python神经网络算法,代码注释详细,实用易懂
Python实现BP神经网络算法Python实现BP神经网络算法 Python实现BP神经网络算法Python实现BP神经网络算法 Python实现BP神经网络算法Python实现BP神经网络算法 Python实现BP神经网络算法Python实现BP神经网络算法 ...