본문 바로가기

DeepLearning Specialization(Andrew Ng)/Convolutional Neural Networks

[Week 1] Programming Assignments

728x90

1. Backpropagation in CNN

 

 기본적으로 Z = WA +b 의 구조는 기존 신경망과 동일하다. 다만, 저기서 언급하는 W는 필터, A는 필터가 씌어진 부분, b는 bias 이다. 

따라서 dA는 그 필터를 씌운 부분을 모오두 계산하므로 시그마가 두번 연산된다.

da_prev는 slice별로 기울기가 구해짐. 

dW도 필터별로 기울기가 구해짐.

db도 필터에 대응하는 것만 기울기가 구해짐.

 

 

 

2. Hare code란?

 

m.blog.naver.com/PostView.nhn?blogId=on21life&logNo=221352286781&proxyReferer=https:%2F%2Fwww.google.com%2F

 

Languages - Hard Coding, 하드 코딩 이란 무엇인가

변경, 숨김 혹은 암호화가 가능하지만 그러한 작업 없이 그대로 사용되는 소스코드 부분하드 코딩​하드 코...

blog.naver.com

3. TensorFlow CNN 여러 함수들

  • tf.nn.conv2d(X,W, strides = [1,s,s,1], padding = 'SAME'): given an input XX and a group of filters WW, this function convolves WW's filters on X. The third parameter ([1,s,s,1]) represents the strides for each dimension of the input (m, n_H_prev, n_W_prev, n_C_prev). Normally, you'll choose a stride of 1 for the number of examples (the first value) and for the channels (the fourth value), which is why we wrote the value as [1,s,s,1]. You can read the full documentation on conv2d.

합성곱 하는 법. training example과 channel에 대해서는 stride가 상식적으로 1이므로 1로 설정.

 

  • tf.nn.max_pool(A, ksize = [1,f,f,1], strides = [1,s,s,1], padding = 'SAME'): given an input A, this function uses a window of size (f, f) and strides of size (s, s) to carry out max pooling over each window. For max pooling, we usually operate on a single example at a time and a single channel at a time. So the first and fourth value in [1,f,f,1] are both 1. You can read the full documentation on max_pool.

  • tf.nn.relu(Z): computes the elementwise ReLU of Z (which can be any shape). You can read the full documentation on relu.

  • tf.contrib.layers.flatten(P): given a tensor "P", this function takes each training (or test) example in the batch and flattens it into a 1D vector.

    • If a tensor P has the shape (m,h,w,c), where m is the number of examples (the batch size), it returns a flattened tensor with shape (batch_size, k), where k=h×w×ck=h×w×c. "k" equals the product of all the dimension sizes other than the first dimension.
    • For example, given a tensor with dimensions [100,2,3,4], it flattens the tensor to be of shape [100, 24], where 24 = 2 3 4. You can read the full documentation on flatten.
  • tf.contrib.layers.fully_connected(F, num_outputs): given the flattened input F, it returns the output computed using a fully connected layer. You can read the full documentation on full_connected.

  • In the last function above (tf.contrib.layers.fully_connected), the fully connected layer automatically initializes weights in the graph and keeps on training them as you train the model. Hence, you did not need to initialize those weights when initializing the parameters.Window, kernel, filter

FC를 위해 쫙 펴진 F(batch size, k) 에 대하여, FC 층의 W를 자동으로 만들어주고 계산하여 결과값을 산출해준다. 

 

 

- The words "window", "kernel", and "filter" are used to refer to the same thing. This is why the parameter ksize refers to "kernel size", and we use (f,f) to refer to the filter size. Both "kernel" and "filter" refer to the "window." ( window = kernel = filter)

 

  • tf.nn.softmax_cross_entropy_with_logits(logits = Z, labels = Y): computes the softmax entropy loss. This function both computes the softmax activation function as well as the resulting loss. You can check the full documentation softmax_cross_entropy_with_logits.

logits 는 Z값이다. 여기서는 cost를 계산하기 위함이므로 마지막 layer의 z값.

 

  • tf.reduce_mean: computes the mean of elements across dimensions of a tensor. Use this to calculate the sum of the losses over all the examples to get the overall cost. You can check the full documentation reduce_mean.

3. CONV & RELU & POOL

 순서가 Conc => Relu => Pool 순서이다. relu랑 pool은 바꿔도 상관없을 것 같긴한데... 여튼 이렇게 하더라

 

4. Tensor Flow 팁

 

 아직 텐서플로가 굉장히 어색한데, 예를 들어 cost = compute_cost(변수, ...) 라고 한다면 이걸 실제로 실행할 때, 

session.run(fetches = [cost, ... ] , feed_dict = {변수 : 변수에 넣을 값, 변수: 변수에 넣을 값, ...}) 이런 구조로 해서 변수에 값을 feed 해준다. 

 

cost = compute_cost(Z3, Y)
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

with tf.Session() as sess
 	_ , temp_cost = sess.run(fetches = [optimizer,cost], feed_dict = [])

 근데 특이한 점은 특성 연산을 fetch로 가져올 때, 연계되어 있는 변수(placeholder)를 feed_dict으로 설정할 수 있다. 

실제로 결국 placeholder로 지정한 X와 Y를 X_train과 Y_train으로 feed 하였다. 

 

5. XavierInitializer

 

activation함수가 tanh 일 때, weight initialize를 위해 사용한다고 배웠는데, 여기서는 relu인데도 쓰네. 왜지?

728x90