'Code run in my Jupyter notebook gives different results from same code I run using Flask

I wrote a neural network code in jupyter notebook. This is a snippet of the code:

import numpy
import matplotlib.pyplot
%matplotlib inline
import imageio

class neuralNetwork:
    
    def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate):
        self.inodes = inputnodes
        self.hnodes = hiddennodes
        self.onodes = outputnodes
        self.wih = numpy.random.normal(0.0,pow(self.inodes,- 0.5),(self.hnodes,self.inodes))
        self.who = numpy.random.normal(0.0,pow(self.hnodes,- 0.5),(self.onodes,self.hnodes))
        
        self.activation_function = lambda x: scipy.special.expit(x)
        
        self.lr = learningrate
        pass

When I run it and test with my testing images, I get around 94% accuracy. I then created a flask application, and kept the above code in a .py file. I imported the code as:

from nn import neuralNetwork

Then I created instance an instance of the class and run the following code to train:

input_nodes = 784
hidden_nodes = 100
output_nodes = 10

learning_rate = 0.3

n = neuralNetwork(input_nodes,hidden_nodes,output_nodes,learning_rate)
training_data_file = open("mnist_dataset/mnist_train.csv",'r')
training_data_list = training_data_file.readlines()
training_data_file.close()

epochs = 1

for e in range(epochs):
    for record in training_data_list:
        all_values = record.split(',')
        inputs = (numpy.asfarray(all_values[1:])/255.0 * 0.99) + 0.01
        targets = numpy.zeros(output_nodes) + 0.01
    targets[int(all_values[0])] = 0.99
    n.train(inputs,targets)
    pass
pass

Then I write the following code to test it:

    test_data_list = test_data_file.readlines()

    scorecard = []
    a = 0
    for record in test_data_list:
        all_values = record.split(',')
        correct_label = int(all_values[0])
        inputs = (numpy.asfarray(all_values[1:])/255.0 * 0.99) + 0.01
        outputs = n.query(inputs)
        label = numpy.argmax(outputs)
        if(label == correct_label):
            scorecard.append(1)
        else:
            scorecard.append(0)
            pass
        pass

    scorecard_array = numpy.asarray(scorecard)

    return str(scorecard_array.sum()/scorecard_array.size)

But I realised that the value of "scorecard" is always below 10. But when I run the same code in Jupyter notebook, I get a scoredcard value above 90. Please any help on how to fix this issue



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source