본문 바로가기
인공지능

[밑바닥부터 시작하는 딥러닝] Part_1/CHAPTER 5 오차역전파법

by SBOX Learning by doing 2022. 7. 23.
반응형
import numpy as np
In [1]:
class Relu:
    def __init__(self):
        self.mask = None

    def forward(self, x):
        self.mask = (x <= 0)
        out = x.copy()
        out[self.mask] = 0

        return out

    def backward(self, dout):
        dout[self.mask] = 0
        dx = dout

        return dx
In [4]:
x = np.array([[1.0, -0.5], [-2.0, 3.0]])
In [5]:
print(x)
[[ 1.  -0.5]
 [-2.   3. ]]
In [6]:
mask = (x <= 0)
In [7]:
print(mask)
[[False  True]
 [ True False]]
In [8]:
# coding: utf-8
import sys, os
sys.path.append(os.pardir)  # 부모 디렉토리의 파일을 가져올 수 있도록 설정
import numpy as np
from dataset.mnist import load_mnist
from two_layer_net import TwoLayerNet

# 데이터읽기
(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, one_hot_label=True)

network = TwoLayerNet(input_size=784, hidden_size=50, output_size=10)

x_batch = x_train[:3]
t_batch = t_train[:3]

grad_numerical = network.numerical_gradient(x_batch, t_batch)
grad_backprop = network.gradient(x_batch, t_batch)
# 각 가중치의 절대 오차의 평균을 구한다.
for key in grad_numerical.keys():
    diff = np.average( np.abs(grad_backprop[key] - grad_numerical[key]) )
    print(key + ":" + str(diff))
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-8-1485d1831192> in <module>
      3 sys.path.append(os.pardir)  # 부모 디렉토리의 파일을 가져올 수 있도록 설정
      4 import numpy as np
----> 5 from dataset.mnist import load_mnist
      6 from two_layer_net import TwoLayerNet
      7 

ModuleNotFoundError: No module named 'dataset.mnist'
In [ ]:
 
반응형

댓글