导入Matlab训练好的BP网络权值和偏置并用python编写BP神经网络预测程序

前言:这里的BP神经网络例子为3层网络,如需更多隐层,请自行更改

参考文献:matlab神经网络函数feedforwardnet构造的网络数学模型是啥 – MATLAB中文论坛第一页是储备知识,第二页是问题解决方法。

关键点:

       1.BP网络输入层到隐层的函数为tansig函数

                     tansig(n)=2/(1+exp(-2*n)) -1;

       2.BP网络隐层到输出层的函数为线性函数purelin

                    purelin(n)=n

       3.Matlab在数据输入的时候做了归一化,在数据输出的时候做了反归一化

                   归一化后数据范围【-1 1】。 所以需要实现Matlab的mapminmax函数。其中xinmin和xinmax需要从Matlab训练后的神经网络种得到,如图2。

                  在Matlab中,使用gensim(net)函数建立可视化simulink框图。

                      

                   

                    

                     

***一定得找到xinmin和xinmax,找不到就别想算得和Matlab一致。

       4.我的输出标签是1-7,你还需要改动程序,主要是归一化和反归一化那里的。

                                                   图1 三层神经网络的结构

                                                  图2  正确使用需要得到的关键参数

Matlab导出训练好的权值和偏置

%读取输入层到隐藏层的权重
w_in2hid.txt=net.IW{1,1};
save D:\w_in2hid.txt -ascii w_in2hid;
%读取输入层到隐藏层的偏置
bias_in2hid=net.b{1,1};
save D:\bias_in2hid.txt -ascii bias_in2hid;
%读取隐藏层到输出层的权重
w_hid2out=net.LW{2,1};
save D:\w_hid2out.txt -ascii w_hid2out;
%读取隐藏层到输出层的偏置
bias_hid2out=net.b{2,1};
save D:\bias_hid2out.txt -ascii bias_hid2out;

net.IW——输入层到隐层的权重

net.LW——输出层到隐层权重

net.b——所有层的偏置

把红色框住的权值和偏置保存下来就行。

我这里的保存成txt格式。名称为:

使用numpy的Python三层BP神经网络程序

#!/usr/bin/env python
# _*_ coding:utf-8 _*_

#使用python实现导入Matlab训练好的BP神经网络权值和偏置,做预测
#完成的是三层BP神经网络

import numpy as np
import time

class BPNetwork(object):
    def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
        # 设定输入层、隐藏层、输出层的节点数nodes、学习率
        self.input_nodes = input_nodes
        self.hidden_nodes = hidden_nodes
        self.output_nodes = output_nodes
        self.lr = learning_rate
        # 为了方便理解,在此将输入设置为mnist数据实例,方便理解各个数据的shape
        # 则input_nodes=784,hidden_nodes=32, output_nodes=64
        # 设定权重值
        # w_in2hid.shape=(32,784)
        self.w_in2hid = np.random.normal(0.0, self.hidden_nodes ** -0.5, (self.hidden_nodes, self.input_nodes))
        self.bias_in2hid = np.random.normal(0.0, self.hidden_nodes ** -0.5, (self.hidden_nodes, 1))
        # w_hid2out.shape=(64,32)
        self.w_hid2out = np.random.normal(0.0, self.output_nodes ** -0.5, (self.output_nodes, self.hidden_nodes))
        self.bias_hid2out = np.random.normal(0.0, self.output_nodes ** -0.5, (self.output_nodes, 1))
        # 激活函数(logistic函数,sigmoid函数)
        self.act_func = (lambda x: 1/(1+np.exp(-x)))
        self.act_tansig=(lambda y:(2/(1+np.exp(-2*y)))-1)

    def load_weight_and_bias(self,w_in,b_in,w_out,b_out):
        self.w_in2hid=w_in
        self.bias_in2hid=b_in
        self.w_hid2out=w_out
        self.bias_hid2out=b_out

    def run(self, inputs_org):
        inputs = np.array(inputs_org, ndmin=2).T
        #归一化输入变量
        xinmin=[106.981239723518,62.3965058481809,45.7142140543688,48.6927276935126,54.4764151404023,50.1104283521850,34.8897953727263,54.2732625530648]
        xinmax=[848.232912409228,370.481406524439,195.427888197606,241.759265358899,334.010783370080,209.190468299929,254.087884158159,267.991924217319]
        xinmax=np.array(xinmax, ndmin=2).T
        xinmin=np.array(xinmin, ndmin=2).T
        inputs = 2/(xinmax - xinmin)*(inputs-xinmin)-1

        # 实现前向传播
        hid_ints = np.dot(self.w_in2hid, inputs)
        hid_ints=np.add(hid_ints,np.array(self.bias_in2hid, ndmin=2).T)
        hid_outs = self.act_tansig(hid_ints)
        # 输出层
        out_ints = np.dot(self.w_hid2out, hid_outs)
        out_ints=np.add(out_ints,np.array(self.bias_hid2out, ndmin=2).T)

        # 反归一化输出变量
        out_outs = np.around((7-1)/2*(out_ints+1)+1)

        return out_outs

#读入数据
w_in2hid = np.loadtxt(r"F:\肌电在线识别运动模式\信号分析算法\w_in2hid.txt",dtype=float)   #将文件中数据加载到datax数组里
bias_in2hid = np.loadtxt(r"F:\肌电在线识别运动模式\信号分析算法\bias_in2hid.txt",dtype=float)   #将文件中数据加载到datax数组里
w_hid2out = np.loadtxt(r"F:\肌电在线识别运动模式\信号分析算法\w_hid2out.txt",dtype=float)   #将文件中数据加载到datax数组里
bias_hid2out = np.loadtxt(r"F:\肌电在线识别运动模式\信号分析算法\bias_hid2out.txt",dtype=float)   #将文件中数据加载到datax数组里


emg=BPNetwork(8,25,1,0.05)
emg.load_weight_and_bias(w_in2hid,bias_in2hid,w_hid2out,bias_hid2out)

# t = time.clock()
# endtime1 = int(round(t * 1000))
# print(endtime1)

dinput=[125.5005,   70.0132,   53.7063,   56.4048,   91.1870,  109.5663,  105.3259,   82.2865]

out=emg.run(dinput)

# t = time.clock()
# endtime2 = int(round(t * 1000))
# print(endtime2)

print(out)

  • 6
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kissgoodbye2012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值