<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf
device_name = tf.test.gpu_device_name()
if device_name != '/device:GPU:0':
  raise SystemError('GPU device not found')
print('Found GPU at: {}'.format(device_name))
# snippet for uploading local files to colab
# from google.colab import files
# uploaded = files.upload()
# # colab does not persist local libraries, so we have to reinstall; colab requires prefix of `!`
!pip install numpy
!pip install matplotlib
!pip install seaborn
# import core python libraries for analysis
import pandas as pd
import numpy as np

# import graphics
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
%matplotlib inline
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))

# enable multiple outputs per cell 
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
# import machine learning
import tensorflow as tf
# Import MNIST dataset
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
n_train = mnist.train.num_examples # 55,000
n_validation = mnist.validation.num_examples # 5000
n_test = mnist.test.num_examples # 10,000
# define neural network architecture
n_input = 784   # input layer (28x28 pixels)
n_hidden1 = 512 # 1st hidden layer
n_hidden2 = 256 # 2nd hidden layer
n_hidden3 = 128 # 3rd hidden layer
n_output = 10   # output layer (0-9 digits)

learning_rate = 1e-4
n_iterations = 10000
batch_size = 128
dropout = 0.5
X = tf.placeholder("float", [None, n_input])
Y = tf.placeholder("float", [None, n_output])
keep_prob = tf.placeholder(tf.float32) 
# define weights & biases
weights = {
    'w1': tf.Variable(tf.truncated_normal([n_input, n_hidden1], stddev=0.1)),
    'w2': tf.Variable(tf.truncated_normal([n_hidden1, n_hidden2], stddev=0.1)),
    'w3': tf.Variable(tf.truncated_normal([n_hidden2, n_hidden3], stddev=0.1)),
    'out': tf.Variable(tf.truncated_normal([n_hidden3, n_output], stddev=0.1)),
}


biases = {
    'b1': tf.Variable(tf.constant(0.1, shape=[n_hidden1])),
    'b2': tf.Variable(tf.constant(0.1, shape=[n_hidden2])),
    'b3': tf.Variable(tf.constant(0.1, shape=[n_hidden3])),
    'out': tf.Variable(tf.constant(0.1, shape=[n_output]))
}
# setup each of the layers
layer_1 = tf.add(tf.matmul(X, weights['w1']), biases['b1'])
layer_2 = tf.add(tf.matmul(layer_1, weights['w2']), biases['b2'])
layer_3 = tf.add(tf.matmul(layer_2, weights['w3']), biases['b3'])
layer_drop = tf.nn.dropout(layer_3, keep_prob)
output_layer = tf.matmul(layer_3, weights['out']) + biases['out']
# setup gradient descent; use tensorflows AdamOptimizer which speeds up convergence by using momentum of descent
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=output_layer))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# Training & Testing
correct_pred = tf.equal(tf.argmax(output_layer, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# initialize and run model

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

# train on mini batches
for i in range(n_iterations):
    batch_x, batch_y = mnist.train.next_batch(batch_size)
    sess.run(train_step, feed_dict={X: batch_x, Y: batch_y, keep_prob:dropout})

    # print loss and accuracy (per minibatch)
    if i%100==0:
        minibatch_loss, minibatch_accuracy = sess.run([cross_entropy, accuracy], feed_dict={X: batch_x, Y: batch_y, keep_prob:1.0})
        print("Iteration", str(i), "\t| Loss =", str(minibatch_loss), "\t| Accuracy =", str(minibatch_accuracy))
# now let's run the model on test images
# set keep_prob to 1.0, so we keep all units in the network
test_accuracy = sess.run(accuracy, feed_dict={X: mnist.test.images, Y: mnist.test.labels, keep_prob:1.0})
print("\nAccuracy on test set:", test_accuracy)
import numpy as np
from PIL import Image
import random           # for selecting random images later
# pick random image from the entire set, display image and our prediction
n_images_to_show=7

# Get images from test set
test_images_totalnum = len(mnist.test.images)
test_images = mnist.test.images

print("We have ",test_images_totalnum , " images to work with.\n\n")

for i in range(n_images_to_show):
    image_to_pick = random.randint(1,test_images_totalnum)
    
    img = np.reshape(test_images[image_to_pick], [28, 28])
   
    plt.imshow(np.reshape(test_images[image_to_pick], [28, 28]), cmap='gray')
    plt.show()
    
    img_flat = img.flatten().reshape(784)
    
    prediction = sess.run(tf.argmax(output_layer,1), feed_dict={X: [img_flat]})
    print ("            Prediction for test image:", np.squeeze(prediction), "\n")

image_to_pick<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12851239669421488, 'fraction_numerical': 0.02727272727272727, 'mean_word_length': 5.254521963824289, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '158903', 'license': 'MIT', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1696, 'n_words': 419, 'path': 'CNN_to_predict_integers_in_images_v2.ipynb', 'repo_name': 'surfaceowl/google.colab_machinelearning', 'size': '4840'}