栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Hopfield网络的设计与实现

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Hopfield网络的设计与实现

Hopfield网络的设计与实现

反馈神经网络,Hopfield网络,DHNN,Python。
通过Python编程,使用Hebb学习方法计算DHNN的权重参数;
通过Python编程,使用同步方法迭代DHNN,获取最终网络稳定状态

实验步骤
1,导入必要的函数库
2. 设计一个DHNN,激活函数使用符号函数
3. 将两幅灰度图片(1.png和2.png)转换为二值向量作为两个模式保存到DHNN
4. 使用外积法(Hebb学习规则)设计网络的权重参数,
5. 用异步迭代法,计算当输入一个全1向量时,网络的最终输出。
6. 计算当输入图片3.png时,网络的最终输出。
7. 计算当输入图片4.png时,网络的最终输出。

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

img2=Image.open("1.png")
img2=np.int64(np.array(img2)>127)
vector_train=img2.flatten()

img2=Image.open("2.png")
img2=np.int64(np.array(img2)>127)
vector_train2=img2.flatten()

attractors=np.array([vector_train,vector_train2])
attractors=attractors*2-1

print("保存的模式:n",attractors)
weight_matrix=np.zeros((len(attractors[0]),len(attractors[0])))

for attractor in attractors:
    temp_matrix=np.zeros((len(attractor),len(attractor)))
    for i in range(len(attractor)):
        for j in range(i+1,len(attractor)):
            temp_matrix[i,j]=attractor[i]*attractor[j]
            temp_matrix[j,i]=temp_matrix[i,j]
    weight_matrix+=temp_matrix
print("权重矩阵:n",weight_matrix)

def DHNN(input_vector):
    vector=input_vector.copy()
    stable_state=False
    random_order=np.arange(len(attractors[0]))
    while not stable_state:
        np.random.shuffle(random_order)
        stable_state=True
        for i in random_order:
            original_value=vector[i]
            vector[i]=weight_matrix[i].dot(vector)
            if(vector[i]>=0):
                vector[i]=1
            else:
                vector[i]=-1
            if(vector[i]!=original_value):
                print("节点",i,"发生变化",original_value,"->",vector[i])
                stable_state=False
    return vector

img2=np.ones((66,66))
x=img2.flatten()
r=DHNN(np.array(x))
matrix_test_update=r.reshape(img2.shape)
img=Image.fromarray(np.uint8(matrix_test_update))
img.show()

img2=Image.open("3.png")
img2=np.int64(np.array(img2)>127)
x=img2.flatten()
x=x*2-1
r=DHNN(np.array(x))
matrix_test_update=r.reshape(img2.shape)
img=Image.fromarray(np.uint8(matrix_test_update))
img.show()

img2=Image.open("4.png")
img2.show()
img2=np.int64(np.array(img2)>127)
x=img2.flatten()
x=x*2-1
r=DHNN(np.array(x))
matrix_test_update=r.reshape(img2.shape)
img=Image.fromarray(np.uint8(matrix_test_update))
img.show()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/316740.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号