def convolutionTest(self): nn = NeuralNet() expected = np.array([[-1,0], [-3,-11], [-5,-17]]) # Convolve the vertical Sobel edge detector data = np.array([[1,1,1,0], [0,1,0,0], [2,1,3,4], [5,1,6,7], [8,9,10,11]]) kernel = np.array([[1,0,-1], [2,0,-2], [1,0,-1]]) output = nn.convolve(data, kernel) return self.arrayIsSame(output, expected)
def activationTest(self): nn = NeuralNet() data = np.array([[1,0,0,0], [1,1,1,0], [1,0,0,0], [1,0,0,0], [1,0,0,1]]) kernel = np.array([[1,0,-1], [2,0,-2], [1,0,-1]]) output = nn.convolve(data, kernel) expectedRelu = np.array([[2,2], [3,1], [4,0]]) expectedBin = np.array([[1,1], [1,1], [1,0]]) expectedSigmoid = np.array([[0.119,0.119], [0.047,0.269], [0.0180,0.731]]) expectedTanH = np.array([[0.964,0.964], [0.995,0.762], [0.999,-0.762]]) expected = np.array([[-1,0], [-3,-11], [-5,-17]]) reluOutput = nn.activationFunction(output, 'relu') binOutput = nn.activationFunction(output, 'binary') sigmoidOutput = nn.activationFunction(output, 'sigmoid') tanhOutput = nn.activationFunction(output, 'tanh') for index, value in np.ndenumerate(sigmoidOutput): sigmoidOutput[index] = round(value, 3) for index, value in np.ndenumerate(tanhOutput): tanhOutput[index] = round(value, 3) if not UnitTests.arrayIsSame(reluOutput, expectedRelu): print "Relu activationFunction test failed." print "Expected:" print expectedRelu print "Actual:" print reluOutput return False if not UnitTests.arrayIsSame(binOutput, expectedBin): print "Binary activationFunction test failed." print "Expected:" print expectedBin print "Actual:" print binOutput return False if not UnitTests.arrayIsSame(sigmoidOutput, expectedSigmoid): print "Sigmoid activationFunction test failed." print "Expected:" print expectedSigmoid print "Actual:" print sigmoidOutput return False if not UnitTests.arrayIsSame(tanhOutput, expectedTanH): print "tanh activationFunction test failed." print "Expected:" print expectedTanH print "Actual:" print tanhOutput return False return True