<jupyter_start><jupyter_text>5.4. Initialize all variables and merge the summaries<jupyter_code>init = tf.global_variables_initializer()
# Merge all summaries
merged = tf.summary.merge_all()<jupyter_output><empty_output><jupyter_text>6. Train<jupyter_code>sess = tf.InteractiveSession()
sess.run(init)
global_step = 0
summary_writer = tf.summary.FileWriter(logs_path, sess.graph)
num_tr_iter = int(len(y_train) / batch_size)
for epoch in range(epochs):
    print('Training epoch: {}'.format(epoch + 1))
    X_train, y_train = randomize(X_train, y_train)
    for iteration in range(num_tr_iter):
        global_step += 1
        start = iteration * batch_size
        end = (iteration + 1) * batch_size
        X_batch, y_batch = get_next_batch(X_train, y_train, start, end)

        # Run optimization op (backprop)
        feed_dict_batch = {X: X_batch, y: y_batch}
        sess.run(optimizer, feed_dict=feed_dict_batch)

        if iteration % display_freq == 0:
            # Calculate and display the batch loss and accuracy
            loss_batch, acc_batch, summary_tr = sess.run([loss, accuracy, merged],
                                                         feed_dict=feed_dict_batch)
            summary_writer.add_summary(summary_tr, global_step)

            print("iter {0:3d}:\t Loss={1:.5f},\tTraining Accuracy={2:.01%}".
                  format(iteration, loss_batch, acc_batch))

    # Run validation after every epoch
    feed_dict_test = {X: X_test, y: y_test}
    loss_test, acc_test = sess.run([loss, accuracy], feed_dict=feed_dict_test)
    print('---------------------------------------------------------')
    print("Epoch: {0}, test loss: {1:.5f}, test accuracy: {2:.01%}".
          format(epoch + 1, loss_test, acc_test))
    print('---------------------------------------------------------')<jupyter_output>Training epoch: 1
iter   0:	 Loss=1126.16565,	Training Accuracy=55.0%
iter  10:	 Loss=18.14694,	Training Accuracy=48.0%
iter  20:	 Loss=2.32702,	Training Accuracy=64.0%
iter  30:	 Loss=4.14352,	Training Accuracy=53.0%
---------------------------------------------------------
Epoch: 1, test loss: 8.17913, test accuracy: 49.4%
---------------------------------------------------------
Training epoch: 2
iter   0:	 Loss=8.58409,	Training Accuracy=53.0%
iter  10:	 Loss=3.84855,	Training Accuracy=67.0%
iter  20:	 Loss=1.66643,	Training Accuracy=65.0%
iter  30:	 Loss=1.38022,	Training Accuracy=70.0%
---------------------------------------------------------
Epoch: 2, test loss: 1.51692, test accuracy: 64.8%
---------------------------------------------------------
Training epoch: 3
iter   0:	 Loss=1.21541,	Training Accuracy=72.0%
iter  10:	 Loss=1.48549,	Training Accuracy=72.0%
iter  20:	 Loss=3.65463,	Training Accuracy=63.0%
iter  30:	 Loss=3.89859,	Training Accuracy=59.0%
--------------------[...]<jupyter_text>7. Test<jupyter_code>test_list = {
    'X': [],
    'y': []
}
import random
for x in range(9):
    rand_idx = random.randint(0, 999)
    test_list['X'].append(X_test[rand_idx])
    test_list['y'].append(y_test[rand_idx])
test_list['X'] = np.array(test_list['X'])
test_list['y'] = np.array(test_list['y'])

y_pred, acc = sess.run([pred, accuracy], feed_dict={X: test_list['X'], y: test_list['y']})
print('Accuracy of random 10 images: %.2f' % acc)
fig, axes = plt.subplots(3, 3, figsize=(15, 15))
fig.subplots_adjust(hspace=0.3, wspace=0.3)
for i, ax in enumerate(axes.flat):
    ax.imshow(test_list['X'][i])
    mapping = ['cat', 'dog']
    cls_true = int(test_list['y'][i])
    cls_pred = int(np.round(y_pred[i]))
    ax.set_title("True: {0}, Pred: {1}".format(mapping[cls_true], mapping[cls_pred]))
    ax.set_xticks([])
    ax.set_yticks([])<jupyter_output>Accuracy of random 10 images: 0.78
{'fraction_non_alphanumeric': 0.23007198080511865, 'fraction_numerical': 0.054385497200746466, 'mean_word_length': 2.503267973856209, 'pattern_counts': {'":': 0, '<': 11, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '626676', 'license': 'MIT', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1279, 'n_words': 275, 'path': 'material/code/lab3/cat and dog-Copy2.ipynb', 'repo_name': 'SiNcSaD/NTUTEE_TF', 'size': '3751'}