Neural Network

init

各层神经元的个数  存在[ ]里  [258, 52, 10]

权重矩阵 相邻两层各个神经元之间的权重

激活函数



fit 

训练的数据存储在[ ]里,行数就是数据的个数,列数就是维度的维数

forward  我们抽样一部分数据,每次产生0~行数之间的随机数,以该随机数为行数,将该行数据传入第一层

backpa


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np  # 矩阵运算


def tanh(x):
    return np.tanh(x)


def tanh_deriv(x):  # 对tanh求导
    return 1.0 - np.tanh(x) * np.tanh(x)


def logistic(x):  # s函数
    return 1 / (1 + np.exp(-x))


def logistic_derivative(x):  # 对s函数求导
    return logistic(x) * (1 - logistic(x))


class NeuralNetwork:  # 面向对象定义一个神经网络类
    def __init__(self, layers, activation='tanh'):  # 下划线构造函数self 相当于本身这个类的指针 layer就是一个list 数字代表神经元个数
        """
        :param layers: A list containing the number of units in each layer.
        Should be at least two values
        :param activation: The activation function to be used. Can be
        "logistic" or "tanh"
        """
        if activation == 'logistic':
            self.activation = logistic  # 之前定义的s函数
            self.activation_deriv = logistic_derivative  # 求导函数
        elif activation == 'tanh':
            self.activation = tanh  # 双曲线函数
            self.activation_deriv = tanh_deriv  # 求导双曲线函数

        self.weights = []  # 初始化一个list作为   权重
        # 初始化权重两个值之间随机初始化
        for i in range(1, len(layers) - 1):  # 有几层神经网络 除去输出层
            # i-1层 和i层之间的权重 随机生成layers[i - 1] + 1 *  layers[i] + 1 的矩阵 -0.25-0.25
            self.weights.append((2 * np.random.random((layers[i - 1] + 1, layers[i] + 1)) - 1) * 0.25)
            # i层和i+1层之间的权重
            self.weights.append((2 * np.random.random((layers[i] + 1, layers[i + 1])) - 1) * 0.25)

    def fit(self, X, y, learning_rate=0.2, epochs=10000):  # 训练神经网络
        # learning rate
        X = np.atleast_2d(X)  # x至少2维
        temp = np.ones([X.shape[0], X.shape[1] + 1])  # 初始化一个全为1的矩阵
        temp[:, 0:-1] = X  # adding the bias unit to the input layer
        X = temp
        y = np.array(y)

        for k in range(epochs):
            i = np.random.randint(X.shape[0])  # 随机选行
            a = [X[i]]

            for l in range(len(self.weights)):  # going forward network, for each layer
                # 选择一条实例与权重点乘 并且将值传给激活函数,经过a的append 使得所有神经元都有了值(正向)
                a.append(self.activation(np.dot(a[l], self.weights[
                    l])))  # Computer the node value for each layer (O_i) using activation function
            error = y[i] - a[-1]  # Computer the error at the top layer 真实值与计算值的差(向量)
            # 通过求导 得到权重应当调整的误差
            deltas = [
                error * self.activation_deriv(a[-1])]  # For output layer, Err calculation (delta is updated error)

            # Staring backprobagation 更新weight
            for l in range(len(a) - 2, 0, -1):  # we need to begin at the second to last layer 每次减一
                # Compute the updated error (i,e, deltas) for each node going from top layer to input layer

                deltas.append(deltas[-1].dot(self.weights[l].T) * self.activation_deriv(a[l]))
            deltas.reverse()
            for i in range(len(self.weights)):
                layer = np.atleast_2d(a[i])
                delta = np.atleast_2d(deltas[i])
                self.weights[i] += learning_rate * layer.T.dot(delta)

    def predict(self, x):
        x = np.array(x)
        temp = np.ones(x.shape[0] + 1)
        temp[0:-1] = x
        a = temp
        for l in range(0, len(self.weights)):
            a = self.activation(np.dot(a, self.weights[l]))
        return a



nn = NeuralNetwork([2, 2, 1], 'tanh')
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0])
nn.fit(X, y)
for i in [[0, 0], [0, 1], [1, 0], [1, 1]]:
    print(i, nn.predict(i))

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页