Deep Learning2019. 12. 12. 19:22
반응형

 김성훈 교수님의 [모두를 위한 딥러닝] 강의 정리

 - https://www.youtube.com/watch?reload=9&v=BS6O0zOGX4E&feature=youtu.be&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm&fbclid=IwAR07UnOxQEOxSKkH6bQ8PzYj2vDop_J0Pbzkg3IVQeQ_zTKcXdNOwaSf_k0

 - 참고자료 : Andrew Ng's ML class

  1) https://class.coursera.org/ml-003/lecture

  2) http://holehouse.org/mlclass/ (note)

 

1. Learning rate

 - 어느 정도의 크기로 기울기가 줄어드는 지점으로 이동하겠는가를 나타내는 지표

 - cost function을 최소화시키기 위해 사용하는 'Gradient descent algorithm'에서 cost 값의 미분한 값 앞 알파값

  c.f) Gradient descent algorithm : 기울기 크기가 줄어드는 쪽으로 가면 cost function이 최소가 되는 지점을 찾아간다.

 - overshooting : learning rate의 값이 너무 커서 최소값에 도달하지 않고 오히려 그래프를 벗어나는 경우

 - small learning rate : step의 간격이 매우 작아서 학습 속도가 매우 느리게 됨

 - cost 값을 확인하여, 적절한 learning rate을 정하는 것이 중요함 : data와 환경에 따라 다름

 

2. Preprocessing for gradient descent algorithm (전처리, 선처리)

 - 두 개의 입력 값(x1, x2)이 적절한 범위 차이를 갖는 경우 : 

 - 두 개의 입력 값(x1, x2)이 매우 큰 차이를 갖는 경우 :

 

 - Preprocessing 필요 

 

 - 여러 normalization (표준화) 중 standardization

 

3. Overfitting

 - 학습 데이터에 딱 맞는 모델을 만드는 경우, 다른 실제 데이터와는 안 맞는 경우가 생김

 - 학습 데이터(training data)가 많을 수록, feature(입력으로 들어오는 변수)의 개수를 줄일 수록, Regularization(일반화)할 수록 Overfitting을 방지할 수 있음

 

 - Regularization : weight에 너무 큰 가중치를 두지 말 것. weight의 값이 커지면 그래프 형태가 구부러진 형태가 되고, weight의 크기가 작으면 그래프 형태가 선형을 이룬다. 이를 위해 cost function 뒤에 아래와 같은 식을 추가한다.

 - Regularization의 Python 구현 : cost에 다음 변수 l2reg을 더함. cost 함수가 틀렸을 때 높은 비용이 발생할 수 있도록 벌점(penalty)을 부과하는 것처럼 W에 대한 값이 클 경우에 penalty를 부여함. λ 값을 사용하여 얼마나 penalty를 부여할 것인지 결정함

                                           l2reg = 0.001 * tf.reduce_sum(tf.square(W))

 

반응형
Posted by CCIBOMB
Deep Learning2019. 12. 11. 19:16
반응형

 김성훈 교수님의 [모두를 위한 딥러닝] 강의 정리

 - https://www.youtube.com/watch?reload=9&v=BS6O0zOGX4E&feature=youtu.be&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm&fbclid=IwAR07UnOxQEOxSKkH6bQ8PzYj2vDop_J0Pbzkg3IVQeQ_zTKcXdNOwaSf_k0

 - 참고자료 : Andrew Ng's ML class

  1) https://class.coursera.org/ml-003/lecture

  2) http://holehouse.org/mlclass/ (note)



1.  Logistic regression (= binary classfication)

 

2. Multinomial classification

 

3. Softmax classification

 - softmax : ① sigmoid와 마찬가지로 0과 1사이의 값으로 변환, ② 변환된 결과에 대한 합계가 1이 되도록 해줌(≒ 확률)
                (tensorflow의 softmax 함수 이용)

 

 - one-hot encoding : softmax로 구한 값 중에서 가장 큰 값을 1로, 나머지를 0으로 만듦
                             (tensorflow의 argmax 함수 이용)

 

