<jupyter_start><jupyter_text>MINST Example<jupyter_code># Test if the system is GPU capable
from theano import function, config, shared, sandbox
import theano.tensor as T
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], T.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')
import cPickle, gzip
import numpy as np
import theano


#############
# LOAD DATA #
#############
# Load the dataset
print('... loading data')
f = gzip.open("./data/mnist.pkl.gz", "rb")
# train_set, valid_set, test_set = cPickle.load(f)
try:
    train_set, valid_set, test_set = cPickle.load(f, encoding='latin1')
except:
    train_set, valid_set, test_set = cPickle.load(f)
f.close()

# store your data and how to access a minibatch
def shared_dataset(data_xy):
    """ Function that loads the dataset into shared variables
    The reason we store our dataset in shared variables is to allow
    Theano to copy it into the GPU memory (when code is run on GPU).
    Since copying data into the GPU is slow, copying a minibatch everytime
    is needed (the default behaviour if the data is not in a shared
    variable) would lead to a large decrease in performance.
    """
    data_x, data_y = data_xy
    shared_x = theano.shared(np.asarray(data_x, dtype=theano.config.floatX)) 
    shared_y = theano.shared(np.asarray(data_y, dtype=theano.config.floatX)) # When storing data on the GPU it has to be stored as floats
    # therefore we will store the labels as ‘‘floatX‘‘ as well
    # (‘‘shared_y‘‘ does exactly that). But during our computations
    # we need them as ints (we use labels as index, and if they are
    # floats it doesn’t make sense) therefore instead of returning
    # ‘‘shared_y‘‘ we will have to cast it to int. This little hack
    # lets us get around this issue
    return shared_x, T.cast(shared_y, 'int32')

test_set_x, test_set_y = shared_dataset(test_set)
valid_set_x, valid_set_y = shared_dataset(valid_set)
train_set_x, train_set_y = shared_dataset(train_set)
batch_size = 500 # size of the minibatch

# accessing the third minibatch of the training set
data  = train_set_x[2 * batch_size: 3 * batch_size]
label = train_set_y[2 * batch_size: 3 * batch_size]<jupyter_output>... loading data<jupyter_text>Supervised Optimization for Deep Learning Supervised learning for classfication models and minibatch stochastic gradient descent used for fine-tuning many of the DLmodels.- Zero-One Loss<jupyter_code># zero_one_loss is a Theano variable representing a symbolic
# expression of the zero one loss ; to get the actual value this
# symbolic expression has to be compiled into a Theano function (see 
# the Theano tutorial for more details)
# f(x) = argmax_kP(Y |x,\Theta)
# where y = \Theta

zero_one_loss = T.sum(T.neq(T.argmax(p_y_given_x), y))

# NLL is a symbolic variable ; to get the actual value of NLL, this symbolic # expression has to be compiled into a Theano function (see the Theano
# tutorial for more details)
NLL = -T.sum(T.log(p_y_given_x)[T.arange(y.shape[0]), y])
# note on syntax: T.arange(y.shape[0]) is a vector of integers [0,1,2,...,len(y)].
# Indexing a matrix M by the two vectors [0,1,...,K], [a,b,...,k] returns the
# elements M[0,a], M[1,b], ..., M[K,k] as a vector.  Here, we use this
##<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11333513659724101, 'fraction_numerical': 0.010278604273735462, 'mean_word_length': 4.267806267806268, 'pattern_counts': {'":': 0, '<': 8, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '344176', 'license': 'MIT', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1239, 'n_words': 454, 'path': 'Theano/basictheano.ipynb', 'repo_name': 'abitofalchemy/xplograph', 'size': '3697'}