<jupyter_start><jupyter_text>Embeddings 事前準備<jupyter_code>import os

import tensorflow as tf

sess = tf.Session()

LOG_PATH = "/root/tmp/tensorboard/mnist"
DATA_PATH = os.path.join(os.getcwd(), "mnist/data/")
LABELS_PATH = os.path.join(os.getcwd(), "mnist/labels_1024.tsv")
SPRITE_PATH = os.path.join(os.getcwd(), "mnist/sprite_1024.png")

mnist = tf.contrib.learn.datasets.mnist.read_data_sets(train_dir=DATA_PATH, one_hot=True)<jupyter_output>Extracting /root/notebooks/playground_ja/tensorflow/mnist/data/train-images-idx3-ubyte.gz
Extracting /root/notebooks/playground_ja/tensorflow/mnist/data/train-labels-idx1-ubyte.gz
Extracting /root/notebooks/playground_ja/tensorflow/mnist/data/t10k-images-idx3-ubyte.gz
Extracting /root/notebooks/playground_ja/tensorflow/mnist/data/t10k-labels-idx1-ubyte.gz<jupyter_text>Layer definition Convolutional Layer<jupyter_code>def conv_layer(input, channels_in, channels_out, name="conv"):
    with tf.name_scope(name):
        w = tf.Variable(tf.truncated_normal([5, 5, channels_in, channels_out], stddev=0.1), name="w")
        b = tf.Variable(tf.constant(0.1, shape=[channels_out]), name="b")
        conv = tf.nn.conv2d(input, w, strides=[1, 1, 1, 1], padding="SAME")
        a = tf.nn.relu(conv + b)
        tf.summary.histogram("weights", w)
        tf.summary.histogram("biases", b)
        tf.summary.histogram("activations", a)
        return a<jupyter_output><empty_output><jupyter_text>Fully Connected Layer<jupyter_code>def fc_layer(input, channels_in, channels_out, name="fc"):
    with tf.name_scope(name):
        w = tf.Variable(tf.truncated_normal([channels_in, channels_out], stddev=0.1), name="w")
        b = tf.Variable(tf.constant(0.1, shape=[channels_out]), name="b")
        a = tf.matmul(input, w) + b
        tf.summary.histogram("weights", w)
        tf.summary.histogram("biases", b)
        tf.summary.histogram("activations", a)
        return a<jupyter_output><empty_output><jupyter_text>Feed-Forward network<jupyter_code># Setup placeholders, and reshape the data
x = tf.placeholder(tf.float32, shape=[None, 784], name="images")
y = tf.placeholder(tf.float32, shape=[None, 10], name="labels")

x_image = tf.reshape(x, [-1, 28, 28, 1])

tf.summary.image('input', x_image, 3)

# Create the network
conv1 = conv_layer(x_image, 1, 32, "conv1")
pool1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")

conv2 = conv_layer(pool1, 32, 64, "conv2")
pool2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")

flattened = tf.reshape(pool2, [-1, 7 * 7 * 64])

fc1 = fc_layer(flattened, 7 * 7 * 64, 1024, "fc1")

fc2 = fc_layer(fc1, 1024, 10, "fc2")<jupyter_output><empty_output><jupyter_text>Loss & Training<jupyter_code># Compute cross entropy as loss function
with tf.name_scope("loss"):
    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(logits=fc2, labels=y)
    )
    tf.summary.scalar("cross_entropy", cross_entropy)

# use the AdamOptimizer to train the network
with tf.name_scope("train"):
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

# compute the accuracy
with tf.name_scope("accuracy"):
    prediction = tf.equal(tf.argmax(fc2, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32))
    tf.summary.scalar("accuracy", accuracy)

sess.run(tf.global_variables_initializer())
merged_summary = tf.summary.merge_all()

embedding = tf.Variable(tf.zeros([1024, 7 * 7 * 64]), name="embedding")
assignment = embedding.assign(flattened)

saver = tf.train.Saver()

writer = tf.summary.FileWriter(LOG_PATH)
writer.add_graph(sess.graph)

config = tf.contrib.tensorboard.plugins.projector.ProjectorConfig()
embedding_config = config.embeddings.add()
embedding_config.tensor_name = embedding.name
embedding_config.sprite.image_path = SPRITE_PATH
embedding_config.metadata_path = LABELS_PATH
embedding_config.sprite.single_image_dim.extend([28, 28])

tf.contrib.tensorboard.plugins.projector.visualize_embeddings(writer, config)

for i in range(300):
    batch = mnist.train.next_batch(100)

    if i % 10 == 0:
        [train_accuracy, summary] = sess.run([accuracy, merged_summary], feed_dict={
            x: batch[0],
            y: batch[1]
        })
        writer.add_summary(summary, i)
        print("step %d train accuracy %g" % (i, train_accuracy))

    if i % 100 == 0:
        sess.run(
            assignment,
            feed_dict={
                x: mnist.test.images[:1024],
                y: mnist.test.labels[:1024]
            }
        )

        saver.save(sess, os.path.join(LOG_PATH, "model.ckpt"), i)

    sess.run(train_step, feed_dict={
        x: batch[0],
        y: batch[1]
    })<jupyter_output>step 0 train accuracy 0.1
step 10 train accuracy 0.23
step 20 train accuracy 0.3
step 30 train accuracy 0.39
step 40 train accuracy 0.67
step 50 train accuracy 0.71
step 60 train accuracy 0.81
step 70 train accuracy 0.75
step 80 train accuracy 0.77
step 90 train accuracy 0.84
step 100 train accuracy 0.92
step 110 train accuracy 0.87
step 120 train accuracy 0.85
step 130 train accuracy 0.87
step 140 train accuracy 0.94
step 150 train accuracy 0.89
step 160 train accuracy 0.91
step 170 train accuracy 0.85
step 180 train accuracy 0.88
step 190 train accuracy 0.93
step 200 train accuracy 0.84
step 210 train accuracy 0.93
step 220 train accuracy 0.98
step 230 train accuracy 0.89
step 240 train accuracy 0.94
step 250 train accuracy 0.9
step 260 train accuracy 0.93
step 270 train accuracy 0.94
step 280 train accuracy 0.91
step 290 train accuracy 0.94
{'fraction_non_alphanumeric': 0.1320687186828919, 'fraction_numerical': 0.05637079455977094, 'mean_word_length': 4.122823098075161, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '315080', 'license': 'MIT', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2009, 'n_words': 447, 'path': 'notebooks/playground_ja/tensorflow/00_tensorboard_embedding.ipynb', 'repo_name': 'tokuda109/tensorflow-docker-skeleton', 'size': '5588'}