4. Cost function (=cross-entropy)

 - S(Y) = sotmax가 예측한 값

 - L = 실제 Y의 값

 - Cost function = (sotmax가 예측한 값)과 (실제 Y의 값)의 차이를 계산 = Distance(S, L)

 

 - Loss(=cost=error) = D(S,L)의 평균

 

 - Gradient descent 알고리즘 = 미분을 통해 최소비용 찾기

 

5. Logistic cost VS cross entropy

 - Logistic regression의 cost 함수 = Multinomial classification의 cross-entropy cost 함수

 

6. TensorFlow로 Softmax Classification의 구현

 - hypothesis = tf.nn.softmax(tf.matmul(X,W)+b)

 - cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(hypothesis),axis=1))

 - optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)

 

 6-1. code 구현

import tensorflow as tf
tf.set_random_seed(777) # for reproducibility
 
x_data = [[1, 2, 1, 1],
[2, 1, 3, 2],
[3, 1, 3, 4],
[4, 1, 5, 5],
[1, 7, 5, 5],
[1, 2, 5, 6],
[1, 6, 6, 6],
[1, 7, 7, 7]]
y_data = [[0, 0, 1],
[0, 0, 1],
[0, 0, 1],
[0, 1, 0],
[0, 1, 0],
[0, 1, 0],
[1, 0, 0],
[1, 0, 0]]
 
X = tf.placeholder("float", [None, 4])     # x_data와 같은 크기의 열 가짐. 행 크기는 모름.
Y = tf.placeholder("float", [None, 3])
nb_classes = 3
 
W = tf.Variable(tf.random_normal([4, nb_classes]), name='weight')
b = tf.Variable(tf.random_normal([nb_classes]), name='bias')
 
# tf.nn.softmax computes softmax activations
# softmax = exp(logits) / reduce_sum(exp(logits), dim)
hypothesis = tf.nn.softmax(tf.matmul(X, W) + b)
 
# Cross entropy cost/loss
cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1))
 
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
 
# Launch graph
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
 
for step in range(2001):
_, cost_val = sess.run([optimizer, cost], feed_dict={X: x_data, Y: y_data})
 
if step % 200 == 0:
print(step, cost_val)
 
print('--------------')
# Testing & One-hot encoding
a = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9]]})
print(a, sess.run(tf.argmax(a, 1)))
 
print('--------------')
b = sess.run(hypothesis, feed_dict={X: [[1, 3, 4, 3]]})
print(b, sess.run(tf.argmax(b, 1)))
 
print('--------------')
c = sess.run(hypothesis, feed_dict={X: [[1, 1, 0, 1]]})
print(c, sess.run(tf.argmax(c, 1)))
 
print('--------------')
all = sess.run(hypothesis, feed_dict={X: [[1, 11, 7, 9], [1, 3, 4, 3], [1, 1, 0, 1]]})
print(all, sess.run(tf.argmax(all, 1)))

 

 6-2. 결과값

(0, 6.926112)
(200, 0.60050154)
(400, 0.47295803)
(600, 0.37342972)
(800, 0.2801836)
(1000, 0.23280506)
(1200, 0.21065345)
(1400, 0.19229901)
(1600, 0.17682335)
(1800, 0.16359548)
(2000, 0.15216163)
--------------
a = (array([[1.3890485e-03, 9.9860197e-01, 9.0612575e-06]], dtype=float32), array([1]))
--------------
b = (array([[0.9311919 , 0.06290215, 0.00590591]], dtype=float32), array([0]))
--------------
c = (array([[1.2732816e-08, 3.3411387e-04, 9.9966586e-01]], dtype=float32), array([2]))
--------------
all = (array([[1.3890457e-03, 9.9860197e-01, 9.0612402e-06],
       [9.3119192e-01, 6.2902153e-02, 5.9059057e-03],
       [1.2732816e-08, 3.3411387e-04, 9.9966586e-01]], dtype=float32), array([1, 0, 2]))

 

7. TensorFlow로 Fancy Softmax Classifier 구현 (cross_entropy, one_hot, reshape)

 7-1. softmax_cross_entropy_with_logits

 - logits(=score) = tf.matmul(X, W) + b

 - hypothesis = tf.nn.softmax(logits)

 

 7-2. tensorflow 구현 (tensorflow 내장함수 이용)

