This is a precursor lab to the final project. You will write most of the functions...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
This is a precursor lab to the final project. You will write most of the functions to use in the final project here. The first thing is to load the MNIST data into our Ma- chine Learning programs. We will set up the digitClassifier.py to expect input as two matrices: (i) the set of training images X and (ii) the set of training labels Y (there will be a corresponding pair of test matrices). Download all 4 files of the MNIST is available at: http://yann.lecun.com/exdb/mnist/ The images in the MNIST data are 28 x 28 pixels, in a proprietory formt For our work we will flatten out each image to a single row of 28 x 28+ 1 = 785 ele- ments in the X matrix. The *+1* is the first element and correspond to the first column of 1's as for any regression X matrix. There are 60,000 example in the train-images-idx3-ubyte.gz file and 60,000 labels in the train-labels-idx1-ubyte.gz file. These are gzipped files. Please read the note (in bold under the data sets) (you may need to replace the '-' after images with a if you get a file-not-found error). The code to load the uncompressed files into your code is given below: # An MNIST loader. import numpy as np #import gzip import struct def load_images (filename): # Open and unzip the file of images: # with gzip.open(filename, 'rb') as f: fh open(filename, 'rb') #uncomment this if you want to use the # Read the header information into a bunch of variables _ignored, n_images, columns, rows = struct. unpack ('>IIII', fh.read(16)) #Read all the pixels into a NumPy array of bytes: all_pixels = np. frombuffer (fh.read(), dtype=np. uint8) # Reshape the pixels into a matrix where each line is an image: return all pixels. reshape (n_images, columns * rows) def stack_ones (X): c1 = np.ones (len (X)) return np.column_stack ((c1, X)) # can use np. shape (X) [0] # The following commands load the data into # 60000 images, each 785 elements (1 bias + 28 * 28 pixels) X_train = stack_ones (load_images ("train-images.idx3-ubyte")) #23Note "-" replace # 60K labels, each with value 1 if the digit is a five, and 0 otherwise Y_train = encode_sevens (load_labels ("train-labels.idx1-ubyte")) # 10000 images, each 785 elements, with the same structure as _train _test = stack_ones (load_images ("t10k-images.idx3-ubyte")) # 10000 labels, with the same encoding as Y_train Y_test = encode_sevens (load_labels ("t10k-labels.idx1-ubyte")) The above file loads all the data into constants X-train etc.. These can be imported into your classification program with import mnist as mn. Write functions: def train( X, Y, numIter, learningRate) This sets up the vector of s and find the s by calling the gradient function. (Recall: the beta vector is updated in each iteration by -gradient(...) *learningRate) def classify (X, beta) which simply rounds and returns the (the pre- dicted result for the logistic regression). def test (X, Y, beta) that computes the percentage of correct classification from: classify (X, beta). (Note the count of correct results are obtained by summing classify (X, beta) ==Y. Recall, Y = 1 for the selected digit and zeros for the rest). The function should print the loss for every tenth iteration. def loss (...) To compute the log-loss function (see Lecture 8 notes). def gradient (X, Y, beta) def sigPredict (X, beta) - (see Lecture 8 Notes) Run train for 100 iterations with a learningRate = 1.e-5 and then 1000 itera- tions with a learning Rate = 1. e-3 to experiment. This is a precursor lab to the final project. You will write most of the functions to use in the final project here. The first thing is to load the MNIST data into our Ma- chine Learning programs. We will set up the digitClassifier.py to expect input as two matrices: (i) the set of training images X and (ii) the set of training labels Y (there will be a corresponding pair of test matrices). Download all 4 files of the MNIST is available at: http://yann.lecun.com/exdb/mnist/ The images in the MNIST data are 28 x 28 pixels, in a proprietory formt For our work we will flatten out each image to a single row of 28 x 28+ 1 = 785 ele- ments in the X matrix. The *+1* is the first element and correspond to the first column of 1's as for any regression X matrix. There are 60,000 example in the train-images-idx3-ubyte.gz file and 60,000 labels in the train-labels-idx1-ubyte.gz file. These are gzipped files. Please read the note (in bold under the data sets) (you may need to replace the '-' after images with a if you get a file-not-found error). The code to load the uncompressed files into your code is given below: # An MNIST loader. import numpy as np #import gzip import struct def load_images (filename): # Open and unzip the file of images: # with gzip.open(filename, 'rb') as f: fh open(filename, 'rb') #uncomment this if you want to use the # Read the header information into a bunch of variables _ignored, n_images, columns, rows = struct. unpack ('>IIII', fh.read(16)) #Read all the pixels into a NumPy array of bytes: all_pixels = np. frombuffer (fh.read(), dtype=np. uint8) # Reshape the pixels into a matrix where each line is an image: return all pixels. reshape (n_images, columns * rows) def stack_ones (X): c1 = np.ones (len (X)) return np.column_stack ((c1, X)) # can use np. shape (X) [0] # The following commands load the data into # 60000 images, each 785 elements (1 bias + 28 * 28 pixels) X_train = stack_ones (load_images ("train-images.idx3-ubyte")) #23Note "-" replace # 60K labels, each with value 1 if the digit is a five, and 0 otherwise Y_train = encode_sevens (load_labels ("train-labels.idx1-ubyte")) # 10000 images, each 785 elements, with the same structure as _train _test = stack_ones (load_images ("t10k-images.idx3-ubyte")) # 10000 labels, with the same encoding as Y_train Y_test = encode_sevens (load_labels ("t10k-labels.idx1-ubyte")) The above file loads all the data into constants X-train etc.. These can be imported into your classification program with import mnist as mn. Write functions: def train( X, Y, numIter, learningRate) This sets up the vector of s and find the s by calling the gradient function. (Recall: the beta vector is updated in each iteration by -gradient(...) *learningRate) def classify (X, beta) which simply rounds and returns the (the pre- dicted result for the logistic regression). def test (X, Y, beta) that computes the percentage of correct classification from: classify (X, beta). (Note the count of correct results are obtained by summing classify (X, beta) ==Y. Recall, Y = 1 for the selected digit and zeros for the rest). The function should print the loss for every tenth iteration. def loss (...) To compute the log-loss function (see Lecture 8 notes). def gradient (X, Y, beta) def sigPredict (X, beta) - (see Lecture 8 Notes) Run train for 100 iterations with a learningRate = 1.e-5 and then 1000 itera- tions with a learning Rate = 1. e-3 to experiment.
Expert Answer:
Related Book For
Business Communication Essentials a skill based approach
ISBN: 978-0132971324
6th edition
Authors: Courtland L. Bovee, John V. Thill
Posted Date:
Students also viewed these algorithms questions
-
The first thing is to load the MNIST data into our Machine Learning programs. We will set up the digitClassifier.py to expect input as two matrices: (i) the set of training images X and (ii) the set...
-
QUIZ... Let D be a poset and let f : D D be a monotone function. (i) Give the definition of the least pre-fixed point, fix (f), of f. Show that fix (f) is a fixed point of f. [5 marks] (ii) Show that...
-
Reverse the order of integration in the following integrals. S.S. f(x, y) dx dy 0 1
-
Some people feel there are simply too many laws and regulations governing how companies may manage their employees. These people believe everyone would be better off if we let the free market work...
-
Equity equals: A. Assets Liabilities. B. Liabilities Assets. C. Assets + Liabilities.
-
A thin, long, straight wire is surrounded by plastic insulation of radius \(R\) and dielectric constant \(\kappa\) (Figure 26.32). The wire carries a uniform distribution of charge with a positive...
-
Mustafa Limited began operations on January 2, 2010. The company employs nine individuals who work eight-hour days and are paid hourly. Each employee earns 10 paid vacation days and six paid sick...
-
Sample Company Account Question You are provided with the trial balance of Twirl Ltd. as at 31st December 2019: Bank 78,000 Ordinary shares at 1 each Retained earnings at 1st January 2019 216,000...
-
A survey asks a representative group of citizens, do you support a cleaner environment? This survey exhibits what pitfall: Sample bias Response bias Response accuracy No pitfall
-
Received an invoice from DeKalb Transport for $2,300 for freight costs (on sales shipments) incurred during the past 30 days, terms n/30. Freight costs are charged to delivery expense when incurred....
-
If one table and two lamps cost $88, and two tables and three lamps cost $153, how much does a lamp cost?
-
Gearhead started out selling bicycles, but later added shoes, and then many other outdoor activity related items. this is an example of how gearhead's product assortment has multiple choice gotten...
-
The accounting practices of not-for-profits are more compatible with those of business than with those of government. Like governments, not-for-profits account for their resources in funds. How funds...
-
What are seven sources from which information about the organization's reporting procedures and practice might come.
-
Maria Garza?s regular hourly wage rate is $15.00, and she receives a wage of 1.5 times the regular hourly rate for work in excess of 40 hours. During a March weekly pay period, Maria worked 42 hours....
-
(a) Bright Sdn Bhd (BSB) is a tax resident manufacturing company in Johor, which involves in ceramic tiles. Currently, BSBs annual sales turnover has been forecasted to be around RM 300,000 for the...
-
Action Quest Games adjusts its accounts annually. The following information is available for the year ended December 31, 2025. 1. Purchased a 1-year insurance policy on June 1 for $1,800 cash. 2....
-
With each pass of a comet about the Sun, the comets mass (a) remains virtually unchanged. (b) actually increases. (c) is appreciably reduced.
-
Why is carbon such a special atom?
Study smarter with the SolutionInn App