<jupyter_start><jupyter_text>[MNIST](http://yann.lecun.com/exdb/mnist/) hosted by <NAME>THE MNIST DATABASE of handwritten digits<jupyter_code>mnist = tf.contrib.learn.datasets.load_dataset("mnist")
train_data = mnist.train.images
train_labels = np.asarray(mnist.train.labels, dtype=np.int32)
eval_data = mnist.test.images
eval_labels = np.asarray(mnist.test.labels, dtype=np.int32)
mnist.train.num_examples
mnist.test.num_examples
plt.rcParams["figure.figsize"] = [18, 9]
for i in range(4):
    for j in range(5):
        idx = i*5 + j
        plt.subplot(4, 5, idx+1)
        plt.imshow(np.reshape(train_data[idx], [28, 28]), cmap='gray')
plt.show()
plt.rcParams["figure.figsize"] = [12, 6]
plt.imshow(np.reshape(train_data[2], [28, 28]), cmap='gray')
plt.show()
train_labels[2]<jupyter_output><empty_output><jupyter_text>- 숫자 이미지 하나의 크기는 $28 \times 28$- Gray scale<jupyter_code>train_data[2][170:190]
# inspired by CNN model from https://www.tensorflow.org/tutorials/layers

def dnn_model_fn(features, labels, mode):
    input_layer = features["x"]
      
    hidden1 = tf.layers.dense(inputs=input_layer, units=1024, activation=tf.nn.relu)
    hidden2 = tf.layers.dense(inputs=hidden1, units=512, activation=tf.nn.relu)
    hidden3 = tf.layers.dense(inputs=hidden2, units=1024, activation=tf.nn.relu)
    logits = tf.layers.dense(inputs=hidden3, units=10)  # activation = None => linear

    predictions = {"classes": tf.argmax(input=logits, axis=1),
                   "probabilities": tf.nn.softmax(logits, name="softmax_tensor")}

    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

    if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
        train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)

    eval_metric_ops = {"accuracy": tf.metrics.accuracy(labels=labels, predictions=predictions["classes"])}
    return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
mnist_classifier = tf.estimator.Estimator(model_fn=dnn_model_fn, model_dir="mnist_dnn_model")
logging_hook = tf.train.LoggingTensorHook(tensors={}, at_end=True)

train_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": train_data},
                                                    y=train_labels,
                                                    batch_size=100,
                                                    num_epochs=None,
                                                    shuffle=True)
mnist_classifier.train(input_fn=train_input_fn,
                       steps=2000,
                       hooks=[logging_hook])
eval_input_fn = tf.estimator.inputs.numpy_input_fn(x={"x": eval_data},
                                                   y=eval_labels,
                                                   num_epochs=1,
                                                   shuffle=False)
mnist_classifier.evaluate(input_fn=eval_input_fn)
test_images = eval_data[np.random.choice(mnist.test.num_examples, 3)]
input_fn = tf.estimator.inputs.numpy_input_fn(x={'x': test_images}, shuffle=False)
preds = list(mnist_classifier.predict(input_fn))

for i in range(3):
    plt.imshow(np.reshape(test_images[i], [28, 28]), cmap='gray')
    plt.show()
    print("Model prediction:", preds[i])<jupyter_output>INFO:tensorflow:Restoring parameters from mnist_dnn_model\model.ckpt-2000
{'fraction_non_alphanumeric': 0.12721729490022174, 'fraction_numerical': 0.02189578713968958, 'mean_word_length': 3.115165336374002, 'pattern_counts': {'":': 5, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '303800', 'license': 'MIT', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1212, 'n_words': 175, 'path': '4. MNIST.ipynb', 'repo_name': 'jongukim/notebooks-deep-learning', 'size': '3612'}