# Cross entropy cost/loss

   cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(hypothesis), axis=1)

# Cross entropy cost/loss

   cost_i = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y_one_hot)

   cost = tf.reduce_mean(cost_i)

 

 7-3. 예제 : 동물 분류(Animal classification)

 - tf.one_hot : one_hot을 사용하게 되면 하나의 차원의 수를 높여준다. 예를들어 [0, 3]의 행렬을 [[1000000], [0001000]]의 식으로 만들어 주게 된다.

 - tf.reshape : 늘어난 차원의 수를 다시 줄이기 위해, reshape 함수를 이용한다.

 

 - zoo.data : https://archive.ics.uci.edu/ml/machine-learning-databases/zoo/zoo.data

불러오는 중입니다...

 

 - 전체 소스코드

import tensorflow as tf
import numpy as np
tf.set_random_seed(777) # for reproducibility
 
# Predicting animal type based on various features
xy = np.loadtxt('data-04-zoo.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
 
print(x_data.shape, y_data.shape)
 
'''
(101, 16) (101, 1)
'''
 
nb_classes = 7 # 0 ~ 6
 
X = tf.placeholder(tf.float32, [None, 16]) # x_data(동물 형질 항목)의 개수
Y = tf.placeholder(tf.int32, [None, 1]) # 0 ~ 6
 
Y_one_hot = tf.one_hot(Y, nb_classes) # one hot, 차원의 수가 1차원 증가함
print("one_hot:", Y_one_hot)
Y_one_hot = tf.reshape(Y_one_hot, [-1, nb_classes])
print("reshape one_hot:", Y_one_hot)
 
'''
one_hot: Tensor("one_hot:0", shape=(?, 1, 7), dtype=float32)
reshape one_hot: Tensor("Reshape:0", shape=(?, 7), dtype=float32)
'''
 
W = tf.Variable(tf.random_normal([16, nb_classes]), name='weight')
b = tf.Variable(tf.random_normal([nb_classes]), name='bias')
 
# tf.nn.softmax computes softmax activations
# softmax = exp(logits) / reduce_sum(exp(logits), dim)
logits = tf.matmul(X, W) + b
hypothesis = tf.nn.softmax(logits)
 
# Cross entropy cost/loss
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits,
labels=tf.stop_gradient([Y_one_hot])))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
 
