This article is a learning memo of Deep Learning from scratch.
--Activation function: It has the role of determining how the sum of the input signals is activated (ignites). In Perceptron, a "step function" that switches the output at the threshold value is used, but in a neural network, a "sigmoid function" or "ReLU function" that draws a smooth curve is used. The activation function of the hidden layer is represented by h (), and the activation function of the output layer is represented by σ (). --By using the inner product of the matrix, it is possible to perform calculations for each neuron in a single layer.
h(x) = \frac{1}{1+ \mathrm{e}^{-x}}
a = w_1x_1+w_2x_2+b
z = h(a)
py3:3layered_neuralnetwork.py.py
import numpy as np
import matplotlib.pyplot as plt
#Weight and bias initialization
def init_network():
network = {}
#1st layer
network['W1'] = np.array([[0.1, 0.3, 0.5], [0.2, 0.4, 0.6]])
network['b1'] = np.array([0.1, 0.2, 0.3])
#2nd layer
network['W2'] = np.array([[0.1, 0.4], [0.2, 0.5], [0.3, 0.6]])
network['b2'] = np.array([0.1, 0.2])
#3rd layer
network['W3'] = np.array([[0.1, 0.3], [0.2, 0.4]])
network['b3'] = np.array([0.1, 0.2])
return network
#Input → Output
def forward(network, x):
W1, W2, W3 = network['W1'], network['W2'], network['W3']
b1, b2, b3 = network['b1'], network['b2'], network['b3']
#1st layer
a1 = np.dot(x, W1) +b1 # A = XW +B
z1 = sigmoid(a1) # Z = h(A)
#2nd layer
a2 = np.dot(z1, W2) +b2
z2 = sigmoid(a2)
#3rd layer
a3 = np.dot(z2, W3) +b3
y = identity_function(a3) #Only the last layer has a different activation function
return y
#Sigmoid function(Activation function)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
#Identity function(Activation function)
def identity_function(x):
return x
#Check the operation below
network = init_network()
x = np.array([1.0, 0.5])
y = forward(network, x)
print(y) # [0.31682708 0.69627909]
Recommended Posts