<jupyter_start><jupyter_text>手寫數字辨識Author: <NAME><jupyter_code>import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST/MNIST_data/", one_hot=True)
print('train image:', mnist.train.images.shape)
print('train label:', mnist.train.labels.shape)
print('test image:', mnist.test.images.shape)
print('test label:', mnist.test.labels.shape)
# Hyper-Parameters
learning_rate = 0.001
batch_size = 100
total_step = 3000

# Network Parameters
n_input = 784
n_hidden_1 = 512
n_hidden_2 = 512
n_classes = 10
x = tf.placeholder(tf.float32, [None, n_input])
y_ = tf.placeholder(tf.float32, [None, n_classes])

with tf.name_scope('hidden1'):
    weights = tf.Variable(tf.random_normal([n_input, n_hidden_1]),
                          name='weights')
    biases = tf.Variable(tf.zeros([n_hidden_1]),
                         name='biases')
    layer_1 = tf.matmul(x, weights) + biases
    
with tf.name_scope('hidden2'):
    weights = tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2]),
                          name='weights')
    biases = tf.Variable(tf.zeros([n_hidden_2]),
                         name='biases')
    layer_2 = tf.matmul(layer_1, weights) + biases

with tf.name_scope('output'):
    weights = tf.Variable(tf.random_normal([n_hidden_2, n_classes]),
                          name='weights')
    biases = tf.Variable(tf.zeros([n_classes]),
                         name='biases')
    logits = tf.matmul(layer_2, weights) + biases

with tf.name_scope('cross_entropy'):
    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=y_))

with tf.name_scope('train'):
    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)

with tf.name_scope('accuracy'):
    y = tf.nn.softmax(logits)
    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    
    for i in range(total_step):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        sess.run(train_step,
                 feed_dict={x: batch_xs, y_: batch_ys})
        
        if i % 100 == 0:
            loss, acc = sess.run([cross_entropy, accuracy],
                                 feed_dict={x: batch_xs, y_: batch_ys})
            test_loss, test_acc = sess.run([cross_entropy, accuracy], 
                                           feed_dict={x: mnist.test.images, y_: mnist.test.labels})
            print('step: {:4d}\t loss: {:.2f}\t acc: {:.2f}\t test_loss: {:.2f}\t test_acc: {:.2f}'.
                  format(i, loss, acc, test_loss, test_acc))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13670613562970937, 'fraction_numerical': 0.018658055256548258, 'mean_word_length': 2.9828571428571427, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '254803', 'license': 'MIT', 'n_tokens_mistral': 1021, 'n_tokens_neox': 967, 'n_words': 162, 'path': 'material/code/lab2/Lab2.ipynb', 'repo_name': 'SiNcSaD/NTUTEE_TF', 'size': '2792'}