神经网络的前向传播

时间:2021-03-08 14:45:10

标签: python neural-network

我正在尝试在 Python 3.8.2 中创建一个前向传播函数。输入看起来像这样:

Test_Training_Input = [(1,2,3,4),(1.45,16,5,4),(3,7,19,67)]
Test_Training_Output = [1,1,0]

我没有使用偏差(不确定它们是否那么重要,它使我的代码非常复杂)但我使用了权重。权重存储在一个列表中,Layer1W,我不确定需要多长时间,但我认为,len(Test_Training_Input)+len(Test_Training_Output) 应该可以工作。

到目前为止,我的函数是这样的:

def forwardprop():
    global Layer1O
    Layer1O = []
    for init in range(0,len(Layer1W)):
        total = sum(Test_Training_Input[1][1])*Layer1W[init]
        Layer1O.append(relu(total))
    return Layer1O

我认为这是非常错误的... 有什么建议吗?

3 个答案:

答案 0 :(得分:0)

考虑使用 numpy,这是一个处理矩阵的库。像前向传播这样的东西可以很容易地实现,例如:

import numpy as np

for layer in layers:
  inputs = np.dot(inputs, layer)

# this returns the outputs after propogating

这只是一个简单的例子,我排除了很多事情,比如在传播过程中缓存每一层的输入。

答案 1 :(得分:0)

因此通常在该函数中初始化任何变量不是一个好主意。初始化所有变量,可能在函数之前的构造函数中。另外我建议在编写之前手动创建或映射神经网络的结构。将您需要做的事情形象化可能会有所帮助。

如果你想尝试前向传播,你需要分解步骤,它有什么作用?首先,您确定您所在的层,如果您在输入上,然后将输入矩阵乘以下一个权重,等等。这应该可以更轻松地编写您的网络并减少您可能犯的错误数量。

答案 2 :(得分:0)

这是对相当复杂的主题的非常粗略的解释,我强烈推荐这个e-book

import numpy as np
import numpy.random as rnd

#inputs
x = np.array([[0, 1, 0],
             [1, 0, 1]])

# x is of shape 2 x 3

#Here is a 3 -> 2 network, layer 1 (L1) will have 3 neurons, layer 2 (L2) will have 2
#   n00 is the first neuron of layer 1
#   n01 is the second neuron of layer 2
#   n02 is the third neuron of layer 3

#   n10 is the first neuron of layer 2
#   n11 is the second neuron of layer 2

#so there will be 6 weights, (let us assume their values also)
#   W(n00->n10) = 0.1, W(n00->n11) = 0.2
#   W(n01->n10) = 0.3, W(n01->n11) = 0.4
#   W(n02->n10) = 0.5, W(n02->n11) = 0.6

# we will store the corresponding weights in this fashion
# you might wonder why make it 2 x 3 matrix, and not 3 x 2
# this is to make use of matrix multiplication

#                 n00   n01  n02
w =    np.array([[0.1, 0.3, 0.5],  #n10
                 [0.2, 0.4, 0.6]]) #n11

# so the weight between n02 and n11 = w[1, 2] = 0.6

# note the input should be a vector,
# for example if the input is like this x = [1,1,0], shape = 3
# we will feed it as a vector, i.e
# [[1], <- x0
#  [1], <- x1
#  [0]] <- x2
# shape =  3 x 1

# so there will be 2 outputs from the feedforward, given by
#o1 = w00*x0 + w01*x1 + w02*x2
#o2 = w10*x0 + w11*x1 + w12*x2

# this is just the matrix multiplication between w and x,
#
#   [[0.1, 0.3, 0.5],   X  [[1],
#    [0.2, 0.4, 0.6]]      [1],     =    [[o1],
#                          [0]]           [o2]]
#

def feedforward(w, x):
    z = np.matmul(w, x)
    return z

# we will make x a vector, i.e change the shape as 2 x (3 x 1)
x = x[:, :, np.newaxis]
print(x.shape)
print(feedforward(w, x[0]))


# for your example
x = np.array([[1,2,3,4],[1.45,16,5,4],[3,7,19,67]])
# make it to 3 x 4 x 1
x = x[:, :, np.newaxis]

#lets try a 4 -> 2 -> 1 network

w1 = rnd.normal(size=(2, 4))
w2 = rnd.normal(size=(1, 2))

wgt = [w1, w2]

def feedforward2(wght, x):
    # best practice would be to put this in a class and store the outputs of each layer
    for w in wght:
        x = np.matmul(w, x)
    return x 

for i in x:
    print(feedforward2(wgt, i))
相关问题