453 KiB
453 KiB
In [1]:
import numpy as np
# We use the Sigmoid function as activation function
def sigmoid(z):
return 1.0/(1.0+np.exp(-z))
def forwardpropagation(x):
# weighted sum of inputs to the hidden layer
z_1 = np.matmul(x, w_1) + b_1
# activation in the hidden layer
a_1 = sigmoid(z_1)
# weighted sum of inputs to the output layer
z_2 = np.matmul(a_1, w_2) + b_2
a_2 = z_2
return a_1, a_2
def backpropagation(x, y):
a_1, a_2 = forwardpropagation(x)
# parameter delta for the output layer, note that a_2=z_2 and its derivative wrt z_2 is just 1
delta_2 = a_2 - y
print(0.5*((a_2-y)**2))
# delta for the hidden layer
delta_1 = np.matmul(delta_2, w_2.T) * a_1 * (1 - a_1)
# gradients for the output layer
output_weights_gradient = np.matmul(a_1.T, delta_2)
output_bias_gradient = np.sum(delta_2, axis=0)
# gradient for the hidden layer
hidden_weights_gradient = np.matmul(x.T, delta_1)
hidden_bias_gradient = np.sum(delta_1, axis=0)
return output_weights_gradient, output_bias_gradient, hidden_weights_gradient, hidden_bias_gradient
# ensure the same random numbers appear every time
np.random.seed(0)
# Input variable
x = np.array([4.0],dtype=np.float64)
# Target values
y = 2*x+1.0
# Defining the neural network, only scalars here
n_inputs = x.shape
n_features = 1
n_hidden_neurons = 1
n_outputs = 1
# Initialize the network
# weights and bias in the hidden layer
w_1 = np.random.randn(n_features, n_hidden_neurons)
b_1 = np.zeros(n_hidden_neurons) + 0.01
# weights and bias in the output layer
w_2 = np.random.randn(n_hidden_neurons, n_outputs)
b_2 = np.zeros(n_outputs) + 0.01
eta = 0.1
for i in range(50):
# calculate gradients
derivW2, derivB2, derivW1, derivB1 = backpropagation(x, y)
# update weights and biases
w_2 -= eta * derivW2
b_2 -= eta * derivB2
w_1 -= eta * derivW1
b_1 -= eta * derivB1[36.89563074] [23.62323175] [15.1251681] [9.68402334] [6.20020163] [3.96963458] [2.54150298] [1.62714703] [1.0417409] [0.66694492] [0.42699034] [0.27336592] [0.17501258] [0.11204514] [0.07173249] [0.04592382] [0.02940083] [0.01882264] [0.01205039] [0.00771475] [0.00493903] [0.003162] [0.00202433] [0.00129599] [0.0008297] [0.00053118] [0.00034006] [0.00021771] [0.00013938] [8.92313548e-05] [5.71263851e-05] [3.6572612e-05] [2.34139775e-05] [1.49897504e-05] [9.5965161e-06] [6.14373934e-06] [3.93325371e-06] [2.51808934e-06] [1.61209378e-06] [1.03207075e-06] [6.60737006e-07] [4.23007231e-07] [2.70811405e-07] [1.73374853e-07] [1.10995472e-07] [7.10598715e-08] [4.54928947e-08] [2.91247848e-08] [1.86458368e-08] [1.19371605e-08]
Warning:
Output truncated. This notebook contains too many cells to display efficiently.