prediction = tf.argmax(hypothesis, 1)  # probability -> 0~6
correct_prediction = tf.equal(prediction, tf.argmax(Y_one_hot, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
 
# Launch graph (학습)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
 
for step in range(2001):
_, cost_val, acc_val = sess.run([optimizer, cost, accuracy], feed_dict={X: x_data, Y: y_data})
 
if step % 100 == 0:
print("Step: {:5}\tCost: {:.3f}\tAcc: {:.2%}".format(step, cost_val, acc_val))
 
# Let's see if we can predict
pred = sess.run(prediction, feed_dict={X: x_data})
# y_data: (N,1) = flatten => (N, ) matches pred.shape
for p, y in zip(pred, y_data.flatten()):
print("[{}] Prediction: {} True Y: {}".format(p == int(y), p, int(y)))

 

 - 결과값

Step:     0 Cost: 5.480 Acc: 37.62%
Step:   100 Cost: 0.806 Acc: 79.21%
Step:   200 Cost: 0.488 Acc: 88.12%
Step:   300 Cost: 0.350 Acc: 90.10%
Step:   400 Cost: 0.272 Acc: 94.06%
Step:   500 Cost: 0.222 Acc: 95.05%
Step:   600 Cost: 0.187 Acc: 97.03%
Step:   700 Cost: 0.161 Acc: 97.03%
Step:   800 Cost: 0.141 Acc: 97.03%
Step:   900 Cost: 0.124 Acc: 97.03%
Step:  1000 Cost: 0.111 Acc: 97.03%
Step:  1100 Cost: 0.101 Acc: 99.01%
Step:  1200 Cost: 0.092 Acc: 100.00%
Step:  1300 Cost: 0.084 Acc: 100.00%
Step:  1400 Cost: 0.078 Acc: 100.00%
Step:  1500 Cost: 0.072 Acc: 100.00%
Step:  1600 Cost: 0.068 Acc: 100.00%
Step:  1700 Cost: 0.064 Acc: 100.00%
Step:  1800 Cost: 0.060 Acc: 100.00%
Step:  1900 Cost: 0.057 Acc: 100.00%
Step:  2000 Cost: 0.054 Acc: 100.00%
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 3 True Y: 3
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 3 True Y: 3
[True] Prediction: 6 True Y: 6
[True] Prediction: 6 True Y: 6
[True] Prediction: 6 True Y: 6
[True] Prediction: 1 True Y: 1
[True] Prediction: 0 True Y: 0
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 1 True Y: 1
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 5 True Y: 5
[True] Prediction: 4 True Y: 4
[True] Prediction: 4 True Y: 4
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 5 True Y: 5
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 3 True Y: 3
[True] Prediction: 5 True Y: 5
[True] Prediction: 5 True Y: 5
[True] Prediction: 1 True Y: 1
[True] Prediction: 5 True Y: 5
[True] Prediction: 1 True Y: 1
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 6 True Y: 6
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 5 True Y: 5
[True] Prediction: 4 True Y: 4
[True] Prediction: 6 True Y: 6
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 1 True Y: 1
[True] Prediction: 1 True Y: 1
[True] Prediction: 1 True Y: 1
[True] Prediction: 3 True Y: 3
[True] Prediction: 3 True Y: 3
[True] Prediction: 2 True Y: 2
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 6 True Y: 6
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 2 True Y: 2
[True] Prediction: 6 True Y: 6
[True] Prediction: 1 True Y: 1
[True] Prediction: 1 True Y: 1
[True] Prediction: 2 True Y: 2
[True] Prediction: 6 True Y: 6
[True] Prediction: 3 True Y: 3
[True] Prediction: 1 True Y: 1
[True] Prediction: 0 True Y: 0
[True] Prediction: 6 True Y: 6
[True] Prediction: 3 True Y: 3
[True] Prediction: 1 True Y: 1
[True] Prediction: 5 True Y: 5
[True] Prediction: 4 True Y: 4
[True] Prediction: 2 True Y: 2
[True] Prediction: 2 True Y: 2
[True] Prediction: 3 True Y: 3
[True] Prediction: 0 True Y: 0
[True] Prediction: 0 True Y: 0
[True] Prediction: 1 True Y: 1
[True] Prediction: 0 True Y: 0
[True] Prediction: 5 True Y: 5
[True] Prediction: 0 True Y: 0
[True] Prediction: 6 True Y: 6
[True] Prediction: 1 True Y: 1

반응형
Posted by CCIBOMB
Deep Learning2019. 12. 4. 20:08
반응형

 김성훈 교수님의 [모두를 위한 딥러닝] 강의 정리

 - https://www.youtube.com/watch?reload=9&v=BS6O0zOGX4E&feature=youtu.be&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm&fbclid=IwAR07UnOxQEOxSKkH6bQ8PzYj2vDop_J0Pbzkg3IVQeQ_zTKcXdNOwaSf_k0

 - 참고자료 : Andrew Ng's ML class

  1) https://class.coursera.org/ml-003/lecture

  2) http://holehouse.org/mlclass/ (note)



1. Linear Regression

 

2. (binary) classification -> 0, 1 encoding

 - Spam E-mail Detection: Spam(0) or Ham(1)

 - Facebook feed: show(0) or hide(1)

 - Credit Card Fraudulent Transaction detection: legitimate(0) or fraud(1)

 

3. Logistic Hypothesis

 

4. Logistic Regression의 새로운 cost 함수

 

5. cost 함수의 최소화 - Gradient decent algorithm

  -> tensorflow

# cost function

cost = -tf.reduce_mean(-tf.reduce_sum(Y*tf.log(hypothesis) + (1-Y)*tf.log(1-hypothesis)))

 

# Minimize

a = tf.Variable(0.1) # Learnign rate, alpha

optimizer = tf.train.GradientDescentOptimizer(a)

train = optimizer.miminize(cost)

 

