728x90
반응형
# Create library "my dnn"

import numpy as np

def sigmoid_f(X) :
    return 1/(1+np.exp(-X))

def sigmoid_b(XE, X) :
    return X*(1-X)*XE

def relu_f(X) :
    return (X>0)*X

def relu_b(XE, X) :
    return (X>0)*1*XE

def softmax_f(X) :
    XMax = np.max(X)
    X = X - XMax
    sumX = np.sum(np.exp(X))
    return np.exp(X)/sumX

def softmax_b(XE, X) :
    return XE # do nothing

def initialize_weight_He(nRow, nCol) :
    W = np.random.uniform(-1, 1, (nRow, nCol))
    return W*np.sqrt(6.0/W.shape[0])
"""
가중치 초기화 HE
가중치 (W) 는 np. 참조 random 난수로 uniform 균등분포로
(7행, 8열) 사이에 최소 -1 에서 1 안으로 랜덤하게 수를 넣어줌.
이러한 W 를 행열 로 만들어서 6으로 나눠 제곱근하여 초기화
"""

def initialize_weight_Le(nRow, nCol) :
    W = np.random.uniform(-1, 1, (nRow, nCol))
    return W*np.sqrt(3.0/W.shape[0])

def calculate_MSE(Y, YT) :
    E = (Y - YT)@(Y - YT).T/2
    return E[0, 0]

def calculate_CEE(Y, YT) :
    e = -np.sum(YT * np.log(Y))
    return e
import numpy as np

from mydnn import *

np.set_printoptions(formatter = {'float_kind':lambda x:"{0:6.3f}".format(x)})

NUM_PATTERN = 10
NUM_X = 7
NUM_H = 8
NUM_Y = 4

xs = np.array([
    [[1, 1, 1, 1, 1, 1, 1]],    # 0
    [[0, 1, 1, 0, 0, 0, 0]],    # 1 
    [[1, 1, 0, 1, 1, 0, 1]],    # 2 
    [[1, 1, 1, 1, 0, 0, 1]],    # 3
    [[0, 1, 1, 0, 0, 1, 1]],    # 4
    [[1, 0, 1, 1, 0, 1, 1]],    # 5
    [[0, 0, 1, 1, 1, 1, 1]],    # 6    
    [[1, 1, 1, 0, 0, 0, 0]],    # 7
    [[1, 1, 1, 1, 1, 1, 1]],    # 8
    [[1, 1, 1, 0, 0, 1, 1]],    # 9
])

yts = np.array([
    [[ 0, 0, 0, 0 ]],
    [[ 0, 0, 0, 1 ]],
    [[ 0, 0, 1, 0 ]],
    [[ 0, 0, 1, 1 ]],
    [[ 0, 1, 0, 0 ]],
    [[ 0, 1, 0, 1 ]],
    [[ 0, 1, 1, 0 ]],
    [[ 0, 1, 1, 1 ]],
    [[ 1, 0, 0, 0 ]],
    [[ 1, 0, 0, 1 ]],
])

np.random.seed(0)

WH = initialize_weight_He(NUM_X, NUM_H)
BH = np.zeros((1, NUM_H))
WY = initialize_weight_Le(NUM_H, NUM_Y)
BY = np.zeros((1, NUM_Y))
lr = 0.01

for epoch in range(0, 10000) :
    
    X = xs[2]
    YT = yts[2]
    
    H = relu_f(X @ WH + BH)
    Y = sigmoid_f(H @ WY + BY)
    
    e = calculate_MSE(Y, YT)
    
    YE = sigmoid_b(Y - YT, Y)
    HE = relu_b(YE @ WY.T, H)
    
    WYE = H.T @ YE
    BYE = 1 * YE
    WHE = X.T @ HE
    BHE = 1 * HE
    
    WY -= lr * WYE
    BY -= lr * BYE
    WH -= lr * WHE
    BH -= lr * BHE
    
    if epoch % 100 == 99 :
        print('epoch =', epoch)
        print(Y)
        
    if e < 0.0000001 : break
728x90
반응형

'Deep Learning' 카테고리의 다른 글

CNN  (0) 2022.09.05
Basic Tensorflow  (0) 2022.09.02
Activation Function(sigmoid, relu, softmax)  (0) 2022.09.01
Matrix and Calculation  (0) 2022.08.31
single perceptron.py  (0) 2022.08.31

+ Recent posts