Skip to content

Commit

Permalink
Move __init__ to the beginning of the class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marina Samuel committed Apr 21, 2014
1 parent 36014bd commit 075cba7
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions ocr/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ class ocrNeuralNetwork:
WIDTH_IN_PIXELS = 20
NN_FILE_PATH = 'nn.json'

def __init__(self, numHiddenNodes, dataMatrix, dataLabels):
self.sigmoid = np.vectorize(self.sigmoidScalar)
self.sigmoidPrime = np.vectorize(self.sigmoidPrimeScalar)
self.dataMatrix = dataMatrix
self.dataLabels = dataLabels

if (not os.path.isfile('nn.json')):
# Step 1: Initialize weights to small numbers
self.theta1 = self.randInitializeWeights(400, numHiddenNodes)
self.theta2 = self.randInitializeWeights(numHiddenNodes, 10)
self.input_layer_bias = self.randInitializeWeights(1, numHiddenNodes)
self.hidden_layer_bias = self.randInitializeWeights(1, 10)

# Train using sample data
self.train([{"y0":self.dataMatrix[i], "label":int(ocrNeuralNetwork.dataLabels[i])} for i in ocrNeuralNetwork.sampleIndices[:3500]])
self.save()
else:
self.load()
#self.test()

def randInitializeWeights(self, sizeIn, sizeOut):
return [x * 0.12 - 0.06 for x in np.random.rand(sizeOut, sizeIn)]

Expand Down Expand Up @@ -112,23 +132,3 @@ def load(self):
self.input_layer_bias = [np.array(nn['b1'][0])]
self.hidden_layer_bias = [np.array(nn['b2'][0])]
nnFile.close()

def __init__(self, numHiddenNodes, dataMatrix, dataLabels):
self.sigmoid = np.vectorize(self.sigmoidScalar)
self.sigmoidPrime = np.vectorize(self.sigmoidPrimeScalar)
self.dataMatrix = dataMatrix
self.dataLabels = dataLabels

if (not os.path.isfile('nn.json')):
# Step 1: Initialize weights to small numbers
self.theta1 = self.randInitializeWeights(400, numHiddenNodes)
self.theta2 = self.randInitializeWeights(numHiddenNodes, 10)
self.input_layer_bias = self.randInitializeWeights(1, numHiddenNodes)
self.hidden_layer_bias = self.randInitializeWeights(1, 10)

# Train using sample data
self.train([{"y0":self.dataMatrix[i], "label":int(ocrNeuralNetwork.dataLabels[i])} for i in ocrNeuralNetwork.sampleIndices[:3500]])
self.save()
else:
self.load()
#self.test()

0 comments on commit 075cba7

Please sign in to comment.