6. Tensorflow로 Logistic (regression) classifier 구현하기

 (1) Training Data

x_data = [[1, 2], [2, 3], [3, 1], [4, 3], [5, 3],[6, 2]]
y_data = [[0], [0], [0], [1], [1], [1]]
 
# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, 2])
Y = tf.placeholder(tf.float32, shape=[None, 1])

 

 (2) tensorflow로 hypothesis 구현

W = tf.Variable(tf.random_normal([2, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
 
# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)

 

 (3) tensorflow로 cost/loss function 구현

# cost function

cost = -tf.reduce_mean(-tf.reduce_sum(Y*tf.log(hypothesis) + (1-Y)*tf.log(1-hypothesis)))

 

 (4) tensorflow로 cost 최소화 구현

# Minimize

a = tf.Variable(0.01) # Learnign rate, alpha

optimizer = tf.train.GradientDescentOptimizer(a)

train = optimizer.miminize(cost)

 

 (5) 예측 정확도 계산

# Accuracy computation
# True if hypothesis>0.5 else False
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

 

 (6) Training the model

# Launch graph
with tf.Session() as sess:
# Initialize TensorFlow variables
sess.run(tf.global_variables_initializer())
 
for step in range(10001):
cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})
if step % 200 == 0:
print(step, cost_val)

 

 (7) 예측 정확도 출력

# Accuracy report
h, c, a = sess.run([hypothesis, predicted, accuracy],
feed_dict={X: x_data, Y: y_data})
print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)

 

 (8) 실행결과 : 정확도 100% !!

 

7. 실제 데이터로 테스트

 (1) diabetes.csv

https://github.com/hunkim/DeepLearningZeroToAll/blob/master/data-03-diabetes.csv

 

hunkim/DeepLearningZeroToAll

TensorFlow Basic Tutorial Labs. Contribute to hunkim/DeepLearningZeroToAll development by creating an account on GitHub.

github.com

 

 (2) tensorflow 구현

# Lab 5 Logistic Regression Classifier
import tensorflow as tf
import numpy as np
tf.set_random_seed(777) # for reproducibility
 
xy = np.loadtxt('data-03-diabetes.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
 
print(x_data.shape, y_data.shape)
 
# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, 8])
Y = tf.placeholder(tf.float32, shape=[None, 1])
 
W = tf.Variable(tf.random_normal([8, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
 
# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(-tf.matmul(X, W)))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
 
# cost/loss function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
tf.log(1 - hypothesis))
 
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
 
# Accuracy computation
# True if hypothesis>0.5 else False
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
 
# Launch graph
with tf.Session() as sess:
# Initialize TensorFlow variables
sess.run(tf.global_variables_initializer())
 
for step in range(10001):
cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})
if step % 200 == 0:
print(step, cost_val)
 
# Accuracy report
h, c, a = sess.run([hypothesis, predicted, accuracy],
feed_dict={X: x_data, Y: y_data})
print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)

 

 (3) 실행결과

step, cost :

(0, 0.82793975)
(200, 0.75518084)
(400, 0.7263554)
(600, 0.70517904)
(800, 0.6866306)
(1000, 0.669853)

...

(9000, 0.49424884)
(9200, 0.49348038)
(9400, 0.49275032)
(9600, 0.49205625)
(9800, 0.49139577)
(10000, 0.4907668)

 

hypothesis :

[0.4434849 ],
[0.9153646 ],
[0.22591159],
[0.93583125],
[0.3376363 ],
[0.70926887],
[0.94409144],

...

 

correct(Y) :

[0.],
[1.],
[0.],
[1.],
[0.],
[1.],
[1.],

...

 

Accuracy :

0.7628459

반응형
Posted by CCIBOMB
Deep Learning2019. 12. 2. 22:04
반응형

 김성훈 교수님의 [모두를 위한 딥러닝] 강의 정리

 - https://www.youtube.com/watch?reload=9&v=BS6O0zOGX4E&feature=youtu.be&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm&fbclid=IwAR07UnOxQEOxSKkH6bQ8PzYj2vDop_J0Pbzkg3IVQeQ_zTKcXdNOwaSf_k0

 - 참고자료 : Andrew Ng's ML class

  1) https://class.coursera.org/ml-003/lecture

  2) http://holehouse.org/mlclass/ (note)

 



