class XorFunctionIntegrationTest(unittest.TestCase):

    def setUp(self):
        self.networkFile = 'network_XOR_SIG.xml'
        self.network = NetParser(COMMON_DIR_PREFIX + self.networkFile).parse()
        

    def tearDown(self):
        pass


    def testWithSigmoidActivationFunction(self):
        input_vector = InputVectorParser(COMMON_DIR_PREFIX + 'input_0_0.xml').parse()
        value = self.network.calculte_answer(input_vector)
        print('f(0,0) = ', str(self.network.calculte_answer(input_vector)))
        self.assertAlmostEqual(0, value[0], 1)

        input_vector = InputVectorParser(COMMON_DIR_PREFIX + 'input_1_0.xml').parse()
        value = self.network.calculte_answer(input_vector)
        print('f(1,0) = ', str(self.network.calculte_answer(input_vector)))
        self.assertAlmostEqual(1, value[0], 1)

        input_vector = InputVectorParser(COMMON_DIR_PREFIX + 'input_0_1.xml').parse()
        value = self.network.calculte_answer(input_vector)
        print('f(0,1) = ', str(self.network.calculte_answer(input_vector)))
        self.assertAlmostEqual(1, value[0], 1)

        input_vector = InputVectorParser(COMMON_DIR_PREFIX + 'input_1_1.xml').parse()
        value = self.network.calculte_answer(input_vector)
        print('f(1,1) = ', str(self.network.calculte_answer(input_vector)))
        self.assertAlmostEqual(0, value[0], 1)
        
    def testWithThresholdActivationFunction(self):
        pass
 def test_backpropagation(self):
     input_vectors = [InputVectorParser(COMMON_DIR_PREFIX + LEARNING_VECTOR_FILE_PREFIX + str(i) + '.xml').parse() for i in range(1, 5)]
     for input_vec in input_vectors:
         print(input_vec)
     #testVectors = [InputVectorParser(COMMON_DIR_PREFIX + TEST_VECTOR_FILE_PREFIX + str(i) + '.xml').parse() for i in range(1, 4)]
     network = NetParser(COMMON_DIR_PREFIX + NETWORK_FILE).parse()#lambda : random.uniform(-1.0, 1.0))
     learning_rate = 0.7
     momentum = 0.1
     iterations = 2000
     network.backpropagation_learn(input_vectors, learning_rate, iterations, momentum)
     print(network)
     error = 0.0
     for inputVector in input_vectors :
         inputVector.printVector(3)
         outputDict = []
         answer = network.calculte_answer(inputVector, False, outputDict)
         print(answer)
         for id, val in outputDict:
             error = error + 0.5*(inputVector.expected_value_dict[id]-val)**2
     print(error)
     print(network.calculte_answer(InputVector({'input1' : 0.5, 'input2' : 0.5})))
Beispiel #3
0
    weight_function = None
    if args.random:
        print(
            "Weight of links between nodes will be set to random values from range [{}; {}]".format(
                args.random[0], args.random[1]
            )
        )
        weight_function = lambda: random.uniform(args.random[0], args.random[1])
    elif args.zeros:
        print("Weight of links between nodes will be set to 0.")
        weight_function = lambda: 0.0
    network = NetParser(args.network).parse(weight_function)
    if args.vector_limits:
        input_vector = RandomInputVectorFactory.create_new(
            args.vector_limits[0], args.vector_limits[1], network.layers[0].get_nodes_ids()
        )
        print(
            "Generated vector with values from range [{}; {}]:\n{}".format(
                args.vector_limits[0], args.vector_limits[1], input_vector
            )
        )
    else:
        input_vectors = [InputVectorParser(vector).parse() for vector in args.vector_file]
    if args.kohonen:
        network.learning_process(input_vectors, args.coefficient, args.coefficient_half_life, args.turns)
    if args.backpropagation:
        network.backpropagation_learn(input_vectors, args.coefficient, args.turns)
    else:
        print("Network response is:")
        print(network.calculte_answer(input_vectors[0]))