*9 Hidden Layer with tensorboard :
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 81 82 83 84 85 86 87 88 | import tensorflow as tf import numpy as np tf.set_random_seed(777) # for reproducibility xy = np.loadtxt('./data/07train.txt') x_data = xy[:,0:-1] y_data = xy[:,[-1]] X = tf.placeholder(tf.float32, [None, 2]) Y = tf.placeholder(tf.float32, [None, 1]) # 계층은 3단에 뉴런의 개수를 5개로 지정했을 시 : W1 = tf.Variable(tf.random_uniform([2, 5], -1., 1.)) W2 = tf.Variable(tf.random_uniform([5, 5], -1., 1.)) W3 = tf.Variable(tf.random_uniform([5, 5], -1., 1.)) W4 = tf.Variable(tf.random_uniform([5, 5], -1., 1.)) W5 = tf.Variable(tf.random_uniform([5, 5], -1., 1.)) W6 = tf.Variable(tf.random_uniform([5, 5], -1., 1.)) W7 = tf.Variable(tf.random_uniform([5, 5], -1., 1.)) W8 = tf.Variable(tf.random_uniform([5, 5], -1., 1.)) W9 = tf.Variable(tf.random_uniform([5, 5], -1., 1.)) W10 = tf.Variable(tf.random_uniform([5, 5], -1., 1.)) W11 = tf.Variable(tf.random_uniform([5, 1], -1., 1.)) b1 = tf.Variable(tf.zeros([5])) b2 = tf.Variable(tf.zeros([5])) b3 = tf.Variable(tf.zeros([5])) b4 = tf.Variable(tf.zeros([5])) b5 = tf.Variable(tf.zeros([5])) b6 = tf.Variable(tf.zeros([5])) b7 = tf.Variable(tf.zeros([5])) b8 = tf.Variable(tf.zeros([5])) b9 = tf.Variable(tf.zeros([5])) b10 = tf.Variable(tf.zeros([5])) b11 = tf.Variable(tf.zeros([1])) # Hypotheis with tf.name_scope("layer1") as scope: L1 = tf.sigmoid(tf.matmul(X, W1) + b1) with tf.name_scope("layer2") as scope: L2 = tf.sigmoid(tf.matmul(L1, W2) + b2) with tf.name_scope("layer3") as scope: L3 = tf.sigmoid(tf.matmul(L2, W3) + b3) with tf.name_scope("layer4") as scope: L4 = tf.sigmoid(tf.matmul(L3, W4) + b4) with tf.name_scope("layer5") as scope: L5 = tf.sigmoid(tf.matmul(L4, W5) + b5) with tf.name_scope("layer6") as scope: L6 = tf.sigmoid(tf.matmul(L5, W6) + b6) with tf.name_scope("layer7") as scope: L7 = tf.sigmoid(tf.matmul(L6, W7) + b7) with tf.name_scope("layer8") as scope: L8 = tf.sigmoid(tf.matmul(L7, W8) + b8) with tf.name_scope("layer9") as scope: L9 = tf.sigmoid(tf.matmul(L8, W9) + b9) with tf.name_scope("layer10") as scope: L10 = tf.sigmoid(tf.matmul(L9, W10) + b10) with tf.name_scope("layer1") as scope: hypothesis = tf.sigmoid(tf.matmul(L10, W11) + b11) # 3단으로 쌓을 시 : L1 = tf.sigmoid(tf.matmul(X,W1) + b1) L2 = tf.sigmoid(tf.matmul(L1,W2) + b2) hypothesis = tf.sigmoid(tf.matmul(L2, W3) + b3) cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis)) train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32) accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for step in range(10001): sess.run(train, feed_dict={X: x_data, Y: y_data}) if step % 100 == 0: print(step, sess.run(cost, feed_dict={ X: x_data, Y: y_data})) h, c, a = sess.run([hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data}) print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a) | cs |
*TensorBoard 란 :
TV logging /debugging tool
=> Visualize your TF Graph
=> Plot quntitative metrics
=> show additional data
*Tensorboard를 사용하는 5가지 방법
=> From TF Graph, decide which tensors you want to log
=> merge all summeries
=> create write and add graph
=> run summary merge and summary
=> lanuch Tensor board
*Tensorboard 출력하기 :
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #$tensorboard --logdir=/tmp/xor_logs2 import tensorflow as tf import numpy as np xy = np.loadtxt('./data/07train.txt', unpack=True) x_data = np.transpose(xy[0:-1]) y_data = np.reshape(xy[-1], (4, 1)) print(x_data) print(y_data) X = tf.placeholder(tf.float32, name='x-input') Y = tf.placeholder(tf.float32, name='y-input') w1 = tf.Variable(tf.random_uniform([2, 10], -1.0, 1.0), name='weight1') w2 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight2') w3 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight3') w4 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight4') w5 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight5') w6 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight6') w7 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight7') w8 = tf.Variable(tf.random_uniform([10, 1], -1.0, 1.0), name='weight8') b1 = tf.Variable(tf.zeros([10]), name="Bias1") b3 = tf.Variable(tf.zeros([10]), name="Bias3") b2 = tf.Variable(tf.zeros([10]), name="Bias2") b4 = tf.Variable(tf.zeros([10]), name="Bias4") b5 = tf.Variable(tf.zeros([10]), name="Bias5") b6 = tf.Variable(tf.zeros([10]), name="Bias6") b7 = tf.Variable(tf.zeros([10]), name="Bias7") b8 = tf.Variable(tf.zeros([1]), name="Bias8") # L2 = tf.nn.relu(tf.matmul(X, w1) + b1) # L3 = tf.nn.relu(tf.matmul(L2, w2) + b2) # L4 = tf.nn.relu(tf.matmul(L3, w3) + b3) # L5 = tf.nn.relu(tf.matmul(L4, w4) + b4) # L6 = tf.nn.relu(tf.matmul(L5, w5) + b5) # L7 = tf.nn.relu(tf.matmul(L6, w6) + b6) # L8 = tf.nn.relu(tf.matmul(L7, w7) + b7) with tf.name_scope("layer1") as scope: L2 = tf.sigmoid(tf.matmul(X, w1) + b1) with tf.name_scope("layer2") as scope: L3 = tf.sigmoid(tf.matmul(L2, w2) + b2) with tf.name_scope("layer3") as scope: L4 = tf.sigmoid(tf.matmul(L3, w3) + b3) with tf.name_scope("layer4") as scope: L5 = tf.sigmoid(tf.matmul(L4, w4) + b4) with tf.name_scope("layer5") as scope: L6 = tf.sigmoid(tf.matmul(L5, w5) + b5) with tf.name_scope("layer6") as scope: L7 = tf.sigmoid(tf.matmul(L6, w6) + b6) with tf.name_scope("layer7") as scope: L8 = tf.sigmoid(tf.matmul(L7, w7) + b7) with tf.name_scope("layer8") as scope: hypothesis = tf.sigmoid(tf.matmul(L8, w8) + b8) with tf.name_scope('cost') as scope: cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1-Y) * tf.log(1 - hypothesis)) tf.summary.scalar("cost", cost) with tf.name_scope('train') as scope: a = tf.Variable(0.003) optimizer = tf.train.GradientDescentOptimizer(a) train = optimizer.minimize(cost) w1_hist = tf.summary.histogram("weights1", w1) w2_hist = tf.summary.histogram("weights2", w2) b1_hist = tf.summary.histogram("biases1", b1) b2_hist = tf.summary.histogram("biases2", b2) y_hist = tf.summary.histogram("y", Y) with tf.name_scope('accuracy') as scope: correct_prediction = tf.equal(tf.floor(hypothesis+0.5), Y) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) tf.summary.scalar("accuracy", accuracy) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) merged = tf.summary.merge_all() writer = tf.summary.FileWriter("./tmp/xor_logs3", sess.graph) for step in range(20000): sess.run(train, feed_dict={X: x_data, Y: y_data}) if step % 200 == 0: summary = sess.run(merged, feed_dict={X: x_data, Y: y_data}) writer.add_summary(summary, step) print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(w1), sess.run(w2)) print(sess.run([hypothesis, tf.floor(hypothesis+0.5), correct_prediction], feed_dict={X: x_data, Y: y_data})) print("accuracy", sess.run(accuracy, feed_dict={X: x_data, Y: y_data})) | cs |
위코드를 실행 후
View > Tool Windows > Terminal 선택 후
아래 Command 실행 :
>>> tensorboard --logdir=./tmp/xor_logs3
http://localhost:6006 브라우저에서 열기
*ReLU 소스코드 :
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 | import tensorflow as tf import numpy as np xy = np.loadtxt('./data/07train.txt') x_data = xy[:,0:-1] y_data = xy[:,[-1]] X = tf.placeholder(tf.float32, [None, 2]) Y = tf.placeholder(tf.float32, [None, 1]) w1 = tf.Variable(tf.random_uniform([2, 10], -1.0, 1.0), name='weight1') w2 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight2') w3 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight3') w4 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight4') w5 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight5') w6 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight6') w7 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight7') w8 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight8') w9 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight9') w10 = tf.Variable(tf.random_uniform([10, 10], -1.0, 1.0), name='weight10') w11 = tf.Variable(tf.random_uniform([10, 1], -1.0, 1.0), name='weight11') b1 = tf.Variable(tf.zeros([10]), name="Bias1") b3 = tf.Variable(tf.zeros([10]), name="Bias3") b2 = tf.Variable(tf.zeros([10]), name="Bias2") b4 = tf.Variable(tf.zeros([10]), name="Bias4") b5 = tf.Variable(tf.zeros([10]), name="Bias5") b6 = tf.Variable(tf.zeros([10]), name="Bias6") b7 = tf.Variable(tf.zeros([10]), name="Bias7") b8 = tf.Variable(tf.zeros([10]), name="Bias8") b9 = tf.Variable(tf.zeros([10]), name="Bias9") b10 = tf.Variable(tf.zeros([10]), name="Bias10") b11 = tf.Variable(tf.zeros([1]), name="Bias11") #L1 = tf.sigmoid(tf.matmul(X, w1) + b1) L1 = tf.nn.relu(tf.matmul(X, w1) + b1) L2 = tf.nn.relu(tf.matmul(L1, w2) + b2) L3 = tf.nn.relu(tf.matmul(L2, w3) + b3) L4 = tf.nn.relu(tf.matmul(L3, w4) + b4) L5 = tf.nn.relu(tf.matmul(L4, w5) + b5) L6 = tf.nn.relu(tf.matmul(L5, w6) + b6) L7 = tf.nn.relu(tf.matmul(L6, w7) + b7) L8 = tf.nn.relu(tf.matmul(L7, w8) + b8) L9 = tf.nn.relu(tf.matmul(L8, w9) + b9) L10 = tf.nn.relu(tf.matmul(L9, w10) + b10) hypothesis = tf.sigmoid(tf.matmul(L10, w11) + b11) cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1-Y) * tf.log(1 - hypothesis)) a = tf.Variable(0.01) optimizer = tf.train.GradientDescentOptimizer(a) train = optimizer.minimize(cost) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) for step in range(10000): sess.run(train, feed_dict={X: x_data, Y: y_data}) if step % 200 == 0: print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(w1), sess.run(w2)) correct_prediction = tf.equal(tf.floor(hypothesis+0.5), Y) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) print(sess.run([hypothesis, tf.floor(hypothesis+0.5), correct_prediction], feed_dict={X: x_data, Y: y_data})) print("accuracy", accuracy.eval({X: x_data, Y: y_data})) | cs |
'Python 활용 딥러닝' 카테고리의 다른 글
CNN + Convolution Neural Network (0) | 2018.12.20 |
---|---|
Neural Nets for MNIST, Xavier Initialization, Dropout 적용 소스 (0) | 2018.12.19 |
Neural Nets & Deep learning, Neural Nets for XOR (0) | 2018.12.19 |
학습에 영향을 주는 요소들 (Learning Rate, Overfitting, Online Learning) (0) | 2018.12.19 |
Softmax Classification(multinominal classification) (0) | 2018.12.18 |