1. Loading data from file

import tensorflow as tf
import numpy as np
tf.set_random_seed(777) # for reproducibility
 
xy = np.loadtxt('data-01-test-score.csv', delimiter=',', dtype=np.float32)
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
 
# Make sure the shape and data are OK
print(x_data, "\nx_data shape:", x_data.shape)
print(y_data, "\ny_data shape:", y_data.shape)
# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, 3])
Y = tf.placeholder(tf.float32, shape=[None, 1])
 
W = tf.Variable(tf.random_normal([3, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
 
# Hypothesis
hypothesis = tf.matmul(X, W) + b
 
# Simplified cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
 
# Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
train = optimizer.minimize(cost)
 
# Launch the graph in a session.
sess = tf.Session()
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer())
 
for step in range(2001):
cost_val, hy_val, _ = sess.run([cost, hypothesis, train],
feed_dict={X: x_data, Y: y_data})
if step % 10 == 0:
print(step, "Cost:", cost_val, "\nPrediction:\n", hy_val)

1950, Cost: 2.8077145

Prediction:

array([[154.30186],
       [183.31505],
       [181.97646],
       [194.59978],
       [142.33385],
       [ 99.34767]], dtype=float32))

1960, Cost: 2.7977974

Prediction:

array([[154.296  ],
       [183.31776],
       [181.97401],
       [194.59859],
       [142.33716],
       [ 99.35353]], dtype=float32))

1970, Cost: 2.787885

Prediction:

array([[154.29016],
       [183.32051],
       [181.97154],
       [194.5974 ],
       [142.34042],
       [ 99.35938]], dtype=float32))

1980, Cost: 2.778064

Prediction:

array([[154.28435],
       [183.32324],
       [181.9691 ],
       [194.59624],
       [142.3437 ],
       [ 99.3652 ]], dtype=float32))

1990, Cost: 2.7683241

Prediction:

array([[154.27856],
       [183.32594],
       [181.96667],
       [194.59506],
       [142.34695],
       [ 99.37102]], dtype=float32))

2000, Cost: 2.7586195

Prediction:

array([[154.27278 ],
       [183.32866 ],
       [181.96426 ],
       [194.5939  ],
       [142.35019 ],
       [ 99.376816]], dtype=float32))

 

2. Loading data from multi-file

import tensorflow as tf
tf.set_random_seed(777) # for reproducibility
 
filename_queue = tf.train.string_input_producer(
['data-01-test-score.csv'], shuffle=False, name='filename_queue')
 
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
 
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[0.], [0.], [0.], [0.]]
xy = tf.decode_csv(value, record_defaults=record_defaults)
 
# collect batches of csv in
train_x_batch, train_y_batch = \
tf.train.batch([xy[0:-1], xy[-1:]], batch_size=10)
 
# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, 3])
Y = tf.placeholder(tf.float32, shape=[None, 1])
 
W = tf.Variable(tf.random_normal([3, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
 
# Hypothesis
hypothesis = tf.matmul(X, W) + b
 
# Simplified cost/loss function
cost = tf.reduce_mean(tf.square(hypothesis - Y))
 
# Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
train = optimizer.minimize(cost)
 
# Launch the graph in a session.
sess = tf.Session()
# Initializes global variables in the graph.
sess.run(tf.global_variables_initializer())
 
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
 
for step in range(2001):
x_batch, y_batch = sess.run([train_x_batch, train_y_batch])
cost_val, hy_val, _ = sess.run(
[cost, hypothesis, train], feed_dict={X: x_batch, Y: y_batch})
if step % 10 == 0:
print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)
 
coord.request_stop()
coord.join(threads)
 
# Ask my score
print("Your score will be ",
sess.run(hypothesis, feed_dict={X: [[100, 70, 101]]}))
 
print("Other scores will be ",
sess.run(hypothesis, feed_dict={X: [[60, 70, 110], [90, 100, 80]]}))

1980, Cost: 2.2382462

Prediction:

array([[152.35132],
       [183.37514],
       [180.53424],
       [197.20535],
       [139.35315],
       [103.52445],
       [152.35132],
       [183.37514],
       [180.53424],
       [197.20535]], dtype=float32))

1990, Cost: 3.407795

Prediction:

array([[139.34067],
       [103.51443],
       [152.33455],
       [183.35727],
       [180.5155 ],
       [197.18425],
       [139.34067],
       [103.51443],
       [152.33455],
       [183.35727]], dtype=float32))

