딥러닝 EXPRESS 06장 연습문제

2022. 3. 25. 01:30Study/딥러닝

반응형

 

책정보

 

제목 : 딥러닝 EXPRESS

저자 : 천인국

출판사 : 생능출판사

출판일 : 2021년 07월 16일

 

구매하기

 

예스24 - https://click.gl/nk9VvW

교보문고 - https://click.gl/2Sqg6V

 

필기노트

 

※ 직접 풀이한 문제이며, 정확한 답이 아님을 알려드립니다.

 

 

1.

- 시그모이드 함수: 미분 가능한 함수다. 0.0에서 1.0까지 연속적인 실수출력을 제공한다.

- ReLU 함수(Rectifed Linear Unit function) : 입력이 0을 넘으면 그대로 출력되고, 0보다 적으면 0이 출력되는 함수다.

- tanh 함수 : 시그모이드 함수와 비슷하며, -1.0에서 1.0까지 출력한다. 순환신경망에서 많이 사용된다.

 

2. 

import numpy as np

x=10
learning_rate=0.2
precision=0.00001
max_iterations=100

loss_func=lambda x:(x-3)**2+10

gradient=lambda x:2*x-6

for i in range(max_iterations):
	x=x-learning_rate*gradient(x)
    print("손실함수값(",x,")=",loss_func(x))

print("최소값 = ",x)

 

3. 

 

4. 은닉층을 h1, h2, h3, 출력층을 y라고 하고 각각 입력의 총합을 net, f(net)을 out이라 했을 때

net(h1)=0.8*1+0.2*0.2=1.0

out(h1)=f(1.0)=1.0

net(h2)=0.4*1+0.9*1=1.3

out(h2)=f(1.3)=1.3

net(h3)=0.2*1+0.5*1=0.7

out(h3)=f(0.7)=0.7

net(y)=0.3*1.0+0.5*1.3+0.9*0.7=0.3+0.65+0.63=1.58

out(y)=1.58

순방향 패스 1.58

오차 = (0-1.58)**2/2 = 1.2482

 

5.

- E'(y)/w7' w7은 h1에서 y로의 가중치

E'(y)/w7' = E'(y)/out'(y) * out'(y)/net'(y) * net'(y)/w7'

(E'(y)/out'(y)=out(y)-target(y))

(out'(y)/net'(y)=out(y)*(1-out(y))

(net'(y)/w7'=out(h1)

= (out(y)-target(y)) * (out(y)*(1-out(y))) * (out(h1))

= (1.58-0) * 1.58*(1-1.58) * 1.0

-1.447912

w7(t+1)=w7(t)-학습률 * E'(y)/w7' (t는 현재 가중치)

 

- E'(y)/w8 w8은 h2에서 y로의 가중치

E'(y)/w8' = E'(y)/out'(y) * out'(y)/net'(y) * net'(y)/w8'

= (1.58-0) * 1.58*(1-1.58) * 1.3

= -1.8822856

 

w8(t+1)=w8(t)-학습률*E'(y)/w8'

 

- E'(y)/w9 w9은 h3에서 y로의 가중치

E'(y)/w9' = E'(y)/out'(y) * out'(y)/net'(y) * net'(y)/w9'

= (1.58-0) * 1.58*(1-1.58) * 0.7

= -1.0135384

 

w9(t+1)=w9(t)-학습률*E'(y)/w9'

 

- E'(y)/w1, w1은 입력 x1에서 은닉층 h1으로의 가중치

= E'(y)/out'(h1) * out'(h1)/net'(h1) * net'(h1)/w1'

(E'(y)/out'(h1)= E'(y)/out'(y) * out'(y)/net'(y) * net'(y)/out'(h1) = -1.447912 * 0.3 = -0.4343736)

(E'(y)/out'(y) * out'(y)/net'(y)= -1.447912)

(net'(y)/out'(h1) = w7)

(out'(h1)/net'(h1) = out(h1)*(1-out(h1))=0)

(net'(h1)/w1'=x1=1.0)

= -0.4343736 * 0 * 1.0 = 0

w1(t+1)=w1(t)-학습률*E'(y)/w1'

 

- E'(y)/w2' w2는 입력 x1에서 은닉층 h2으로의 가중치

= (-1.447912 * w8) * (1.3 * (1-1.3)) * x1

= 0.28234284

w2(t+1)=w2(t)-학습률*E'(y)/w2'

 

- E'(y)/w3' w3은 입력 x1에서 은닉층 h3으로의 가중치

= (-1.447912 * w9) * (0.7 * (1-0.7)) * x1

-0.273655368

 

w3(t+1)=w3(t)-학습률*E'(y)/w3'

 

- E'(y)/w5' w5는 입력 x2에서 은닉층 h2으로의 가중치

= (-1.447912 * w8) * (1.3 * (1-1.3)) * x1

= 0.28234284

w5(t+1)=w5(t)-학습률*E'(y)/w5'

 

- E'(y)/w6' w6은 입력 x2에서 은닉층 h3으로의 가중치

= (-1.447912 * w9) * (0.7 * (1-0.7)) * x1

-0.273655368

w6(t+1)=w6(t)-학습률*E'(y)/w6'

 

6.

import numpy as np

#ReLU 함수
def actf(x):
	return x>=0?x:0

#ReLU 함수의 미분
def actf_deriv(x):
	if(x>0):
    	return 1
    else if(x<0):
    	return 0
    else:
    	print("error")
        return

inputs,hiddens,outputs=2,3,1
learning_rate=0.2

X=np.array([0,0],[0,1],[1,0],[1,1]])
T=np.array([[1],[0],[0],[1]])

W1=np.array([[0.8,0.4,0.3],[0.2,0.9,0.5]])
W2=np.array([[0.3],[0.5],[0.9]])
B1=np.array([0.0,0.0,0.0])
B2=np.array([0.0])

def predict(x):
	layer0=x
    Z1=np.dot(layer0,W1)+B1
    layer1=actf(Z1)
    Z2=np.dot(layer1,W2)+B2
    layer2=actf(Z2)
    return layer0,layer1,layer2

def fit():
	global W1,W2,B1,B2:
    for i in range(90000):
    	for x,y in zip(X,T):
        	x=np.reshape(x,(1,-1))
            y=np.reshape(y,(1,-1))
            
            layer0,layer1,layer2=predict(x)
            layer2_error=layer2-y
            layer2_delta=layer2_error*actf_deriv(layer2)
            layer1_error=np.dot(layer2_delta,W2.T)
            layer1_delta=layer1_error*actf_deriv(layer1)
            
            W2+=-learning_rate*np.dot(layer1.T,layer2_delta)
            W1+=-learning_rate*np.dot(layer0.T,layer1_delta)
            B2+=-learning_rate*np.sum(layer2_delta,axis=0)
            B1+=-learning_rate*np.sum(layer1_delta,axis=0)

def test():
	for x,y in zip(X,T):
    	x=np.reshape(x,(1,-1))
        layer0,layer1,layer2=predict(x)
        print(x,y,layer2)

fit()
test()

 

7. 

#tanh 함수
def actf(x):
	return np.tanh(x)

#tanh 함수의 미분
def actf_deriv(x):
	return (1-np.tanh(x))*(1+np.tanh(x))

 

8. 

 

9. 

import numpy as np
from sklearn.datasets import load_iris
from sklearn.linear_model import Perceptron

iris=load_iris()
X=iris.data[:,(0,1)]
y=(iris.target==0).astype(np.int)

percep=Perceptron(random_state=32)
percep.fit(X,y

 

 

 

 

 

 

 

 

 

 

 

 

반응형