50 MEC VISWAS
4 min readJun 6, 2021

--

Building Our Own Classifier

To build a own classifier ,first we have install tensorflow , keras from anaconda ,this will make our process easy and using the jupyter notebook.in that import the keras ,load the image from mnist dataset ,process with dataset, next train the dataset and finally test the dataset.

Now we have to learn some topics that will useful for build classifier

FUTURE
Future statement is a directive to the compiler that a particular module should be compiled using syntax or semantics that will be available in a specified future release of Python.

_future_ is a real module, and serves three purposes:

To avoid confusing existing tools that analyze import statements and expect to find the modules they’re importing.

To ensure that future statements run under releases prior to 2.1 at least yield runtime exceptions (the import of _future_ will fail, because there was no module of that name prior to 2.1).

To document when incompatible changes were introduced, and when they will be — or were — made mandatory. This is a form of executable documentation, and can be inspected programmatically via importing _future_ and examining its contents.
from _future_ import division

print 8/7 # prints 1.1428571428571428

print 8//7 # prints 1

from __future__ import print_function

np.random.seed
NumPy random seed is simply a function that sets the random seed of the NumPy pseudo-random number generator. It provides an essential input that enables NumPy to generate pseudo-random numbers for random processes.

The random() function generates pseudo-random numbers based on a seed value.

KERAS
Keras is an API designed for human beings, not machines. Keras follows best practices for reducing cognitive load: it offers consistent & simple APIs, it minimizes the number of user actions required for common use cases, and it provides clear & actionable error messages. It also has extensive documentation and developer guides.

MNIST
MNIST is a database. The acronym stands for “Modified National Institute of Standards and Technology.” The MNIST database contains handwritten digits (0 through 9), and can provide a baseline for testing image processing systems.

MNIST is the “hello world” of machine learning. Data scientists will train an algorithm on the MNIST dataset simply to test a new architecture or framework, to ensure that they work.

MNIST is divided into two datasets: the training set has 60,000 examples of hand-written numerals, and the test set has 10,000.

now explanation some of line used in code

(x_train , y_train) ,(x_test , y_test) =mnist.load_data()
x_train is a training dataset ,it is used to train the algorithm, y_train is label of x_train

x_test is a testing dataset, it is used to test the algorithm, y_test is label of x_test

can we change the value of this Num_classes=10
Num_classes=10 ,we can’t change the class value, because our handwritten digits are in between 0 to 9.

batch_size=128
batch_size is a number of training data in the one iteration, In our project ,batch_size is 128 ,it train 128 data first iteration and next iteration ,it will train next 128 data ,it repeats untill reaches the end.

epochs=2
epoch is indicates the number of passes of the entire training dataset the algorithm has completed. epoch value will increase the accuracy of the prediction algorithm, error decreases . higher epoch value not increase the accuracy, the perfect epoch value is just an assumption, once reaches the prefect accuracy , we should not increase the epoch value ,because it leads to more errors. epoch value is based on size of the dataset.

SEQUENTIAL
For creating a own neural network is very difficult ,with help of keras and tensorflow it done very easily. In that ,there is sequential model and functional model.

Sequential model for easy to create models layer by layer for most problems

Functional model for create models that have a lot of flexibility.

Layers in our project
3 layers in our project . Creating layers are depends upon the training examples and the output of project.

To find optimal value of layer by two methods

1 Trial and error — → it just changing and checking the values by manually compares to performance

2 classic way /developer friendly : its very simple ,somebody done similar project to our project ,we just find value in internet and take that value as reference ,then now check trial and error to reference value .

we using the 512 in the dense layer , because this value gives the high performance and less error . This value is just get by Trail and error or using reference value . Some people use the formula but it won’t works always.

Optimizers

Optimizer are used to minimize the loss function and make our prediction accurate as possible. It will updates the parameters of model based on output of loss function.

In our project , optimizer is Stochastic Gradient Descent (SGD)

Reshaping the dataset
In a multilayer model ,reduce the image down into vector of pixel . Higher pixel of image cannot be used ,so we changing the pixel size before fed into algorithm. we used the float32 because it will easily fit to algorithm.

x_train = x_train.reshape(60000, 784)  # 28x28 = 784x_test = x_test.reshape(10000, 784)x_train = x_train.astype('float32')x_test = x_test.astype('float32')x_train /= 255  # 0 - 255 ...  0-1x_test /= 255   # normalizing your dataprint(x_train.shape[0], 'train samples')print(x_test.shape[0], 'test samples')60000 train samples
10000 test samples

Normalising the datasets
The pixel values in image must be scaled prior to providing the image as input to deep learning, the pixel value change to 1 and 0.

to_categorical
This is converts a class to binary class matrix.

# convert class vectors to binary class matricesy_train = np_utils.to_categorical(y_train, num_classes)y_test = np_utils.to_categorical(y_test, num_classes)

Train the dataset

history = model.fit( x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))

Test the dataset

score[1]

score[1]

Out[18]:

0.6517000198364258

--

--