티스토리 뷰
cs231n assignment#1
CIFAR-10 data
#Reshape the image data into rows
X_train = np.reshape(X_train, (X_train.shape[0], -1))
X_test = np.reshape(X_test, (X_test.shape[0], -1))
print(X_train.shape, X_test.shape)
//result: (5000, 3072) (500, 3072)
basic form: np.reshape( data, (row, column))
data를 row * column form으로 reshape한다.
위 cs231n code에 의하면, CIFAR-10 Image data를 하나의 긴 vector form으로 바꿔 다루고자 numpy의 reshape을 사용한 것이다.
CIFAR-10 Image data인 X_train의 본래 shape: (5000, 32, 32, 3) <- num training = 5000
X_train = np.reshape(X_train, (X_train.shape[0], -1))
코드 해석: X_train 데이터를 5000* ? form으로 바꾸자.
#-1 의 의미
-1은 data의 dimension을 모를 때 유용하게 사용할 수 있다. row 또는 column 중 하나만 값을 정해주고 나머지에 -1을 넣어준다면, 정해준 값에 맞춰서 reshape이 이뤄진다.
예를 들어,
a 라는 array가 있을 때, 이 array의 form을 모르는 상황이다.
a array를 3줄로 바꾸고 싶을 때! (row가 3줄이면 되는 상황, column수는 상관없다!)
np.array(a, (3,-1))
//result: array([[1,2],
[3,4],
[5,6]])
//a 는 range 1~6 을 갖는 array였군요.
cs231n의 코드를 다시 보면,
X_train = np.reshape(X_train, (X_train.shape[0], -1))
32*32*3 form의 5000개 data를
column 상관없이 5000개의 row를 맞췄으니, 32*32*3(3072) 배열로 이뤄져있던 data가 하나의 긴 vector로 reshape된 것이다.
'Basic section > Python' 카테고리의 다른 글
부모 디렉토리 참조 (0) | 2018.02.26 |
---|---|
softmax function overflow problem (0) | 2018.02.22 |
numpy array 차원의 인덱스 (0) | 2018.02.22 |
numpy 배열 부등호 연산 (0) | 2018.02.21 |
numpy newaxis (0) | 2018.02.21 |