Posts

Python: I/O Operations

In this article we show how we can to input/output operations using user input and file. Python supports standard keyboard input and screen output using input and print keywords, respectively. File I/O has more option depending if you want to write, read or append a file. Python also supports text and binary mode of writing,however both modes have a very minor difference based on how cr-lf ('\r\n') is handled. The following code sample demonstrate how interactive user input and output are handled in Python. After that we look at code sample to perform File I/O. Inline comments (starting with # ) clarify the purpose of specific lines of code. # Code for demonstration of Python Dictionary class DemoScreenIOClass: def ReadFromKeyboard(self): print("---------- ReadFromKeyboard") # Read a value from keyboard self.__value = input('Enter any value:') # Validation can be added using a try-except block self.__numValue =...

Python: Data Collections - Dictionary

In a previous article , we looked at the features of the list data collection in Python. In this article we look at the dictionary collection type in Python. As in other languages, the dictionary is a list of key-value pairs and just like other variables in Python we do not explicitly need to define the data type of key and value. It is easy to do bulk updates in Python dictionaries using other dictionary objects, but unlike a list the slice(:) operator is not supported. Hence some bulk updates have to be done using a loop. The following code snippets demonstrate how dictionaries are created, modified and deleted. Inline comments (starting with # ) clarify the purpose of specific lines of code. # Code for demonstration of Python Dictionary class DemoDictClass: # List Initialization def Initialize(self): # Dictionaries are defined between curly brackets '{}' self._dictStr={"One" : 1, "Two":2, "Three":3, "Four"...

Python: Data Collections - Lists

Like other programming languages, Python supports collection data types like arrays and dictionaries. In these series of posts, we are only looking at the features of Python 3.x where there is no longer any array data type in this version. However, there is no need to worry as the List data type in Python 3.x has all features of an array plus more.Lists are initialized like an array but Python 3.x makes it very easy to add, update and remove elements which can be done as individual elements or as slices. When dealing with collections it is important to discuss the concept of slices. A slice refers to a group of consecutive elements of the list on which a single operation can be applied. As an example, if you have to divide elements from indexes 1 through 10 by of an array 'numbers' by 2 and transfer the results to a new array 'div', the operation can be performed by the following statement: div[0:9] = numbers[1:10]/2 In languages that don't support slices, a...

Python: Control Statements

In this article, we take a look at the major control statements in Python. Primary we look at the control statements if-elif-else, for,while and control exit statement break . We list sample code for each of these and use in-line comments to explain what the code line does. Note that we declare the same functions in a class to adhere to object-oriented semantics. You can easily copy/paste the code in your Python code. if-elif-else statement class DemoClass: def IfThenElse(self): # Using integer inp = int(input("Enter a number: ")) if inp The output for the above code is: Sum is :3 Enter a number: 12 Number is positive Enter your name: Chaitanya You are nobody ! for loop class DemoClass: def ForLoop(self): # Print values from 0 to 5 for value in range(6): print(value) # Print values from 1 to 5 for value in range(1,6): print(value) # Print values from 1 to 5, step of 2...

Part-III - Python: Object Oriented Programming

Image
In part-II of this series, we saw how inheritance works when we derive from a single base class. In part-II, the base and derived classes were all declared in the same file. In this article, we look at how classes can be declared in their own files and how they reference each other. The ability to reference source code in multiple files is a very important requirement towards building complex scalable applications. Our project solution will the have following 3 files: Python C# Comments CBaseMath.py CBaseMath.cs Base class code CAdvancedMath.py CAdvancedMath.cs Derived class code Program.py Program.cs Code to instantiate and call functions The code for each of these files is given below: CBaseMath.py / CBaseMath.cs Python class CBaseMath: def __init__(self, sigDigits): self.__sigDigits=sigDigits #This is a private function #There is no keyword in Python to make this protec...

Part-I: Neural Net Backpropagation using representative equation transformation

Image
Backpropagation in Neural Networks (NN) is a critical step in adjusting the weights of each layer and making the NN model more accurate based on the provided training data. In previous articles , we derived the backpropagation for a Neural Networks using the architecture of the network. In this series of articles, we convert the neural network architecture into a representative linear equation and use it to derive the backpropagation. As in other articles, we start with a very simple NN and proceed to more complex NN in subsequent articles. Consider the NN below with 1 hidden layer and only a single neuron in each layer. We have derived the backpropagation of this NN without using the equation form here . The output of this NN is \(r_o\), and the prediction error for each training sample \(E\) is defined as: $$ E = {(t - r_o)^2\over 2} \text { ...eq.(1.1)} $$ where t is given(expected) output for that specific training example. Our goal is minimize \(E\) by making \(r_o...

Part IV: Backpropagation mechanics for a Convolutional Neural Network

Image
In part-III of this series, we derived the weight update equation for the backpropagation operation of a simple Convolutional Neural Network (CNN) using 3 inputs matrices representing the red, blue and green spectrum of an image. In this article, we derive the backpropagation with 2 consecutive convolution and ReLu layers, and subsequently derive a general backpropagation equation for deep-learning (multi-layered) CNN. To keep our equations simple (and readable), we revert back to only 1 input matrix. This CNN is similar to one used in part-II, except there are 2 convolution and ReLu layers. The CNN we use is represented by: In this CNN, there are 1 4x4 input matrices, two 2x2 filter matrices (or kernels), two convolution layers , two ReLu layers, a single pooling layer (which applies the MaxPool function) and a single fully connected (FC) layer. The elements of the filter matrices are equivalent to the unit weights in a standard NN and will be updated during the backpropaga...