python实现hopfield神经网络的分类

只是一个演示实例,参考:http://packages.python.org/neurolab/ex_newhop.html

python代码:

#!/usr/bin/python
# -*- coding:utf-8 -*-
# cp@chenpeng.info
 
"""
Example of use Hopfield Recurrent network
=========================================
 
Task: Recognition of letters 
 
"""
 
import numpy as np
import neurolab as nl
from neurolab.tool import simhop
 
 
# N E R O
target =  [[1,0,0,0,1,
           1,1,0,0,1,
           1,0,1,0,1,
           1,0,0,1,1,
           1,0,0,0,1],
          [1,1,1,1,1,
           1,0,0,0,0,
           1,1,1,1,1,
           1,0,0,0,0,
           1,1,1,1,1],
          [1,1,1,1,0,
           1,0,0,0,1,
           1,1,1,1,0,
           1,0,0,1,0,
           1,0,0,0,1],
          [0,1,1,1,0,
           1,0,0,0,1,
           1,0,0,0,1,
           1,0,0,0,1,
           0,1,1,1,0]]
 
chars = ['N', 'E', 'R', 'O']
# [ 1. 0. 0. 0. 1. 1. 1. 0. 0. 1. 1. 0. 1. 0. 1. 1. 0. 0. 1. 1. 1. 0. 0. 0. 1.]
target = np.asfarray(target)
# [ 1. -1. -1. -1. 1. 1. 1. -1. -1. 1. 1. -1. 1. -1. 1. 1. -1. -1. 1. 1. 1. -1. -1. -1. 1.]
target[target == 0] = -1
 
 
# Create and train network
# <neurolab.core.Net object at 0x01BB5330>
net = nl.net.newhop(target)
 
# output [ 1. -1. -1. -1. 1. 1. 1. -1. -1. 1. 1. -1. 1. -1. 1. 1. -1. -1. 1. 1. 1. -1. -1. -1. 1.]
# ows array([[ 1., -1., -1., -1., 1., 1., 1., -1., -1., 1., 1., -1., 1., -1., 1., 1., -1., -1., 1., 1., 1., -1., -1., -1., 1.]])
output, ows = simhop(net, target)
print "Test on train samples:"
for i in range(len(target)): 
    print chars[i], (output[i] == target[i]).all(), 'Sim. steps',len(ows[i])
 
print "nTest on defaced N:"
test =   [[1,0,0,0,1,
           1,1,0,0,1,
           1,0,0,0,1,
           1,0,0,1,1,
           1,0,0,0,1],
          [1,1,1,1,1,
           1,0,0,0,0,
           0,0,1,0,1,
           1,0,0,0,0,
           1,1,1,1,1],
          [1,1,1,1,0,
           1,0,0,0,1,
           1,1,1,1,0,
           1,0,0,1,0,
           1,0,0,0,0],
          [0,0,0,0,0,
           1,0,0,0,0,
           1,0,0,0,1,
           1,0,0,0,1,
           0,1,1,1,0]]
# test =np.asfarray([0,0,0,0,0,
                   # 1,1,0,0,1,
                   # 1,1,0,0,1,
                   # 1,0,1,1,1,
                   # 0,0,0,1,1])
test = np.asfarray(test)
# test[test==0] = -1
for i in range(len(test)): 
    test[i][test[i] == 0] = -1
 
# out, ows = simhop(net, [test])
print "Test on train samples:"
# [test[i]] [array()]
# test[i]   []
out0, ows0 = simhop(net, [test[0]])
out1, ows1 = simhop(net, [test[1]])
out2, ows2 = simhop(net, [test[2]])
out3, ows3 = simhop(net, [test[3]])
 
# print (out[0] == target[0]).all(), 'Sim. steps',len(ows[0])
print 'N : ',(out0[0] == target[0]).all(), 'Sim. steps',len(ows0[0])
print 'E : ',(out1[0] == target[1]).all(), 'Sim. steps',len(ows1[0])
print 'R : ',(out2[0] == target[2]).all(), 'Sim. steps',len(ows2[0])
print 'O : ',(out3[0] == target[3]).all(), 'Sim. steps',len(ows3[0])

输出结果:

"D:Python27python.exe" -u "F:ProgramingPythonhopfieldtrain.py"
Process started >>>
Test on train samples:
N True Sim. steps 1
E True Sim. steps 1
R True Sim. steps 1
O True Sim. steps 1
 
Test on defaced N:
Test on train samples:
1 :  True Sim. steps 1
2 :  True Sim. steps 2
3 :  True Sim. steps 1
4 :  True Sim. steps 1
<<< Process finished.
================ READY ================

About 智足者富

http://chenpeng.info

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>