def executeTest(self, indexRange, networkFile): inputVectors = [InputVectorParser(COMMON_DIR_PREFIX + LEARNING_VECTOR_FILE_PREFIX + str(i) + '.xml').parse() for i in indexRange] network = NetParser(COMMON_DIR_PREFIX + networkFile).parse(lambda : random.uniform(-1.0, 1.0)) speed = 0.5 momentum = 0.15 iterations = 5000 network.backpropagation_learn(inputVectors, speed, iterations, momentum) inputVectors = [InputVectorParser(COMMON_DIR_PREFIX + LEARNING_VECTOR_FILE_PREFIX + str(i) + '.xml').parse() for i in range(1, 9)] for inputVector in inputVectors : self.printResult(network, inputVector)
def test_backpropagation(self): input_vectors = [InputVectorParser(COMMON_DIR_PREFIX + LEARNING_VECTOR_FILE_PREFIX + str(i) + '.xml').parse() for i in range(1, 4)] 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.9 momentum = 0.6 iterations = 5000 network.backpropagation_learn(input_vectors, learning_rate, iterations, momentum) print(network) for inputVector in input_vectors : self.print_results(network, inputVector)
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, 4)] 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.5 momentum = 0.2 iterations = 5000 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)
def testSilly(self): network = NetParser(COMMON_DIR_PREFIX + "kohonen_network_lab2.xml").parse() vector1 = InputVectorParser(COMMON_DIR_PREFIX + "input1.xml").parse() vector2 = InputVectorParser(COMMON_DIR_PREFIX + "input2.xml").parse() vector3 = InputVectorParser(COMMON_DIR_PREFIX + "input3.xml").parse() vector4 = InputVectorParser(COMMON_DIR_PREFIX + "input4.xml").parse() coefficient = 0.12 conscience_coefficient = 0.0 for i in range(1, 32000): reducer = pow(0.5, i / 8000) network.learn(vector1, coefficient * reducer, conscience_coefficient * reducer, 1) network.learn(vector2, coefficient * reducer, conscience_coefficient * reducer, 1) network.learn(vector3, coefficient * reducer, conscience_coefficient * reducer, 1) network.learn(vector4, coefficient * reducer, conscience_coefficient * reducer, 1) if i % 8000 == 0: print(network) self.printResult(network, vector1) self.printResult(network, vector2) self.printResult(network, vector3) self.printResult(network, vector4) vector5 = InputVectorParser(COMMON_DIR_PREFIX + "input5.xml").parse() self.printResult(network, vector5)
def testCP(self): inputVectors = [InputVectorParser(COMMON_DIR_PREFIX + LEARNING_VECTOR_FILE_PREFIX + str(i) + '.xml').parse() for i in range(1, 10)] for input_vec in inputVectors: 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(0.0, 0.2)) coefficient = 0.04 coefficient_half_life = 500 grossberg_coefficient_half_life = 10000 turns = 5000 grossberg_coefficient = 0.4 neighbourhoodWidth = 0 network.cp_learning_process(inputVectors, coefficient, coefficient_half_life, turns, neighbourhoodWidth, grossberg_coefficient, grossberg_coefficient_half_life) print(network) inputVectors = [InputVectorParser(COMMON_DIR_PREFIX + LEARNING_VECTOR_FILE_PREFIX + str(i) + '.xml').parse() for i in range(1, 10)] for inputVector in inputVectors : self.printResult(network, inputVector) print('---------------------------------------------------------------------') for testVector in testVectors : self.printResult(network, testVector)
def setUp(self): self.networkFile = 'network_XOR_SIG.xml' self.network = NetParser(COMMON_DIR_PREFIX + self.networkFile).parse()
argument_parser.add_argument("--turns", help="how many turns learning will take", type=int) argument_parser.add_argument("--kohonen", action="store_true") argument_parser.add_argument("--backpropagation", action="store_true") args = argument_parser.parse_args() 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)