2000, Cost: 3.3214183

Prediction:

array([[180.62273],
       [197.30028],
       [139.42564],
       [103.57615],
       [152.42416],
       [183.46718],
       [180.62273],
       [197.30028],
       [139.42564],
       [103.57615]], dtype=float32))

'Your score will be ', array([[182.8681]], dtype=float32))
'Other scores will be ', array([[169.80573], [177.92252]], dtype=float32))

반응형
Posted by CCIBOMB
Deep Learning2019. 11. 19. 23:24
반응형

김성훈 교수님의 [모두를 위한 딥러닝] 강의 정리

 - https://www.youtube.com/watch?reload=9&v=BS6O0zOGX4E&feature=youtu.be&list=PLlMkM4tgfjnLSOjrEJN31gZATbcj_MpUm&fbclid=IwAR07UnOxQEOxSKkH6bQ8PzYj2vDop_J0Pbzkg3IVQeQ_zTKcXdNOwaSf_k0

 - 참고자료 : Andrew Ng's ML class

  1) https://class.coursera.org/ml-003/lecture

 

Coursera

 

class.coursera.org

  2) http://holehouse.org/mlclass/ (note)

 

Machine Learning - complete course notes

Stanford Machine Learning The following notes represent a complete, stand alone interpretation of Stanford's machine learning course presented by Professor Andrew Ng and originally posted on the ml-class.org website during the fall 2011 semester. The topic

holehouse.org

 

 

1. Tensorflow

 - '데이터 플로우 그래프(data flow graphs)'를 이용한 '수치 계산(numerical computation)'을 위한 오픈소스 라이브러리

 

2. Tensorflow 설치 : pip install --upgrade tensorflow

 

3. Tensorflow 버전확인 : tensorflow.__version__

 

4. Deep Learning 관련 소스코드

 - https://github.com/hunkim/DeepLearningZeroToAll/   (2017년경 다수 commit, 최근에는 활동이 적음)

 

Build software better, together

GitHub is where people build software. More than 40 million people use GitHub to discover, fork, and contribute to over 100 million projects.

github.com

 

5. Hello, Tensorflow

 - Input : 

# Create a constant op

# This op is added as a node to the default graph // "Hello, TensorFlow!"라는 노드 생성

hello = tf.constant("Hello, TensorFlow!")

 

# start a TF session // 다른 프로그래밍 언어와 달리, 명령어 실행을 하려면 Session 생성이 필요

sess = tf.Session()

 

# run the op and get result

print(sess.run(hello))

 

 - Output : 

Hello, TensorFlow!

 

6. 계산 그래프

 - Input : 

# 노드 1, 2, 3 생성

node1 = tf.constant(3.0, tf.float32)

node2 = tf.constant(4.0) // tf.float32 생략

node3 = tf.add(node1, node2)

 

# 계산결과 출력

sess = tf.Session()

print("sess.run(node1, node2): ", sess.run([node1, node2]))

print("sess.run(node3): ", sess.run(node3))

 

 - Output : 

 

7. TensorFlow 메커니즘

 (1) 그래프 생성

 (2) 세션 실행 : 데이터 입력 및 그래프 실행

 (3) 그래프 업데이트 및 결과값 반환

※ 그림출처 : mathwarehouse.com

 

8. Placeholder // 처음에는 값이 없지만 입력 값을 받을 수 있는 노드

 - Input : 

a = tf.placeholder(tf.float32)

b = tf.placeholder(tf.float32)

adder_node = a + b

 

print(sess.run(adder_node, feed_dict={a: 3, b: 4.5}))

print(sess.run(adder_node, feed_dict={a: [1,3], b: [2, 4]}))

 

 - Output : 

반응형
Posted by CCIBOMB