跳转至

神经网络预测导论:前向传播

3.1 什么是预测

旋钮➡权重

预测结果 = 权重 * 输入数据

神经网络由多个层(layer)组成,每一层包含若干个神经元。层与层之间通过权重连接,形成一个网络结构。

  • 神经:神经元,是基本的计算单元。每个神经元接受输入数据,进行一定的计算(例如加权求和),然后将输出传递给下一个神经元
  • 网络:大量神经元和它们之间的连接组成的结构和信息流动路径。

3.4 这个神经网络做了什么?

将输入乘以权重,将输入"缩放"一定的比例.

weight = 0.1
def neural_network(input, weight):
    prediction = input * weight
    return prediction

number_of_toes = [8.5, 9.5, 10, 9]
input = number_of_toes[0]
pred = neural_network(input, weight)
print(pred)
//输出0.8500000000000001

神经网络的交互: - 接受输入变量,以此作为信息来源; - 拥有权重变量,一次作为知识; - 融合以上,输出预测结果

权重:作为网络的输入和预测之间敏感度的度量

3.5 使用多个输入进行预测

神经网络可以融合多个数据点的智能

智能:描述神经网络通过学习和分析大量数据点(或数据集)的能力,从而提取出这些数据中的模式、关系和特征,并用这些学习到的信息来进行预测、分类或其他任务。

weights = [0.1, 0.2, 0]

def w_sum(a, b):
    assert(len(a) == len(b))
    output = 0
    for i in range(len(a)):
        output += (a[i] * b[i])

    return output

def neural_network(input, weight):
    pred = w_sum(input, weights)
    return pred

toes = [8.5, 9.5, 9.9, 9.0]
wlrec = [0.65, 0.8, 0.8, 0.9]
nfans = [1.2, 1.3, 0.5, 1.0]

input = [toes[0], wlrec[0], nfans[0]]

pred = neural_network(input, weight)

print(pred)
//0.9800000000000001

3.6 多个输入:这个神经网络做了什么?

将三个输入 * 三个权重并求和➡加权和

  • 加权和: 一组数值乘以相应的权重后再求和的结果
  • 向量:一个数字列表(input,weight,toes...)

这种网络可以接受多个输入并作出预测,使其能够结合多种形式,做出更明智的决定

使用NumPy

numerical Python

import numpy as np

weights = np.array([0.1, 0.2, 0])

def neural_network(input, weights):
    //dot函数:点乘
    pred = input.dot(weights)
    return pred

toes = np.array([8.5, 9.5, 9.9, 9.0])
wlrec = np.array([0.65, 0.8, 0.8, 0.9])
nfans = np.array([1.2, 1.3, 0.5, 1.0])

input = np.array([toes[0], wlrec[0], nfans[0]])

pred = neural_network(input, weights)
print(pred)
//0.9800000000000001

3.8 预测多个输出

神经网络实现只有一个输入做出多个预测

在这个案例,三项预测结果完全独立,每个子网络接收相同的输入数据

在python,先定义函数再调用

# 定义 ele_mul 函数
def ele_mul(number, vector):
    output = [0, 0, 0]
    assert(len(output) == len(vector))
    for i in range(len(vector)):
        output[i] = number * vector[i]
    return output

# 定义 neural_network 函数
def neural_network(input, weights):
    pred = ele_mul(input, weights)
    return pred

# 定义权重和输入
weights = [0.3, 0.2, 0.9]
wlrec = [0.65, 0.8, 0.8, 0.9]
input = wlrec[0]

# 调用 neural_network 函数并打印结果
pred = neural_network(input, weights)
print(pred)
//[0.195, 0.13, 0.5850000000000001]

3.9 使用多个输入和输出进行预测

输入➡权重➡输出

//向量的数组称为矩阵
weights = [ [0.1, 0.1, -0.3],
            [0.1, 0.2, 0.0],
            [0.0, 1.3, 0.1]]

