Print..Print

๋งจ๋‚  ํ™•์ธํ•˜๋ ค๋ฉด ํ”„๋ฆฐํŠธํ•ด์•ผํ•ด.

GUI๋กœ ๋ฉ‹์ง€๊ฒŒ ๋ด๋ณด์ž.

5 Steps of using TensorBoard

  1. ๋‚ด๊ฐ€ Logํ™” ํ•˜๊ณ  ์‹ถ์€ ํ…์„œ๋“ค์„ ๊ฒฐ์ •ํ•œ๋‹ค.

    ์ด๊ฒƒ๋“ค์€ ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

    w2_hist = tf.summary.histogram("weight2", W2)
    cost_sum = tf.summary.scalar("cost", cost)
  2. summary๋ฅผ ๋ชจ๋‘ ๋ณ‘ํ•ฉํ•œ๋‹ค.

    summary = tf.summary.merge_all()
  3. ํŒŒ์ผ์“ฐ๊ธฐ๋ฅผ ์œ„ํ•œ ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ๋ž˜ํ”„๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

    writer = tf.summary.FileWriter('./logs')
    writer.add_graph(sess.graph)
  4. summary ๋…ธ๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ณ  ํŒŒ์ผ์— ๊ธฐ๋กํ•œ๋‹ค.

    s, _ = sess.run([summary, optimizer], feed_dict - feed_dict)
    writer.add_summary(s, global_step=global_step)
  5. ํ…์„œ๋ณด๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.

    $ tensorboard --logdir=./logs

Scalar tensors

cost์™€ ๊ฐ™์ด ์‹์ด ํ•œ์ค„์ธ, ์ฆ‰ output์ด ์Šค์นผ๋ผ๊ฐ’์ธ ๋…€์„๋“ค์€ ์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

cost_summ = tf.summary.scalar("cost", cost)

Histogram (multi-dimensional tensors)

W1 = tf.Variable(tf.random_normal([2, 2]), name="weight_1")
    b1 = tf.Variable(tf.random_normal([2]), name="bias_1")
    layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)
 
    tf.summary.histogram("W1", W1)
    tf.summary.histogram("b1", b1)
    tf.summary.histogram("Layer1", layer1)

Scope (graph hierarchy)

with tf.name_scope("Layer1"):
    W1 = tf.Variable(tf.random_normal([2, 2]), name="weight_1")
    b1 = tf.Variable(tf.random_normal([2]), name="bias_1")
    layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)
 
    tf.summary.histogram("W1", W1)
    tf.summary.histogram("b1", b1)
    tf.summary.histogram("Layer1", layer1)
 
with tf.name_scope("Layer2"):
    W2 = tf.Variable(tf.random_normal([2, 1]), name="weight_2")
    b2 = tf.Variable(tf.random_normal([1]), name="bias_2")
    hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)
 
    tf.summary.histogram("W2", W2)
    tf.summary.histogram("b2", b2)
    tf.summary.histogram("Hypothesis", hypothesis)

ํ•œ๋ฒˆ์— ๋ณด๊ธฐ ํž˜๋“œ๋‹ˆ ์ด๋ ‡๊ฒŒ ๊ณ„์ธต ๊ตฌ์กฐ๋กœ ๋ณด์—ฌ์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค.

ํ•ด๋‹น layer๋ฅผ ํด๋ฆญํ•  ๊ฒฝ์šฐ ๋ณด๋‹ค ์ƒ์„ธํ•œ ๊ทธ๋ž˜ํ”„ ๊ตฌ์กฐ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Merge summaries

 
merged_summary = tf.summary.merge_all()

writer node ์ƒ์„ฑ

writer = tf.summary.FileWriter("./logs/xor_logs_r0_01")
writer.add_graph(sess.graph)  # Show the graph

ํ•ด๋‹น ๊ฒฝ๋กœ์— ๋กœ๊ทธํŒŒ์ผ์„ ์“ฐ๊ณ ,

์ด๊ฑธ sess.graph์— ์ถ”๊ฐ€ํ•ด์ค˜!

Run merged summary and write

for step in range(10001):
        _, summary, cost_val = sess.run(
            [train, merged_summary, cost], feed_dict={X: x_data, Y: y_data}
        )
        writer.add_summary(summary, global_step=step)
 
        if step % 100 == 0:

