지금까지 합성곱 계층과 풀링 계층에 대해 자세히 설명했습니다. 이번 절에서는 이들 두 계층
을 파이썬으로 구현해보겠습니다. “
5
장 오차역전파법”에서 설명한 것처럼 이번 절에서 구현하
는 클래스에도
forward
와
backward
메서드를 추가하여 모듈로 이용할 수 있도록 했습니다.
합성곱 계층과 풀링 계층은 복잡해 보이지만, 사실 ‘트릭’을 사용하면 쉽게 구현할 수 있습니다.
이번 절에서는 그 트릭을 활용해 문제를 간단히 하면서 합성곱 계층을 구현해보겠습니다.
7.4.1
4
차원 배열
앞에서 설명한 대로
CNN
에서 계층 사이를 흐르는 데이터는
4
차원입니다. 예를 들어 데이터의
형상이 (
10
,
1
,
28
,
28
)이라면, 이는 높이
28
, 너비
28
, 채널
1
개인 데이터가
10
개라는 이야
깁니다. 이를 파이썬으로 구현하면 다음과 같습니다.
>>>
x
=
np
.
random
.
rand
(
10
,
1
,
28
,
28
)
# 무작위로 데이터 생성
>>>
x
.
shape
(
10
,
1
,
28
,
28
)
여기에서 (
10
개 중) 첫 번째 데이터에 접근하려면 단순히
x
[
0
]이라고 씁니다(파이썬의 인덱
스는
0
부터 시작합니다). 마찬가지로 두 번째 데이터는
x
[
1
] 위치에 있습니다.
>>>
x
[
0
].
shape
#
(
1
,
28
,
28
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.