def w_sum(a, b):
    assert(len(a) == len(b))
    output = 0
    for i in range(len(a)):
        output += (a[i] * b[i])
    return output

def vect_mat_mul(vect, matrix):
    //assert语句用于调试:True则continue,false则error
    assert(len(vect) == len(matrix))
    output = [0, 0, 0]    
    for i in range(len(vect)):
        output[i] = w_sum(vect, matrix[i])
    return output

def neural_network(input, weights):
    pred = vect_mat_mul(input, weights)
    return pred

toes = [8.5, 9.5, 9.9, 9.0]
wlrec = [0.65, 0.8, 0.8, 0.9]
nfans = [1.2, 1.3, 0.5, 1.0]

input = [toes[0], wlrec[0], nfans[0]]
pred = neural_network(input, weights)
print(pred)
//[0.555, 0.9800000000000001, 0.9650000000000001]

3.10 多输出多输出神经网络的工作原理

对输入数据执行三次独立的加权和操作,产生三个预测结果

想象这个神经网络为三个独立的点积操作

  • 点积用于计算神经元的输入和权重之间的关系。

给定两个向量 $$ \mathbf{a} = [a_1, a_2, \ldots, a_n] 和 \mathbf{b} = [b_1, b_2, \ldots, b_n] $$

它们的点积定义为: $$ [ \mathbf{a} \cdot \mathbf{b} = a_1b_1 + a_2b_2 + \ldots + a_nb_n ] $$

  • 针对输入分别做三次独立的求加权和。每个输出节点都会针对输入节点进行自己的加权和操作,并做出相应预测

3.11 用预测结果进一步预测

神经网络可以堆叠

两个连续的向量矩阵乘法

import numpy as np

ih_wgt = np.array([
    [0.1, 0.2, -0.1],
    [-0.1, 0.1, 0.9],
    [0.1, 0.4, 0.1]
])

hp_wgt = np.array([
    [0.3, 1.1, -0.3],
    [0.1, 0.2, 0.0],
    [0.0, 1.3, 0.1]
])

weights = [ih_wgt, hp_wgt]

def neural_network(input, weights):
    hid = input.dot(weights[0])
    pred = hid.dot(weights[1])
    return pred

toes = np.array([8.5, 9.5, 9.9, 9.0])
wlrec = np.array([0.65, 0.8, 0.8, 0.9])
nfans = np.array([1.2, 1.3, 0.5, 1.0])

input = np.array([toes[0], wlrec[0], nfans[0]])

pred = neural_network(input, weights)
print(pred)
//[ 0.496  1.256 -0.286]???

3.12 NumPy快速入门

向量和矩阵

import numpy as np

a = np.array([0, 1, 2, 3]) #一个向量
b = np.array([4, 5, 6, 7]) #另一个向量
c = np.array([[0, 1, 2, 3],  #一个矩阵
              [4, 5, 6, 7]
             ])

d = np.zeros((2, 4))  # 2*4的全零矩阵
e = np.random.rand(2, 5) #2*5的随机矩阵,元素值再0 - 1之间

print(a)
print(b)
print(c)
print(d)
print(e)

[0 1 2 3]
[4 5 6 7]
[[0 1 2 3]
 [4 5 6 7]]
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]]
[[0.38159036 0.68556592 0.94706175 0.72064966 0.0586601 ]
 [0.47688729 0.41322412 0.934228   0.59755553 0.83321826]]

print(a * 0.1) #将向量a中每一个数字x0.1
print(c * 0.2) #将向量c中每一个数字x0.2
print(a * b) #对向量a 和 b执行逐元素乘法
print(a * b * 0.2) #执行逐元素乘法再x0.2
print(a * c) #对矩阵c的每一行执行逐元素乘法

print(a * e) #列数不同->异常

[0.  0.1 0.2 0.3]
[[0.  0.2 0.4 0.6]
 [0.8 1.  1.2 1.4]]
[ 0  5 12 21]
[0.  1.  2.4 4.2]
[[ 0  1  4  9]
 [ 0  5 12 21]]

待续...