Basic Neural Network(NN)
- 실행코드
import tensorflow as tf import numpy as np import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' x_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32) y_data = np.array([[0], [1], [1], [0]], dtype=np.float32) X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) W1 = tf.Variable(tf.random_normal([2, 2]), name='weight1') b1 = tf.Variable(tf.random_normal([2]), name='bias1') layer1 = tf.sigmoid(tf.matmul(X, W1) + b1) W2 = tf.Variable(tf.random_normal([2, 1]), name='weight2') b2 = tf.Variable(tf.random_normal([1]), name='bias2') hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2) # cost/loss function cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis)) train = tf.train.GradientDescentOptimizer(learning_rate=0.1).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): sess.run(train, feed_dict={X: x_data, Y: y_data}) if step % 5000 == 0: print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run([W1, W2])) # Accuracy report h, c, a = sess.run([hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data}) print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)
- 실행 결과
Basic NN vs Wide NN
- Wide 코드 변경 부분
W1 = tf.Variable(tf.random_normal([2, 10]), name='weight1') b1 = tf.Variable(tf.random_normal([10]), name='bias1') layer1 = tf.sigmoid(tf.matmul(X, W1) + b1) W2 = tf.Variable(tf.random_normal([10, 1]), name='weight2') b2 = tf.Variable(tf.random_normal([1]), name='bias2') hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)
Basic NN vs WideNN vs Deep NN
- Deep 코드 변경 부분
W1 = tf.Variable(tf.random_normal([2, 10]), name='weight1') b1 = tf.Variable(tf.random_normal([10]), name='bias1') layer1 = tf.sigmoid(tf.matmul(X, W1) + b1) W2 = tf.Variable(tf.random_normal([10, 10]), name='weight2') b2 = tf.Variable(tf.random_normal([10]), name='bias2') layer2 = tf.sigmoid(tf.matmul(layer1, W2) + b2) W3 = tf.Variable(tf.random_normal([10, 10]), name='weight3') b3 = tf.Variable(tf.random_normal([10]), name='bias3') layer3 = tf.sigmoid(tf.matmul(layer2, W3) + b3) W4 = tf.Variable(tf.random_normal([10, 1]), name='weight4') b4 = tf.Variable(tf.random_normal([1]), name='bias4') hypothesis = tf.sigmoid(tf.matmul(layer3, W4) + b4)
- 실행 결과 비교
-> Depp Network의 성능이 우수하다.
- 이런 이유로 NN이 Deep Learning으로 불려오게 되었다.
'IT > 머신러닝' 카테고리의 다른 글
[section_9] ReLU and 초기값 정하기(1) (2006/2007 breakthrough) (0) | 2018.06.01 |
---|---|
[section_9_lab] TensorBoard 사용하기 (0) | 2018.06.01 |
[section_9] Neural Network 1: XOR 문제와 학습방법, Backpropagation(2) (0) | 2018.06.01 |
[section_9] Neural Network 1: XOR 문제와 학습방법, Backpropagation(1) (0) | 2018.06.01 |
[section_8_lab] Tensor Manipulation (0) | 2018.06.01 |