def testEmbeddingIndexing(self): filename = "conll_reader_tests/embedding_index.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path(filename) self.setup_holder.block_loader.load(filepath) conll_filepath = self.setup_holder.filepath_handler.get_test_block_path("conll_reader_tests/test_conll_file.conll") self.setup_holder.variable_repository.set_variable_value("data_file", conll_filepath) embedding_filepath = self.setup_holder.filepath_handler.get_test_block_path( "conll_reader_tests/test_embeddings.txt") self.setup_holder.variable_repository.set_variable_value("embedding_file", embedding_filepath) component_spec = CreationComponentSpecifications() component_spec.name = "indexer" target = self.setup_holder.component_repository.get(component_spec)[0] target_socket = target.get_out_socket("output") runs = [[target_socket]] run_graphs = self.setup_holder.graph_converter.to_executable(runs) self.assertEqual(1, len(run_graphs)) self.assertEqual([[[0,1,2,3,4], [5,1,0,4]]], run_graphs[0].execute())
def testLoadsSimpleComponent(self): component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Constant" component_type = self.setup_holder.type_repository.create( component_type_spec) def test_assign(dic): dic["value"] = "test_value" dic["value_type"] = "test_value" component_type.assign_default_value = test_assign text = """<component name="constant_1" type="Constant"><value>5.17</value><type>float</type></component>""" self.setup_holder.component_loader.load_component(text, 0) self.assertEqual(1, self.setup_holder.component_repository.count()) spec = CreationComponentSpecifications() spec.name = "constant_1" components = self.setup_holder.component_repository.get(spec) self.assertEqual(1, len(components)) self.assertEqual("constant_1", components[0].name) self.assertEqual("Constant", components[0].get_component_type_name()) self.assertEqual([("5.17", {})], components[0].get_value_dictionary()["value"]) self.assertEqual([("float", {})], components[0].get_value_dictionary()["type"])
def testLoadsSimpleCanvasWithEdge(self): component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Constant" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.out_sockets = ["out"] component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Printer" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.in_sockets = ["in"] text = """<canvas name="main"> <component name="constant_1" type="Constant"></component> <component name="printer" type="Printer"></component> <edge><source socket=out>constant_1</source><target socket=in>printer</target></edge> </canvas>""" self.setup_holder.canvas_loader.load_canvas(text, 0) spec = CreationComponentSpecifications() spec.name = "constant_1" components = self.setup_holder.component_repository.get(spec) graph = components[0].graph self.assertEqual(1, len(graph.get_edges())) self.assertEqual(2, len(graph.get_vertices())) edge = graph.get_edges()[0] self.assertEqual("constant_1", edge.get_source_component_name()) self.assertEqual("printer", edge.get_target_component_name())
def testAccuracyOnly(self): filename = "iris_tests/untrained_iris_accuracy.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) data_filepath = self.setup_holder.filepath_handler.get_test_block_path( "iris_tests") self.setup_holder.variable_repository.set_variable_value( "data_folder", data_filepath) component_spec = CreationComponentSpecifications() component_spec.name = "accuracy" component = self.setup_holder.component_repository.get( component_spec)[0] accuracy = component.get_out_socket("output") ml_helper = self.setup_holder.ml_helper_factory.build_ml_helper( evaluate=accuracy) performance = ml_helper.evaluate() self.assertGreaterEqual(1.0, performance) self.assertLessEqual(0.0, performance) self.assertFalse(ml_helper.evaluate_function.has_batches("test"))
def testAccuracyOnlyWithoutMlHelper(self): filename = "iris_tests/untrained_iris_accuracy.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) data_filepath = self.setup_holder.filepath_handler.get_test_block_path( "iris_tests") self.setup_holder.variable_repository.set_variable_value( "data_folder", data_filepath) component_spec = CreationComponentSpecifications() component_spec.name = "accuracy" component = self.setup_holder.component_repository.get( component_spec)[0] accuracy = component.get_out_socket("output") runs = [[accuracy]] run_graphs = self.setup_holder.graph_converter.to_executable(runs) self.setup_holder.initialization_helper.initialize(run_graphs) run_graphs[0].init_batches() performance = sum(run_graphs[0].execute()[0]) self.assertGreaterEqual(10.0, performance) self.assertLessEqual(0.0, performance)
def testTileDuplicatesProperly(self): #TODO: Move to separate file filename = "iris_tests/batch_and_tile_iris.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) data_filepath = self.setup_holder.filepath_handler.get_test_block_path( "iris_tests") self.setup_holder.variable_repository.set_variable_value( "data_folder", data_filepath) component_spec = CreationComponentSpecifications() component_spec.name = "tile" adder = self.setup_holder.component_repository.get(component_spec)[0] target_socket_1 = adder.get_out_socket("output") runs = [[target_socket_1]] run_graphs = self.setup_holder.graph_converter.to_executable(runs) result = run_graphs[0].execute()[0] self.assertEqual(100, len(result)) self.assertEqual(25, len(result[0])) for i in range(50): self.assertEqual(result[i * 2], result[i * 2 + 1]) for i in range(5): for j in range(4): self.assertEqual(result[0][i * 5], result[0][i * 5 + j + 1])
def testDeIndexer(self): filename = "conll_reader_tests/conll_reader_with_indexer.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path(filename) self.setup_holder.block_loader.load(filepath) conll_filepath = self.setup_holder.filepath_handler.get_test_block_path( "conll_reader_tests/test_conll_file.conll") self.setup_holder.variable_repository.set_variable_value("data_file", conll_filepath) component_spec = CreationComponentSpecifications() component_spec.name = "deindexer" adder = self.setup_holder.component_repository.get(component_spec)[0] target_socket = adder.get_out_socket("output") runs = [[target_socket]] run_graphs = self.setup_holder.graph_converter.to_executable(runs) self.assertEqual(1, len(run_graphs)) results = run_graphs[0].execute() self.assertEqual([[["this", "is", "a", "sentence", "."], ["so", "is", "this", "."]]], results)
def testEmbeddingLookup(self): filename = "conll_reader_tests/embedding_lookup.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path(filename) self.setup_holder.block_loader.load(filepath) conll_filepath = self.setup_holder.filepath_handler.get_test_block_path("conll_reader_tests/test_conll_file.conll") self.setup_holder.variable_repository.set_variable_value("data_file", conll_filepath) embedding_filepath = self.setup_holder.filepath_handler.get_test_block_path( "conll_reader_tests/test_embeddings.txt") self.setup_holder.variable_repository.set_variable_value("embedding_file", embedding_filepath) component_spec = CreationComponentSpecifications() component_spec.name = "embedding_lookup" target = self.setup_holder.component_repository.get(component_spec)[0] target_socket = target.get_out_socket("output") runs = [[target_socket]] run_graphs = self.setup_holder.graph_converter.to_executable(runs) self.setup_holder.initialization_helper.initialize(run_graphs) results = run_graphs[0].execute()[0] self.assertEqual(2, len(results)) self.assertEqual(5, len(results[0])) self.assertEqual(4, len(results[1])) for r in results[0]: self.assertEqual(3, len(r)) for r in results[1]: self.assertEqual(3, len(r))
def testCreateWithUndeclaredTypeRaisesException(self): type_specs = ComponentTypeSpecifications() type_specs.name = "TestType" specs = CreationComponentSpecifications() specs.component_type_name = "TestType" with self.assertRaises(ComponentTypeNotFoundException): self.repository.create(specs)
def testFullTraining(self): filename = "iris_tests/full_iris.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) data_filepath = self.setup_holder.filepath_handler.get_test_block_path( "iris_tests") self.setup_holder.variable_repository.set_variable_value( "data_folder", data_filepath) component_spec = CreationComponentSpecifications() component_spec.name = "adam_upd" adder = self.setup_holder.component_repository.get(component_spec)[0] update = adder.get_out_socket("update") component_spec = CreationComponentSpecifications() component_spec.name = "cross_ent" adder = self.setup_holder.component_repository.get(component_spec)[0] loss = adder.get_out_socket("output") component_spec = CreationComponentSpecifications() component_spec.name = "accuracy" component = self.setup_holder.component_repository.get( component_spec)[0] accuracy = component.get_out_socket("output") ml_helper = self.setup_holder.ml_helper_factory.build_ml_helper( update=update, loss=loss, evaluate=accuracy) ml_helper.train() performance = ml_helper.evaluate() self.assertGreaterEqual(1.0, performance) self.assertLess(0.9, performance)
def testCreateWithComponentTypeName(self): type_specs = ComponentTypeSpecifications() type_specs.name = "TestType" component_type = self.setup_holder.type_repository.create(type_specs) specs = CreationComponentSpecifications() specs.component_type_name = "TestType" element = self.repository.create(specs) self.assertIsNotNone(element.component_type) self.assertEqual(component_type, element.component_type) self.assertEqual("TestType", element.get_component_type_name())
def testCreateAssignsLanguage(self): type_specs = ComponentTypeSpecifications() type_specs.name = "TestType" component_type = self.setup_holder.type_repository.create(type_specs) component_type.languages = ["test_language", "other_test_language"] specs = CreationComponentSpecifications() specs.component_type_name = "TestType" element = self.repository.create(specs) self.assertIsNotNone(element.language) self.assertEqual("test_language", element.language)
def testGetByName(self): specs = CreationComponentSpecifications() specs.name = "testComponent" element_1 = self.repository.create(specs) element_retrieved = self.repository.get(specs) self.assertEqual(1, len(element_retrieved)) self.assertEqual(element_1.identifier, element_retrieved[0].identifier) self.assertEqual(element_1, element_retrieved[0]) specs.name = "falseTestName" element_retrieved = self.repository.get(specs) self.assertEqual(0, len(element_retrieved))
def testIntegratedBatches(self): filename = "iris_tests/integrated_batch_iris.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) data_filepath = self.setup_holder.filepath_handler.get_test_block_path( "iris_tests") self.setup_holder.variable_repository.set_variable_value( "data_folder", data_filepath) component_spec = CreationComponentSpecifications() component_spec.name = "data_splitter" adder = self.setup_holder.component_repository.get(component_spec)[0] target_socket_1 = adder.get_out_socket("left") runs = [[target_socket_1]] run_graphs = self.setup_holder.graph_converter.to_executable(runs) run_graphs[0].init_batches() result = run_graphs[0].execute()[0] self.assertTrue(run_graphs[0].has_batches("test")) self.assertEqual(40, len(result)) result = run_graphs[0].execute()[0] self.assertTrue(run_graphs[0].has_batches("test")) self.assertEqual(40, len(result)) result = run_graphs[0].execute()[0] self.assertTrue(run_graphs[0].has_batches("test")) self.assertEqual(40, len(result)) result = run_graphs[0].execute()[0] self.assertEqual(30, len(result)) self.assertFalse(run_graphs[0].has_batches("test")) run_graphs[0].init_batches() result = run_graphs[0].execute()[0] self.assertTrue(run_graphs[0].has_batches("test")) self.assertEqual(40, len(result)) result = run_graphs[0].execute()[0] self.assertTrue(run_graphs[0].has_batches("test")) self.assertEqual(40, len(result)) result = run_graphs[0].execute()[0] self.assertTrue(run_graphs[0].has_batches("test")) self.assertEqual(40, len(result)) result = run_graphs[0].execute()[0] self.assertEqual(30, len(result)) self.assertFalse(run_graphs[0].has_batches("test"))
def testCreateWithCanvasName(self): canvas_specs = CanvasSpecifications() canvas_specs.name = "TestCanvas" canvas = self.setup_holder.canvas_repository.create(canvas_specs) specs = CreationComponentSpecifications() specs.canvas_name = "TestCanvas" element = self.repository.create(specs) self.assertIsNotNone(element.canvas) self.assertEqual(canvas, element.canvas) self.assertEqual(1, len(canvas.components)) self.assertEqual(element, canvas.components[0]) self.assertEqual("TestCanvas", element.get_canvas_name())
def testBasicLanguageModelWithEmbeddingTenSentences(self): filename = "seq_to_seq_tests/basic_language_model_with_embedding.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) data_filepath = self.setup_holder.filepath_handler.get_test_block_path( "conll_reader_tests") self.setup_holder.variable_repository.set_variable_value( "data_folder", data_filepath) self.setup_holder.variable_repository.set_variable_value( "data_file", "larger_test_conll_file.conll") component_spec = CreationComponentSpecifications() c = self.setup_holder.component_repository.get(component_spec)[0] graph = c.get_graph() ml_helper = self.setup_holder.ml_helper_factory.build_ml_helper_from_graph( graph) ml_helper.train() loss = ml_helper.validate() self.assertLess(0.0, loss) self.assertGreater(0.35, loss)
def testBeamSearchNoDuplicateStops(self): filename = "seq_to_seq_tests/basic_language_model_with_beam_search_top_3.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) data_filepath = self.setup_holder.filepath_handler.get_test_block_path( "conll_reader_tests") self.setup_holder.variable_repository.set_variable_value( "data_folder", data_filepath) self.setup_holder.variable_repository.set_variable_value( "data_file", "larger_test_conll_file.conll") component_spec = CreationComponentSpecifications() c = self.setup_holder.component_repository.get(component_spec)[0] graph = c.get_graph() ml_helper = self.setup_holder.ml_helper_factory.build_ml_helper_from_graph( graph) for i in range(2000): ml_helper.train(1) predictions = ml_helper.predict() for prediction in predictions: if len(prediction) < 50: self.assertEqual(prediction.count("_STOP_"), 1)
def testDimDropouts(self): filename = "iris_tests/full_iris_with_dim_dropouts.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) data_filepath = self.setup_holder.filepath_handler.get_test_block_path( "iris_tests") self.setup_holder.variable_repository.set_variable_value( "data_folder", data_filepath) self.setup_holder.variable_repository.set_variable_value( "input_dropout", 0.1) self.setup_holder.variable_repository.set_variable_value( "output_dropout", 0.9999999) component_spec = CreationComponentSpecifications() c = self.setup_holder.component_repository.get(component_spec)[0] graph = c.get_graph() ml_helper = self.setup_holder.ml_helper_factory.build_ml_helper_from_graph( graph) ml_helper.train() performance = ml_helper.evaluate() self.assertGreater(0.7, performance)
def testCanAddEdgeWithinGraph(self): c_spec = CreationComponentSpecifications() c1 = self.setup_holder.component_repository.create(c_spec) c2 = self.setup_holder.component_repository.create(c_spec) out_s = CreationComponentOutSocket(c1, "test_out") in_s = CreationComponentInSocket(c2, "test_in") c1.add_out_socket(out_s) c2.add_in_socket(in_s) g1 = c1.graph g2 = c2.graph self.setup_holder.graph_repository.merge(g1, g2) self.setup_holder.graph_repository.add_edge(out_s, in_s) self.assertEqual(1, len(g1.edges)) edge = g1.edges[0] self.assertEqual(out_s, edge.source_socket) self.assertEqual(in_s, edge.target_socket) self.assertEqual([edge], out_s.edges) self.assertEqual(edge, in_s.edge)
def testMultiplyByAdding(self): filename = "multiply_by_adding.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) component_spec = CreationComponentSpecifications() component_spec.name = "adder_3" adder = self.setup_holder.component_repository.get(component_spec)[0] target_socket = adder.get_out_socket("output") runs = [[target_socket]] run_graphs = self.setup_holder.graph_converter.to_executable(runs) self.assertEqual(1, len(run_graphs)) self.assertEqual([12.4], run_graphs[0].execute())
def testSecondLadderAdd(self): filename = "tensorflow_unit_test_blocks/ladder_add_tensorflow_2.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path(filename) self.setup_holder.block_loader.load(filepath) component_spec = CreationComponentSpecifications() component_spec.name = "adder_3" adder = self.setup_holder.component_repository.get(component_spec)[0] target_socket = adder.get_out_socket("output") runs = [[target_socket]] run_graphs = self.setup_holder.graph_converter.to_executable(runs) self.setup_holder.initialization_helper.initialize(run_graphs) self.assertEqual(1, len(run_graphs)) self.assertAlmostEqual(8.0, run_graphs[0].execute()[0], places=5)
def testExcludesIrrelevantPartsFromExecution(self): filename = "add_constants_with_extra_adder.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) component_spec = CreationComponentSpecifications() component_spec.name = "adder" adder = self.setup_holder.component_repository.get(component_spec)[0] target_socket = adder.get_out_socket("output") runs = [[target_socket]] run_graphs = self.setup_holder.graph_converter.to_executable(runs) self.setup_holder.initialization_helper.initialize(run_graphs) self.assertEqual(1, len(run_graphs)) self.assertEqual([8.15], run_graphs[0].execute())
def testModeSplitOnlyUsedPartsPulled(self): filename = "mode_pull_constants.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) component_spec = CreationComponentSpecifications() component_spec.name = "mode_split" adder = self.setup_holder.component_repository.get(component_spec)[0] target_socket = adder.get_out_socket("output") runs = [[target_socket], [target_socket]] run_graphs = self.setup_holder.graph_converter.to_executable( runs, run_modes=["train", "test"]) self.assertEqual(2, len(run_graphs[0].get_components())) self.assertEqual(2, len(run_graphs[1].get_components()))
def testCreateWithComponentTypeAssignsDefaultValue(self): type_specs = ComponentTypeSpecifications() type_specs.name = "TestType" component_type = self.setup_holder.type_repository.create(type_specs) def test_assign(dic): dic["test_field"] = "test_value" component_type.assign_default_value = test_assign specs = CreationComponentSpecifications() specs.component_type_name = "TestType" element = self.repository.create(specs) self.assertIsNotNone(element.component_value) self.assertEqual({"test_field": "test_value"}, element.component_value)
def testAdderWithVariable(self): filename = "add_constants_with_variable.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) component_spec = CreationComponentSpecifications() component_spec.name = "adder" adder = self.setup_holder.component_repository.get(component_spec)[0] target_socket = adder.get_out_socket("output") runs = [[target_socket], [target_socket]] run_modes = ["train", "test"] run_graphs = self.setup_holder.graph_converter.to_executable( runs, run_modes=run_modes) self.assertEqual(2, len(run_graphs)) self.assertEqual([8.15], run_graphs[0].execute()) self.assertEqual([13.15], run_graphs[1].execute())
def testCreateAssignsUID(self): specs = CreationComponentSpecifications() element_1 = self.repository.create(specs) element_2 = self.repository.create(specs) self.assertIsNotNone(element_1) self.assertIsNotNone(element_2) self.assertIsNotNone(element_1.identifier) self.assertIsNotNone(element_2.identifier) self.assertNotEqual(element_1.identifier, element_2.identifier)
def testCellSanity(self): filename = "seq_to_seq_tests/repeat_rnn_cell_sanity.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) component_spec = CreationComponentSpecifications() component_spec.name = "cell" target_c = self.setup_holder.component_repository.get( component_spec)[0] target_socket = target_c.get_out_socket("output") runs = [[target_socket]] run_graphs = self.setup_holder.graph_converter.to_executable(runs) self.assertEqual(1, len(run_graphs)) output = run_graphs[0].execute()[0] self.assertEqual(5.17, output[0]) self.assertEqual(2.98, output[1])
def testCreateAssignsInSockets(self): type_specs = ComponentTypeSpecifications() type_specs.name = "TestType" component_type = self.setup_holder.type_repository.create(type_specs) component_type.in_sockets = ["in_1", "in_2"] specs = CreationComponentSpecifications() specs.component_type_name = "TestType" element = self.repository.create(specs) self.assertIsNotNone(element.out_sockets) self.assertIsNotNone(element.in_sockets) self.assertEqual(0, element.count_out_sockets()) self.assertEqual(2, element.count_in_sockets()) self.assertIn("in_1", element.in_sockets) self.assertIn("in_1", element.in_sockets) self.assertIsNotNone(element.get_in_socket("in_1")) self.assertIsNotNone(element.get_in_socket("in_2"))
def testLoadsLanguagesCorrect(self): component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Constant" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.out_sockets = ["output"] component_type.languages = ["test_language"] component_type_spec = ComponentTypeSpecifications() component_type_spec.name = "Add" component_type = self.setup_holder.type_repository.create( component_type_spec) component_type.in_sockets = ["left", "right"] component_type.languages = ["tensorflow", "python"] filename = "add_constants.xml" filepath = self.setup_holder.filepath_handler.get_test_block_path( filename) self.setup_holder.block_loader.load(filepath) spec = CreationComponentSpecifications() spec.name = "constant_1" component = self.setup_holder.component_repository.get(spec)[0] self.assertEqual("test_language", component.language) spec = CreationComponentSpecifications() spec.name = "adder" component = self.setup_holder.component_repository.get(spec)[0] self.assertEqual("python", component.language)
def testCreateAddsToRepository(self): specs = CreationComponentSpecifications() self.assertEqual(0, self.repository.count()) element_1 = self.repository.create(specs) self.assertEqual(1, self.repository.count()) element_2 = self.repository.create(specs) self.assertEqual(2, self.repository.count())