BP神经网络回归预测模型(python实现)

       神经网络模型一般用来做分类,回归预测模型不常见,本文基于一个用来分类的BP神经网络,对它进行修改,实现了一个回归模型,用来做室内定位。模型主要变化是去掉了第三层的非线性转换,或者说把非线性激活函数Sigmoid换成f(x)=x函数。这样做的主要原因是Sigmoid函数的输出范围太小,在0-1之间,而回归模型的输出范围较大。模型修改如下:代码如下:#coding: utf8''''...
摘要由CSDN通过智能技术生成

       神经网络模型一般用来做分类,回归预测模型不常见,本文基于一个用来分类的BP神经网络,对它进行修改,实现了一个回归模型,用来做室内定位。模型主要变化是去掉了第三层的非线性转换,或者说把非线性激活函数Sigmoid换成f(x)=x函数。这样做的主要原因是Sigmoid函数的输出范围太小,在0-1之间,而回归模型的输出范围较大。模型修改如下:



代码如下:

#coding: utf8
''''
author: Huangyuliang
'''
import json
import random
import sys
import numpy as np

#### Define the quadratic and cross-entropy cost functions
class CrossEntropyCost(object):

    @staticmethod
    def fn(a, y):
        return np.sum(np.nan_to_num(-y*np.log(a)-(1-y)*np.log(1-a)))

    @staticmethod
    def delta(z, a, y):
        return (a-y)

#### Main Network class
class Network(object):

    def __init__(self, sizes, cost=CrossEntropyCost):

        self.num_layers = len(sizes)
        self.sizes = sizes
        self.default_weight_initializer()
        self.cost=cost

    def default_weight_initializer(self):

        self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
        self.weights = [np.random.randn(y, x)/np.sqrt(x)
                        for x, y in zip(self.sizes[:-1], self.sizes[1:])]
    def large_weight_initializer(self):

        self.biases = [np.random.randn(y, 1) for y in self.sizes[1:]]
        self.weights = [np.random.randn(y, x)
                        for x, y in zip(self.sizes[:-1], self.sizes[1:])]
    def feedforward(self, a):
        """Return the output of the network if `	
  • 38
    点赞
  • 352
    收藏
    觉得还不错? 一键收藏
  • 34
    评论
以下是使用Python实现BP神经网络回归代码: ```python import numpy as np class BPNeuralNetwork: def __init__(self, hidden_units): self.hidden_units = hidden_units self.input_weights = None self.output_weights = None self.hidden_layer = None self.output_layer = None def fit(self, X, y, lr=0.1, epochs=1000): n_features = X.shape[1] n_output = y.shape[1] self.input_weights = np.random.randn(n_features, self.hidden_units) self.output_weights = np.random.randn(self.hidden_units, n_output) for epoch in range(epochs): # Forward propagation through the network self.hidden_layer = self.sigmoid(np.dot(X, self.input_weights)) self.output_layer = np.dot(self.hidden_layer, self.output_weights) # Backpropagation of the error output_error = y - self.output_layer output_delta = output_error hidden_error = np.dot(output_delta, self.output_weights.T) * self.sigmoid_derivative(self.hidden_layer) hidden_delta = hidden_error # Updating the weights with the calculated deltas self.output_weights += lr * np.dot(self.hidden_layer.T, output_delta) self.input_weights += lr * np.dot(X.T, hidden_delta) def predict(self, X): hidden_layer = self.sigmoid(np.dot(X, self.input_weights)) output_layer = np.dot(hidden_layer, self.output_weights) return output_layer def sigmoid(self, x): return 1 / (1 + np.exp(-x)) def sigmoid_derivative(self, x): return x * (1 - x) ``` 其中,`hidden_units`参数是指隐藏层中神经元的数量,`lr`参数是指学习率,`epochs`参数是指训练次数。在`fit`函数中,我们首先初始化输入层与输出层的权重矩阵,然后进行前向传播和反向传播,最后根据计算得到的误差更新权重。
评论 34
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值