Exemplic Neural Network with 2 layers.

This model has 2 layers - 1 input and 1 output layer. The input will be set of feature vector of images (here, 48 images (size: 500x400 approx)). Feature vector is tending to BTC(Block Truncation coding). The output layer will be trained set size similar to input number of images. The training process is done with activation function- sigmoid function which has unique property of differtiation and has range (-1,1)[This is weakness of function, which can be changed]. This model can be extended with layer augmentation(deep learning).

Training process is step-wise process:

  1. Set weights to randomized across output size.

  2. Set output to zeros with input training size.

  3. For definite iterations:

     i. set first layer to input training set i.e. feature vector set (This is forward propagation in layers)
     ii. Using sigmoid function, calculate multiplication of layer1 and weight.
     iii. Calculate error with respect to predicted output and find change in output with respect to error (layer2 delta).
     iv. Update the weights with summation of product of input layer and layer2 delta. 

Link: https://en.wikibooks.org/wiki/Artificial_Neural_Networks/Activation_Functions

Let us see python code.


In [2]:
# import and initialization.
import cv2
import os
import numpy as np

img_matrix = []
fv =[]
train_out = []

#function reading image into 2-D matrix.
def read_image():
    global img_matrix
    l1 = os.listdir('frames')
    if len(l1)>0:
        for name in l1:
            img_matrix.append(cv2.imread('frames/'+name))

#sigmoid function.
def sigmoid(x,deriv=False):
    if deriv == True:
        return x*(1-x)
    return 1/(1+np.exp(-x))

#feature vector function.
#here only mean of R,G,B spectral taken as feature vector. In next version, consider for this for BTC, TSTBTC etc.
def set_fv():
    global img_matrix,fv
    for img in img_matrix:
        fv.append([np.mean(img[:,:,0]),np.mean(img[:,:,1]),np.mean(img[:,:,2])]) 
        # input size is 48*3 as 48 is image used in data set and 3 for feature vector per image
        
# This section contains training function. 
def train_NN():
    global fv,train_out
    out = np.zeros((len(fv),1)) # We dont know about output so initialised to zero.
    np.random.seed(1)
    wt = 2*np.random.random((3,1)) - 1 # initialise weights to random numbers across input feature vector column size.
    for i in range(len(img_matrix)): 
        for j in xrange(10000): # definite iterations for learning.
            layer1_in = np.asarray(fv) # layer1 has 48x3 size
            layer2_out = sigmoid(np.dot(layer1_in,wt)) #layer2 has 48x1 size as 48x3 dot product 3x1
            layer2_err = out - layer2_out # error has 48x1 size
            layer2_deriv = layer2_err*sigmoid(layer2_out,True) #delta change has size 48x1
            wt += np.dot(layer1_in.T,layer2_deriv) #weight matrix calculation 3x48 dot product 48x1 
    train_out.append(layer2_out)

read_image()
set_fv()
train_NN()
print train_out


[array([[  5.56696425e-29],
       [  1.97593607e-05],
       [  1.26445420e-14],
       [  2.32833116e-17],
       [  3.07694498e-08],
       [  4.58782374e-29],
       [  1.69940771e-64],
       [  4.52620598e-17],
       [  3.69949184e-29],
       [  5.65514558e-29],
       [  1.89391949e-17],
       [  4.93117224e-29],
       [  1.23933008e-28],
       [  2.20049901e-22],
       [  1.37556311e-25],
       [  4.89364761e-29],
       [  5.75399800e-23],
       [  4.33540381e-25],
       [  7.37554174e-29],
       [  1.61630347e-32],
       [  2.13057224e-05],
       [  9.52683994e-29],
       [  1.59499210e-26],
       [  1.00130933e-28],
       [  4.33615611e-16],
       [  2.08385840e-19],
       [  8.16228343e-29],
       [  7.32885407e-29],
       [  6.11457702e-16],
       [  3.49874180e-29],
       [  5.66594203e-29],
       [  5.00157965e-16],
       [  4.32849407e-29],
       [  4.83649296e-29],
       [  3.54854752e-20],
       [  1.22007917e-16],
       [  3.77331623e-16],
       [  9.64131040e-15],
       [  1.34727049e-17],
       [  7.00614839e-23],
       [  7.77686415e-44],
       [  1.86521859e-31],
       [  1.49016386e-28],
       [  6.40400511e-29],
       [  1.37697536e-32],
       [  1.65675110e-16],
       [  1.36791063e-28],
       [  3.24961459e-22]])]

In [ ]: