티스토리 뷰

Multilayer Neural Networks

 - 텐서플로우를 이용하여 다층 레이어를 이용한 네트워크를 설계해보자.

 - hidden layer를 추가하게 될 경우 더 복잡한 모델을 만들 수 있다.

 - 또한 hidden layer단에서 비선형(non-linear) activation 함수를 사용하게될 경우

 - 비선형 함수를 모델링 할 수 있다.


 - 텐서플로우를 사용한 hidden-layer의 구현 중 가장 먼저 알아야 할 내용은 

 - ReLU hidden layer이다.

 - ReLU는 비선형 함수이고 정제된 선형 유닛(rectified linear unit)이다.

 - ReLU함수는 음수에 대해선 0을, 0 이상인 x에 대해서는 x 자신을 리턴한다.




TensorFlow ReLUs

 - 텐서플로우는 ReLU함수를 tf.nn.relu() 키워드로 사용하게끔 제공해준다.

1
2
3
4
5
# Hidden Layer with ReLU activation function
hidden_layer = tf.add(tf.matmul(features, hidden_weights), hidden_biases)
hidden_layer = tf.nn.relu(hidden_layer)
 
output = tf.add(tf.matmul(hidden_layer, output_weights), output_biases)
cs

 - 위 코드는 ReLU 함수를 hidden_layer에 적용하여 ReLU함수의 특성상

 - 음의 weights 에 대해선 0의 값을 리턴하므로 on / off 스위치처럼 작동하게끔 해 준다.

 - activation function 이후로 레이어를 추가로 더 더해주면 모델이 비선형으로 바뀌게된다.

 - 이렇게 비선형 모델을 만들게 되면 네트워크가 더 복잡한 문제를 해결할 때 효과적이다.






Quiz

 - ReLU 함수를 사용하여 선형 단일 네트워크(linear single layer network)를 

 - 비선형 다층 네트워크로 변환해보자.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import tensorflow as tf
 
output = None
hidden_layer_weights = [
    [0.10.20.4],
    [0.40.60.6],
    [0.50.90.1],
    [0.80.20.8]]
out_weights = [
    [0.10.6],
    [0.20.1],
    [0.70.9]]
 
# Weights and biases
weights = [
    tf.Variable(hidden_layer_weights),
    tf.Variable(out_weights)]
biases = [
    tf.Variable(tf.zeros(3)),
    tf.Variable(tf.zeros(2))]
 
# Input
features = tf.Variable([[1.02.03.04.0], [-1.0-2.0-3.0-4.0], [11.012.013.014.0]])
 
# TODO: Create Model
hidden_layer = tf.add(tf.matmul(features, weights[0]), biases[0])
hidden_layer = tf.nn.relu(hidden_layer)
logits = tf.add(tf.matmul(hidden_layer, weights[1]), biases[1])
 
# TODO: Print session results
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(logits))
cs


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함