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)
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)
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]))