<jupyter_start><jupyter_text>Lab 9 XOR  Lab09-4-xor_tensorboard> tensorboard를 이용해서 그래프 표현!!>> terminal 창에서 명령어 입력해줘야함!!>> $ tensorboard --logdir=./logs/xor_logs<jupyter_code>import tensorflow as tf
import numpy as np
tf.set_random_seed(777)  # for reproducibility
learning_rate = 0.01
x_data = [[0, 0],
          [0, 1],
          [1, 0],
          [1, 1]]
y_data = [[0],
          [1],
          [1],
          [0]]
x_data = np.array(x_data, dtype=np.float32)
y_data = np.array(y_data, dtype=np.float32)
X = tf.placeholder(tf.float32, [None, 2], name='x-input')
Y = tf.placeholder(tf.float32, [None, 1], name='y-input')
# 나중에 tensor graph를 보여줄 때 layer별로 구분되게 그림이 나오게 하기 위해서
# tf.name_scope()를 이용함
with tf.name_scope("layer1"):
    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)

    w1_hist = tf.summary.histogram("weights1", W1)
    b1_hist = tf.summary.histogram("biases1", b1)
    layer1_hist = tf.summary.histogram("layer1", layer1)
with tf.name_scope("layer2"):
    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)

    w2_hist = tf.summary.histogram("weights2", W2)
    b2_hist = tf.summary.histogram("biases2", b2)
    hypothesis_hist = 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))
    cost_summ = tf.summary.scalar("cost", cost)
# Optimizer
with tf.name_scope("train"):
    train = tf.train.AdamOptimizer(learning_rate=learning_rate).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))
accuracy_summ = 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, _ = sess.run([merged_summary, train], feed_dict={X: x_data, Y: y_data})
        writer.add_summary(summary, global_step=step)

        if step % 100 == 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)


'''
Hypothesis:  [[  6.13103184e-05]
 [  9.99936938e-01]
 [  9.99950767e-01]
 [  5.97514772e-05]]
Correct:  [[ 0.]
 [ 1.]
 [ 1.]
 [ 0.]]
Accuracy:  1.0
'''<jupyter_output>0 0.715638 [array([[ 0.79269588,  0.68861037],
       [-1.20728338, -0.29517072]], dtype=float32), array([[ 1.7177    ],
       [ 0.35572484]], dtype=float32)]
100 0.690733 [array([[ 0.32268244, -0.23303036],
       [-0.7743417 ,  0.80529428]], dtype=float32), array([[ 1.46081126],
       [ 0.78415203]], dtype=float32)]
200 0.495032 [array([[ 1.67216921, -2.32165432],
       [-2.21686363,  2.65614676]], dtype=float32), array([[ 2.82439137],
       [ 2.60258079]], dtype=float32)]
300 0.223746 [array([[ 3.18776321, -3.74428773],
       [-3.69724321,  4.12956667]], dtype=float32), array([[ 4.70040846],
       [ 4.31880569]], dtype=float32)]
400 0.122295 [array([[ 3.98867249, -4.52083302],
       [-4.4841404 ,  4.90343523]], dtype=float32), array([[ 5.84872627],
       [ 5.41899443]], dtype=float32)]
500 0.0788432 [array([[ 4.48614645, -5.01518631],
       [-4.97325087,  5.39076996]], dtype=float32), array([[ 6.64474869],
       [ 6.20202541]], dtype=float32)]
600 0.0559812 [array([[ 4.839[...]
{'fraction_non_alphanumeric': 0.17950635751682872, 'fraction_numerical': 0.1393667414609823, 'mean_word_length': 3.4726867335562988, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '615640', 'license': 'MIT', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1654, 'n_words': 302, 'path': 'Python/tensorflow/DeepLearningZeroToAll/Lab09-4-xor_tensorboard.ipynb', 'repo_name': 'statKim/TIL', 'size': '4011'}