티스토리 뷰

Linear functions in TensorFlow

 - neural network를 설계함에있어 가장 흔히 사용되는 연산은 

 - input, weight 그리고 bias의 linear function을 계산하는것이다.

 - 선형 연산의 output은 다음과 같이 쓸 수 있다.

 - W 는 두 레이어를 연결해주는 가중치 행렬

 - y 는 output, x 는 input, b는 바이어스 벡터이다.




Weights and Bias in TensorFlow

 - 신경망을 훈련시키는 목표는 label을 가장 잘 예측할 수 있도록 가중치와 바이어스를 수정하는것이다.

 - 가중치와 바이어스를 사용하려면 수정 가능한 텐서가 필요하다.

 - tf.placeholder(), tf.constant() 이 두 텐서는 수정될 수 없으므로 적절하지 않다.

 - 따라서 tf.Variable 클래스를 사용해야한다.

 - tf.Variable 클래스는 일반 파이썬 변수와 마찬가지로 수정할 수 있는 초기값을 가진 텐서를 만든다.

 - 이 텐서는 세션(Session)에서 상태를 저장하므로 텐서의 상태를 수동으로 초기화해야한다.

 - tf.global_variables_initializer() 함수를 사용하여 모든 가변텐서의 상태를 초기화 할 수 있다.

 - tf.global_variables_initializer() 함수의 호출은 모든 텐서플로우 변수를 그래프에서 초기화하는 연산을 반환한다.

 - 위에서 알 수 있듯, 이 함수는 세션을 사용하여 모든 변수를 초기화 함

 - tf.Variable 클래스를 사용하면 가중치와 바이어스를 변경할 수 있지만, 초기값을 지정해줘야한다.




 - 정규 분포에서 랜덤 넘버를 뽑아내서 가중치를 초기화 하는것이 좋다.

 - 가중치를 무작위로 추출하면 모델을 훈련할 때 마다 같은 장소에 갇히지 않게 됨(stuck in the same place)

 - gradient descent를 다룰 때 더 자세하게 알아보도록 하자

 - 마찬가지로 정규분포에서 가중치를 선택하면 하나의 가중치가 다른 가중치를 압도(overwhelming)하지 않게 된다.

 - tf.truncated_normal() 함수를 사용하여 정규분포에서 난수를 생성할 수 있다.




tf.truncated_normal()

 - tf.truncated_normal() 함수는 평균으로부터의 표준편차가 2보다 크지 않은 랜덤 밸류의 텐서를 리턴한다.

 - 가중치가 이미 네트워크가 갇히는 상황(stuck)에 놓이는 것을 방지하기 때문에,

 - 바이어스를 무작위로 추출할 필요가 없다.




tf. zeros()

 - tf.zero() 함수는 0인 텐서를 리턴한다.






Linear Classifier Quiz

 - 텐서플로우를 사용해서 MNIST 데이터셋의 손으로 쓴 글자 0, 1, 2를 분류해보도록 하자.

 - 손글씨로 쓰여있어서 각도도 다르고 모양도 다른 글자를 serif 체와 비교하는 모델을 만들것인데,

 - 유사성과 차이점(similarities and differences)은 모델의 가중치를 형성하는 데에 중요한 역할을 하게 된다.




 - 위의 이미지는 각 라벨(0, 1, 2)에 대한 훈련된 가중치이다.

 - 각 가중치들은 훈련을 통해 찾은 각 자리수(0, 1, 2)의 고유한 특성을 표시해준다.




Instructions

 - quiz.py의 get_weights가 가중치의 tf.Variable를 리턴하도록 구현

 - quiz.py의 get_biases가 바이어스의 tf.Variable를 리턴하도록 구현

 - quiz.py의 xW + b 의 linear function을 구현

 - sandbox.py의 모든 가중치를 초기화

 - xW + b의 xW는 행렬 곱셈이므로 tf.matmul() 함수를 사용해야함

 - 행렬의 곱셈에선 순서가 중요하므로(교환법칙이 성립하지 않으므로) 

 - tf.matmul(a, b)는 tf.matmul(b, a)와 같지 않다는것에 주의해야함






<sandbox.py>

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import tensorflow as tf
# Sandbox Solution
# Note: You can't run code in this tab
from tensorflow.examples.tutorials.mnist import input_data
from quiz import get_weights, get_biases, linear
 
 
def mnist_features_labels(n_labels):
    """
    Gets the first <n> labels from the MNIST dataset
    :param n_labels: Number of labels to use
    :return: Tuple of feature list and label list
    """
    mnist_features = []
    mnist_labels = []
 
    mnist = input_data.read_data_sets('/datasets/ud730/mnist', one_hot=True)
 
    # In order to make quizzes run faster, we're only looking at 10000 images
    for mnist_feature, mnist_label in zip(*mnist.train.next_batch(10000)):
 
        # Add features and labels if it's for the first <n>th labels
        if mnist_label[:n_labels].any():
            mnist_features.append(mnist_feature)
            mnist_labels.append(mnist_label[:n_labels])
 
    return mnist_features, mnist_labels
 
 
# Number of features (28*28 image is 784 features)
n_features = 784
# Number of labels
n_labels = 3
 
# Features and Labels
features = tf.placeholder(tf.float32)
labels = tf.placeholder(tf.float32)
 
# Weights and Biases
= get_weights(n_features, n_labels)
= get_biases(n_labels)
 
# Linear Function xW + b
logits = linear(features, w, b)
 
# Training data
train_features, train_labels = mnist_features_labels(n_labels)
 
with tf.Session() as session:
    session.run(tf.global_variables_initializer())
 
    # Softmax
    prediction = tf.nn.softmax(logits)
 
    # Cross entropy
    # This quantifies how far off the predictions were.
    # You'll learn more about this in future lessons.
    cross_entropy = -tf.reduce_sum(labels * tf.log(prediction), reduction_indices=1)
 
    # Training loss
    # You'll learn more about this in future lessons.
    loss = tf.reduce_mean(cross_entropy)
 
    # Rate at which the weights are changed
    # You'll learn more about this in future lessons.
    learning_rate = 0.08
 
    # Gradient Descent
    # This is the method used to train the model
    # You'll learn more about this in future lessons.
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
 
    # Run optimizer and get loss
    _, l = session.run(
        [optimizer, loss],
        feed_dict={features: train_features, labels: train_labels})
 
# Print loss
print('Loss: {}'.format(l))
 
cs






<quiz.py>

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
34
35
# Quiz Solution
# Note: You can't run code in this tab
import tensorflow as tf
 
def get_weights(n_features, n_labels):
    """
    Return TensorFlow weights
    :param n_features: Number of features
    :param n_labels: Number of labels
    :return: TensorFlow weights
    """
    # TODO: Return weights
    return tf.Variable(tf.truncated_normal((n_features, n_labels)))
 
 
def get_biases(n_labels):
    """
    Return TensorFlow bias
    :param n_labels: Number of labels
    :return: TensorFlow bias
    """
    # TODO: Return biases
    return tf.Variable(tf.zeros(n_labels))
 
 
def linear(input, w, b):
    """
    Return linear function in TensorFlow
    :param input: TensorFlow input
    :param w: TensorFlow weights
    :param b: TensorFlow biases
    :return: TensorFlow linear function
    """
    # TODO: Linear Function (xW + b)
    return tf.add(tf.matmul(input, w), b)
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
글 보관함