I am trying to create a forward-propagation function in Python 3.8.2. The inputs look like this:
JavaScript
x
3
1
Test_Training_Input = [(1,2,3,4),(1.45,16,5,4),(3,7,19,67)]
2
Test_Training_Output = [1,1,0]
3
I am not using biases (not sure if they are that important and it makes my code very complicated) but I am using weights. The weights are stored in a list, Layer1W
, I’m not sure how long to make it, but I think, len(Test_Training_Input)+len(Test_Training_Output)
should work.
So far, my function looks like this:
JavaScript
1
8
1
def forwardprop():
2
global Layer1O
3
Layer1O = []
4
for init in range(0,len(Layer1W)):
5
total = sum(Test_Training_Input[1][1])*Layer1W[init]
6
Layer1O.append(relu(total))
7
return Layer1O
8
I think this is very wrong… Any suggestions?
Advertisement
Answer
This is a very rough explanation of a considerably complex topic, I highly recommend this e-book
JavaScript
1
82
82
1
import numpy as np
2
import numpy.random as rnd
3
4
#inputs
5
x = np.array([[0, 1, 0],
6
[1, 0, 1]])
7
8
# x is of shape 2 x 3
9
10
#Here is a 3 -> 2 network, layer 1 (L1) will have 3 neurons, layer 2 (L2) will have 2
11
# n00 is the first neuron of layer 1
12
# n01 is the second neuron of layer 2
13
# n02 is the third neuron of layer 3
14
15
# n10 is the first neuron of layer 2
16
# n11 is the second neuron of layer 2
17
18
#so there will be 6 weights, (let us assume their values also)
19
# W(n00->n10) = 0.1, W(n00->n11) = 0.2
20
# W(n01->n10) = 0.3, W(n01->n11) = 0.4
21
# W(n02->n10) = 0.5, W(n02->n11) = 0.6
22
23
# we will store the corresponding weights in this fashion
24
# you might wonder why make it 2 x 3 matrix, and not 3 x 2
25
# this is to make use of matrix multiplication
26
27
# n00 n01 n02
28
w = np.array([[0.1, 0.3, 0.5], #n10
29
[0.2, 0.4, 0.6]]) #n11
30
31
# so the weight between n02 and n11 = w[1, 2] = 0.6
32
33
# note the input should be a vector,
34
# for example if the input is like this x = [1,1,0], shape = 3
35
# we will feed it as a vector, i.e
36
# [[1], <- x0
37
# [1], <- x1
38
# [0]] <- x2
39
# shape = 3 x 1
40
41
# so there will be 2 outputs from the feedforward, given by
42
#o1 = w00*x0 + w01*x1 + w02*x2
43
#o2 = w10*x0 + w11*x1 + w12*x2
44
45
# this is just the matrix multiplication between w and x,
46
#
47
# [[0.1, 0.3, 0.5], X [[1],
48
# [0.2, 0.4, 0.6]] [1], = [[o1],
49
# [0]] [o2]]
50
#
51
52
def feedforward(w, x):
53
z = np.matmul(w, x)
54
return z
55
56
# we will make x a vector, i.e change the shape as 2 x (3 x 1)
57
x = x[:, :, np.newaxis]
58
print(x.shape)
59
print(feedforward(w, x[0]))
60
61
62
# for your example
63
x = np.array([[1,2,3,4],[1.45,16,5,4],[3,7,19,67]])
64
# make it to 3 x 4 x 1
65
x = x[:, :, np.newaxis]
66
67
#lets try a 4 -> 2 -> 1 network
68
69
w1 = rnd.normal(size=(2, 4))
70
w2 = rnd.normal(size=(1, 2))
71
72
wgt = [w1, w2]
73
74
def feedforward2(wght, x):
75
# best practice would be to put this in a class and store the outputs of each layer
76
for w in wght:
77
x = np.matmul(w, x)
78
return x
79
80
for i in x:
81
print(feedforward2(wgt, i))
82