<jupyter_start><jupyter_text>Lesson 28 - CIFAR-10 Cats and Dogs Round 6<NAME>-Pinion CM 208<jupyter_code>import numpy as np
import tensorflow as tf
from sklearn.preprocessing import OneHotEncoder
import time
# load training, validation and testing data sets
CIFAR10 = np.load('./Data/CIFAR-10_cats_dogs_split.npz')
label_names  = CIFAR10['label_names']
images_train = CIFAR10['images_train']
labels_train = CIFAR10['labels_train']
images_valid = CIFAR10['images_valid']
labels_valid = CIFAR10['labels_valid']

print('CIFAR-10 cats and dogs training and validation data set loaded.')
print('  training input:',images_train.shape,' output:',labels_train.shape)
print('validation input:',images_valid.shape,' output:',labels_valid.shape)
inputs_train = images_train
inputs_valid = images_valid
# one-hot-encode labels
outputs_train = OneHotEncoder(sparse=False).fit_transform(labels_train.reshape(-1,1))
outputs_valid = OneHotEncoder(sparse=False).fit_transform(labels_valid.reshape(-1,1))

print('Data reshaped, scaled and one-hot-encoded:')
print('   training input:',inputs_train.shape,' output:',outputs_train.shape)
print(' validation input:',inputs_valid.shape,' output:',outputs_valid.shape)
# Hyper-parameters
EPOCHS = 100
N1 = 6000
PS = 4
DEPTH = 8
DEPTH_2 = 8*2
LAM = 0.01
BS = 500
STD = 0.01
# Tensorflow computation graph
x = tf.placeholder(shape=[None, 32, 32, 3], dtype=tf.float32)
y = tf.placeholder(shape=[None, 2], dtype=tf.float32)

# input --> convLayer 1
W0 = tf.Variable(tf.truncated_normal([PS, PS, 3, DEPTH], stddev=STD))
b0 = tf.Variable(tf.truncated_normal([DEPTH], stddev=STD))
y1 = tf.nn.relu(tf.nn.conv2d(x, W0, [1, 2, 2, 1], padding='SAME') + b0)

# convLayer 1 --> convLayer2
W1 = tf.Variable(tf.truncated_normal([PS, PS, DEPTH, DEPTH_2], stddev=STD))
b1 = tf.Variable(tf.truncated_normal([DEPTH_2], stddev=STD))
y2 = tf.nn.relu(tf.nn.conv2d(y1, W1, [1, 2, 2, 1], padding='SAME') + b1)

# convLayer2 --> fullyConnected
flat_shape = 8 * 8 * DEPTH_2
y2_flat = tf.reshape(y2, [-1, flat_shape])
W2 = tf.Variable(tf.truncated_normal([flat_shape, 2], stddev=STD))
b2 = tf.Variable(tf.truncated_normal([2], stddev=STD))
logits = tf.matmul(y2_flat, W2) + b2
y_pred = tf.argmax(tf.nn.softmax(logits), 1)

l2 = LAM * tf.reduce_mean(tf.nn.l2_loss(W0) + tf.nn.l2_loss(W1) + tf.nn.l2_loss(W2))
CE = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, y))
optimizer = tf.train.AdamOptimizer().minimize(CE + l2)
accuracy = 1.0 - tf.contrib.metrics.accuracy(y_pred, tf.argmax(y, 1))
def trainNetwork(sess, init):
    sess.run(init)
    t_start = time.time()
    num_batches = np.ceil(N1 / BS)
    print('%15s%20s%23s' % (' ','cross-entropy','error-rate'))
    print('%15s%12s%12s%12s%12s%12s%12s' % ('epoch','training','validation','training','validation','L2','time (min)'))
    for e in np.arange(EPOCHS):
        perm = np.random.permutation(N1)
        ix = np.arange(0, BS)
        e += 1
        for batch in np.arange(num_batches):
            x_batch = inputs_train[perm[ix], :]
            y_batch = outputs_train[perm[ix], :]
            sess.run([optimizer],
                     feed_dict={x: x_batch, y: y_batch})
            ix += BS
        if (e % (EPOCHS / 10)) == 0:
            CE_train, error_train, l2_loss = sess.run([CE, accuracy, l2],
                                         feed_dict={x: inputs_train,
                                                    y: outputs_train})
            CE_valid, error_valid = sess.run([CE, accuracy],
                                         feed_dict={x: inputs_valid,
                                                    y: outputs_valid})
            total_compute_time = (time.time() - t_start) / 60
            print('%7d %7d%12.5f%12.5f%12.3f%12.3f%12f%12.1f' \
                  % (EPOCHS, e, CE_train, CE_valid,
                     error_train, error_valid, l2_loss, total_compute_time))
sess = tf.Session()
init = tf.initialize_all_variables()
trainNetwork(sess, init)<jupyter_output>cross-entropy             error-rate
          epoch    training  validation    training  validation          L2  time (min)
    100      10     0.61208     0.64131       0.333       0.373    0.002219         0.2
    100      20     0.54286     0.64249       0.277       0.358    0.005635         0.3
    100      30     0.48016     0.66319       0.233       0.350    0.009629         0.5
    100      40     0.44841     0.69359       0.214       0.335    0.013664         0.6
    100      50     0.40333     0.76226       0.185       0.354    0.016978         0.8
    100      60     0.37042     0.81106       0.163       0.350    0.019728         1.0
    100      70     0.36070     0.84040       0.161       0.350    0.022861         1.1
    100      80     0.32011     0.92668       0.136       0.357    0.026228         1.3
    100      90     0.30028     1.03825       0.127       0.373    0.030039         1.4
    100     100     0.28084     1.11324       0.122       0.3[...]
{'fraction_non_alphanumeric': 0.1339519870889651, 'fraction_numerical': 0.10006052047609441, 'mean_word_length': 2.377384196185286, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '398045', 'license': 'MIT', 'n_tokens_mistral': 2068, 'n_tokens_neox': 1800, 'n_words': 364, 'path': 'Lesson 28 - CIFAR-10 Cats and Dogs Round 6.ipynb', 'repo_name': 'derrowap/MA490-Deep-Learning', 'size': '4964'}