step๋งˆ๋‹ค ๊ทธ๋ž˜ํ”„๋ฅผ summary๋ฅผ ๋”ํ•ด๋ผ.

Launch tensorboard(local)

ํŒŒ์ผ์„ ์“ด ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•˜๊ณ 

$ tensorboard --logdir=./logs/xor_logs

Launch tensorboard(remote server)

$ ssh -L local_port:127.0.0.1:remote_port username@server.com

์ถ”๊ฐ€ ๊ณต๋ถ€๊ฐ€ ํ•„์š”ํ•จ ์ถ”๊ฐ€๋กœ ๋‹ค๋“ฌ๊ฑฐ๋‚˜ ๊ณต๋ถ€ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค

์ „์ฒด ์ฝ”๋“œ

# Lab 9 XOR
import tensorflow as tf
import numpy as np
 
tf.set_random_seed(777)  # for reproducibility
 
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, [None, 2], name="x")
Y = tf.placeholder(tf.float32, [None, 1], name="y")
 
with tf.name_scope("Layer1"):
    W1 = tf.Variable(tf.random_normal([2, 2]), name="weight_1")
    b1 = tf.Variable(tf.random_normal([2]), name="bias_1")
    layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)
 
    tf.summary.histogram("W1", W1)
    tf.summary.histogram("b1", b1)
    tf.summary.histogram("Layer1", layer1)
 
with tf.name_scope("Layer2"):
    W2 = tf.Variable(tf.random_normal([2, 1]), name="weight_2")
    b2 = tf.Variable(tf.random_normal([1]), name="bias_2")
    hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)
 
    tf.summary.histogram("W2", W2)
    tf.summary.histogram("b2", b2)
    tf.summary.histogram("Hypothesis", hypothesis)
 
# cost/loss function
with tf.name_scope("Cost"):
    cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))
    tf.summary.scalar("Cost", cost)
 
with tf.name_scope("Train"):
    train = tf.train.AdamOptimizer(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))
tf.summary.scalar("accuracy", accuracy)
 
# Launch graph
with tf.Session() as sess:
    # tensorboard --logdir=./logs/xor_logs
    merged_summary = tf.summary.merge_all()
    writer = tf.summary.FileWriter("./logs/xor_logs_r0_01")
    writer.add_graph(sess.graph)  # Show the graph
 
    # Initialize TensorFlow variables
    sess.run(tf.global_variables_initializer())
 
    for step in range(10001):
        _, summary, cost_val = sess.run(
            [train, merged_summary, cost], feed_dict={X: x_data, Y: y_data}
        )
        writer.add_summary(summary, global_step=step)
 
        if step % 100 == 0:
            print(step, cost_val)
 
    # Accuracy report
    h, p, a = sess.run(
        [hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data}
    )
    
    print(f"\nHypothesis:\n{h} \nPredicted:\n{p} \nAccuracy:\n{a}")
 
"""
Hypothesis:
[[6.1310326e-05]
 [9.9993694e-01]
 [9.9995077e-01]
 [5.9751470e-05]] 
Predicted:
[[0.]
 [1.]
 [1.]
 [0.]] 
Accuracy:
1.0
"""

Multiple runs (ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ”๊ฟ”์„œ)

/logs ํด๋”์•ˆ์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ”๊ฟ”์„œ ํŒŒ์ผ์„ ์—ฌ๋Ÿฌ๊ฐœ ์“ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ํ•™์Šต์œจ์„ ๋‹ค๋ฅด๊ฒŒ ์“ด ๋…€์„์„ ๋น„๊ตํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด,

with tf.name_scope("Train"):
    train = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)
...
writer = tf.summary.FileWriter("./logs/xor_logs")
with tf.name_scope("Train"):
    train = tf.train.AdamOptimizer(learning_rate=0.1).minimize(cost)
...
writer = tf.summary.FileWriter("./logs/xor_logs")

์ด๋ ‡๊ฒŒ ๋‘๊ฐœ๋ฅผ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋‹ค๋ฅด๊ฒŒ ํ•ด์„œ ๊ธฐ๋กํ•ด ๋‘” ๋’ค, terminal์—์„œ

$ tensorboard --logdir=./logs

์ด๋ ‡๊ฒŒ ์ƒ์œ„ ํด๋”๋ฅผ ์‹คํ–‰์‹œ์ผœ ๋ฒ„๋ฆฐ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ๋‘ ๊ฐœ์˜ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ , ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.