<jupyter_start><jupyter_text>Training dataset imagesThese can be found in folder mentioned in the list, below, named folderPath<jupyter_code>folderPath = ["traindb/p/", "traindb/n/", "traindb/a/"]
images, inputMatrix, outputMatrix = getInputFeature(folderPath, True)
inputMatrix = inputMatrix.T
outputMatrix = (outputMatrix).T

print("Input:\n")
for input in inputMatrix:
    print(input)
print("Output:\n",outputMatrix)
def initParameters(inpSize, outSize):
    np.random.seed(1)
    bias = np.zeros(shape=(outSize, 1))
    weights = np.random.randn(outSize, inpSize) * 0.01
    
    assert(bias.shape == (outSize, 1))
    assert(weights.shape == (outSize, inpSize))
    
    return [weights, bias]<jupyter_output><empty_output><jupyter_text>Initialize parameters according to architechture of Neural Networksince this is three layer model, which consists of 2 hidden layer hence model will be structured as 100(input nodes) --> 30(hidden nodes) --> 10(hidden node) --> 3(output nodes)<jupyter_code>parameters = []
layer_one_param = initParameters(100, 30)
parameters.append(layer_one_param)
layer_two_param = initParameters(30, 10)
parameters.append(layer_two_param)
layer_three_param = initParameters(10, 3)
parameters.append(layer_three_param)

print("Weights, Bias for layer 1:", parameters[0][0], "\n", parameters[0][1])
print("Weights, Bias for layer 2:", parameters[1][0], "\n", parameters[1][1])
print("Weights, Bias for layer 3:", parameters[2][0], "\n", parameters[2][1])
def sigmoid(X):
    Y = 1/(1+np.exp(-X))
    return Y
def differentialSigmoid(X, delBack):
    Y = 1/(1+np.exp(-X))
    dX = delBack*Y*(1-Y)
    assert(dX.shape == X.shape)
    
    return dX
def linearForward(X, wts, bias):
    Yin = np.dot(wts, X) + bias
    
    assert(Yin.shape == (wts.shape[0], X.shape[1]))
    
    return Yin
def forwardActivation(X, wts, bias):
    Yin = linearForward(X, wts, bias)
    Y = sigmoid(Yin)
    
    assert(Y.shape == (wts.shape[0], X.shape[1]))
    
    return Y, Yin
def linearBackProp(delY, layerCache):
    Yin, X, wts, bias, Yout = layerCache
    dY = differentialSigmoid(Yin, delY)
    m = X.shape[1]
    dW = np.dot(dY, X.T)*1.0/m
    dB = np.sum(dY, axis = 1, keepdims = True)
    dX = np.dot(wts.T, dY)
    return dX, dW, dB
def computeCost(Y, target):
    m = Y.shape[1]
    error = np.sum(np.power(Y-target, 2))
    cost = np.squeeze(error/m)
    return (cost, error)
def update_param(parameters, delParams, alpha):
    
    L = len(delParams)
    
    for i in range(L):
        parameters[i][0] -= alpha * delParams[i][0]
        parameters[i][1] -= alpha * delParams[i][1]
    return parameters
def trainModel(inputMatrix, outputMatrix, parameters, alpha = 0.05):
    wts = []
    bias = []
    costs = []
    layerCache = []
    grad = []
    epochCount = 0
    
    stopCondition = False
    
    while(not stopCondition and epochCount<100000):
        for param in parameters:
            wts.append(param[0])
            bias.append(param[1])
        
        Y = inputMatrix
        epochCount += 1
        error = 0
        layerCache = []
        for i in range(len(parameters)):
            inputgrid = Y
            Y, Yin = forwardActivation(inputgrid, wts[i], bias[i])
            layerCache.append((Yin, inputgrid, wts[i], bias[i], Y))
            
        cost, error = computeCost(Y, outputMatrix)
        
#         if(epochCount%200==0):
#             print("iteration #{} \n  Cost: {} \n  Error: {}".format(epochCount, cost, error))
        
        #  error backpropagation
        
        delY = -2*(outputMatrix - Y)
        delParams = []
        for i in range(len(parameters)):
            delH, delWh, delBh = linearBackProp(delY, layerCache[len(parameters) - i - 1])
            delY = delH
            delParams.append([delWh, delBh])
#         delX, delWx, delBx = linearBackProp(delH, layerCache[0])
        
#         delParams = [[delWx, delBx],[delWh, delBh]]
        delParams = delParams[::-1]
        
        parameters = update_param(parameters, delParams, alpha)
        
        if(error<0.005):
            stopCondition = True
    print("iteration #{} \n  Cost: {} \n  Error: {}".format(epochCount, cost, error))
    return parameters
parameters = trainModel(inputMatrix, outputMatrix, parameters, 0.5)
def testModel(folderPath, parameters):
    
    images, test_input  = getInputFeature(folderPath, False)

    print("Input:\n",len(test_input))
    Y = test_input
    Y=np.array(Y).T
    wts = []
    bias = []
    for param in parameters:
            wts.append(param[0])
            bias.append(param[1])
    for i in range(len(parameters)):
            inputgrid = Y
            Y, Yin = forwardActivation(inputgrid, wts[i], bias[i])
    Y = Y.T
    for case in range(len(Y)):
        pyplot.imshow(images[case][0])
        pyplot.show()
        print(images[case][1])
        
        temp=Y[case].tolist()
        ans_idx=temp.index(max(temp))
        
        print(temp)
        if(ans_idx==0):
            print("predicted P")
        elif(ans_idx==1):
            print("predicted N")
        else:
            print("predicted A")
    
folderPath = [["testdb/"], ["traindb/"]]
testModel(folderPath[0], parameters)
testModel(folderPath[1], parameters)
def runPrediction(alpha):
    folderPath = ["traindb/p/", "traindb/n/", "traindb/a/"]
    images, inputMatrix, outputMatrix= getInputFeature(folderPath, True)

    inputMatrix, outputMatrix = inputMatrix.T, outputMatrix.T
        

    parameters = []
    layer_one_param = initParameters(100, 30)
    layer_two_param = initParameters(30, 10)
    layer_three_param = initParameters(10, 3)
    
    parameters.append(layer_one_param)
    parameters.append(layer_two_param)
    parameters.append(layer_three_param)
    
    parameters = trainModel(inputMatrix, outputMatrix, parameters, alpha)

    
    folderPath = [["testdb/"], ["traindb/"]]
    testModel(folderPath[0], parameters)<jupyter_output><empty_output><jupyter_text>For each case we have taken threshold as cost < 0.005            or          epoch > 100000<jupyter_code>print("For given learning rate, 0.01")
runPrediction(0.01)
print("For given learning rate, 0.05")
runPrediction(0.05)
print("For given learning rate, 0.1")
runPrediction(0.1)
print("For given learning rate, 0.2")
runPrediction(0.2)
print("For given learning rate, 0.4")
runPrediction(0.4)
print("For given learning rate, 0.8")
runPrediction(0.8)<jupyter_output>For given learning rate, 0.8
iteration #5755 
  Cost: 0.000333283389711568 
  Error: 0.00499925084567352
Input:
 3
{'fraction_non_alphanumeric': 0.13731480067206353, 'fraction_numerical': 0.029326409042309456, 'mean_word_length': 2.9781287970838397, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 16, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '124946', 'license': 'MIT', 'n_tokens_mistral': 2303, 'n_tokens_neox': 2203, 'n_words': 499, 'path': 'recogniser_NN-3-Layer.ipynb', 'repo_name': 'appmonster007/basic-ebpa-NN', 'size': '6547'}